Run maya from python shell

So, I have hundreds of May files to be run with one script. So I was thinking why I even have to worry about opening Maya, I would have to do it from the python shell (not the python shell in Maya, python shells in windows)

So the idea is this:

fileList = ["....my huge list of files...."]
for f in fileList:
    openMaya
    runMyAwesomeScript

      

I found this:

C:\Program Files\Autodesk\Maya201x\bin\mayapy.exe
maya.standalone.initialize()

      

And it looks like it is loading sth because I can see my scripts being loaded from custom paths. However, it does not start maya.exe launch.

Any help is appreciated as I have never done this kind of Maya python external stuff.

PS Using Maya 2015 and python 2.7.3

+3


source to share


1 answer


You are on the right track. Maya.standalone

runs headless, non-gui versions of Maya, so it's perfect for batch processing, but it's essentially a command line application. Apart from the lack of a GUI, this is the same as a normal session, so you will have the same python path and

You want to create a batch process so that it doesn't need any UI interactions (so, for example, you want you to save or export things in a way that doesn't create dialogs from the user),

If you just want maya with the command line, this will allow you to start a session interactively:

mayapy.exe -i -c "import maya.standalone; maya.standalone.initialize()"

      

If you're using a script instead, include import maya.standalone

both maya.standalone.initialize()

at the top and then all the work you want to do. Then run it from the command line like this:



mayapy.exe "path/to/script.py"

      

Presumably, you want to include a list of files to process in this script and just burn them one at a time. Something like that:

import maya.standalone
maya.standalone.initialize()
import maya.cmds as cmds
import traceback

files = ['path/to/file1.ma'. '/path/to/file2.ma'.....]

succeeded, failed = {}

for eachfile in files:
    cmds.file(eachfile, open=True, force=True)
    try:
        # real work goes here, this is dummy
        cmds.polyCube()  
        cmds.file(save=True)
        succeeded[eachfile] = True
    except:
        failed[eachfile] = traceback.format_exc()

print "Processed %i files" % len(files)
print "succeeded:"
for item in succeeded: 
       print "\t", item

print "failed:"
for item, reason in failed.items():
    print "\t", item
    print "\t", reason

      

which should perform some operation on a bunch of files and tell which ones will succeed and which ones don't work for whatever reason

+3


source







All Articles