从python shell运行maya

Joz*_*ata 4 python maya

因此,我有数百个必须使用一个脚本运行的maya文件。所以我在想为什么我什至要打扰maya,我应该能够从python shell(而不是maya的python shell,windows的python shell)进行操作

因此,想法是:

fileList = ["....my huge list of files...."]
for f in fileList:
    openMaya
    runMyAwesomeScript
Run Code Online (Sandbox Code Playgroud)

我找到了这个:

C:\Program Files\Autodesk\Maya201x\bin\mayapy.exe
maya.standalone.initialize()
Run Code Online (Sandbox Code Playgroud)

看起来好像加载了某物,因为我可以看到我的脚本是从自定义路径加载的。但是,它不会使maya.exe运行。

欢迎提供任何帮助,因为我从未进行过此类Maya python外部操作。

PS使用Maya 2015和python 2.7.3

the*_*dox 5

您走在正确的轨道上。Maya.standalone运行无头的非GUI版本的Maya,因此非常适合批处理,但实际上它是命令行应用程序。除了缺少GUI外,它与常规会话相同,因此您将拥有相同的python路径和

您需要设计批处理过程,使其不需要任何UI交互(因此,例如,您要确保以不会向用户抛出对话框的方式保存或导出内容)。

如果只想使用仅命令行的Maya,则可以交互式运行会话:

mayapy.exe -i -c "import maya.standalone; maya.standalone.initialize()"
Run Code Online (Sandbox Code Playgroud)

如果要运行脚本import maya.standalone,则maya.standalone.initialize()在顶部包含和,然后再进行任何您想做的工作。然后从命令行运行它,如下所示:

mayapy.exe "path/to/script.py"
Run Code Online (Sandbox Code Playgroud)

大概您想在该脚本中包含要处理的文件列表,并使其一次只浏览一次。像这样:

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
Run Code Online (Sandbox Code Playgroud)

哪些应该对一堆文件进行一些操作,并报告哪些文件成功,哪些文件失败,原因是什么