Restart Maya from MEL

Hi!

Working on installation script for my tool, trying to make it as simple and intuitive as I can.

Is there a way to restart maya from within MEL? Not just close it, but actually close and start again. Ideally - crossplatform.

You could potentially hack together something using the system command, and some shell scripts, but it wouldn’t be cross platform.

Might be easier to execute from python, but for the life of me I can’t remember how to kick off a new process, without it being a child of the currently running one, so that it doesn’t close along with its parent.

I think in Python you could do it by kicking off a daemon thread that waited for some amount of time and then launched Maya using os.system.

if you were really masochistic you could start a maya in batch mode using MEL that did the same thing: wait for some length of time and then start a regular Maya. That would be stretching MEL way beyond it’s comfort zone, but it would be relatively platform independent.

our studio’s restart procedure is python, ands it works like this:

-get a maya_executable path variable
-get a scene_name variable
-save user prefs
-prompt user to save the maya file (unless a force flag is passed)
-use os.spawnl() to launch maya from the command line with args to re open the current scene:

`os.spawnl(os.P_NOWAIT, maya_executable, '-file', current_scene)`

-quit the current session

each one of those is doable in mel
except possibly os.spawnl()
you might be able to achieve similar with MEL’s system command

1 Like

Ha! I knew there was a way.

I think in essence when you need to do a meta-maya session’ operation ( such as restarting) you have to pop out a level and do it with os shell commands

1 Like

That’s cool! Thanks, guys.

But then how to reliably find a path to maya executable? I don’t think there’s such env variable available, and from what I gather even maya location is not fully reliable. Again, ideally cross platform, but at least for Windows.

The Mel code

getenv maya_location

will return the folder C:/Program Files/Autodesk/Maya20XX
you can then append “/bin/maya.exe” to it.

I think

import sys
maya = sys.executable
1 Like

For python apart from the above suggestions I found an interesting approach!

def launch_code():
     # something like subprocess with maya path and file name

import atexit
# Register the code to be executed at python exit
atexit.register(launch_code)
# Then quit maya 

That’s a nice way to do it – although you might need to be careful to check for other atexit handlers. You’ll need to make sure that the relaunch is in a new process (so I think subprocess, not just os.system or a Popen

1 Like

@Mambo4, that’ll work for windows, what about Mac and Linux?

@Theodox, oh, looks cool if it works, I’ll check it out, thanks

@cg-cnu, Gotta check how stable atexit is in Maya, but looks good