Restart Maya from MEL

maya
mel

#1

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.


#2

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.


#3

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.


#4

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


#5

Ha! I knew there was a way.


#6

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


#7

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.


#8

The Mel code

getenv maya_location

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


#9

I think

import sys
maya = sys.executable

#10

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 

#11

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


#12

@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