run maya from python shell

2019-02-10 17:36发布

问题:

So I have hundreds of maya files that have to be run with one script. So I was thinking why do I even have to bother opening maya, I should be able to do it from python shell (not the python shell in maya, python shell in windows)

So the idea is:

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 loads sth, because I can see my scripts loading from custom paths. However it does not make the maya.exe run.

Any help is welcome since I never did this kind of maya python external things.

P.S. Using maya 2015 and python 2.7.3

回答1:

You are on the right track. Maya.standalone runs a headless, non-gui versions of Maya so it's ideal for batching, but it is essentially a command line app. Apart from lacking GUI it is the same as regular session, so you'll have the same python path and

You'll want to design your batch process so it doesn't need any UI interactions (so, for example, you want to make sure you are saving or exporting things in a way that does not throw dialogs at the user).

If you just want a commandline-only maya, this will let you run an session interactively:

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

If you have a script to run instead, include import maya.standalone and maya.standalone.initialize() at the top and then whatever work you want to do. Then run it from the command line like this:

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

Presumably you'd want to include a list of files to process in that script and have it just chew through them one at a time. Something like this:

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 do some operation on a bunch of files and report which ones succeed and which fail for what reason



标签: python maya