run maya from python shell

走远了吗. 提交于 2019-12-18 17:21:52

问题


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



来源:https://stackoverflow.com/questions/44886329/run-maya-from-python-shell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!