从python sh运行maya

2024-06-28 20:29:22 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我有成百上千的maya文件必须用一个脚本运行。所以我在想为什么我还要费心打开maya,我应该可以从python shell(而不是maya中的python shell,windows中的python shell)

所以我们的想法是:

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

我发现了这个:

^{pr2}$

它看起来像是在加载某个东西,因为我可以看到我的脚本从自定义路径加载。但是它不能使maya.exe快跑。在

任何帮助都是欢迎的,因为我从来没有做过这种maya python外部的事情。在

p.S.使用maya 2015和python 2.7.3


Tags: 文件ofin脚本formywindows费心
1条回答
网友
1楼 · 发布于 2024-06-28 20:29:22

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

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

如果只需要命令行maya,则可以交互式运行会话:

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

如果要运行脚本,请在顶部包含import maya.standalone和{},然后再执行任何您想执行的工作。然后从命令行运行它,如下所示:

^{pr2}$

大概你想在脚本中包含一个要处理的文件列表,然后让它一次一个地咀嚼它们。像这样:

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

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

相关问题 更多 >