Run Maya and execute a script

How can I execute a python scripts after I launch Maya with subprocess


I need Maya with GUI. And the Python script I need to run is not static (and I don`t need to execute something all the time I run Maya), so I don’t think I can utilize

I need to create/open asset/shot scenes from an external app, like Shotgun.

So one option is to use the -command flag when launching maya, to run a mel command probably something like

subprocess.Popen(['C:/maya/maya.exe', '-command', "python('execfile(\"path_to_your_script\"')"])

Another would be to have a startup script that opens up the commandPort and then you can squirt commands through that to the running process.

has a pretty good example on how to set that up for sublime.

1 Like

I can execute python code directly (without saving it to a file), right? Did not work for me:

cmd = [maya_exe, '-hideConsole', '-command', "python('print 'OLA' ')"]

// Error: python('print 'OLA' '); // 
// Error: Line 1.8: Syntax error //

cmd = [maya_exe, '-hideConsole', '-command', "python(\"print 'OLA' \")"]

Mel basically has no idea about single quotes, so when passing in the python string you want it to eval, you have to wrap it in double quotes.

1 Like