gpt4 book ai didi

python - 在 Python 中运行来自 maya 的 cmd.exe 命令列表

转载 作者:行者123 更新时间:2023-12-04 23:28:09 59 4
gpt4 key购买 nike

我正在编写一个maya python脚本来将场景批量渲染为jpg,然后使用ffmpeg将它们变成mov。目前,该脚本将一串命令保存为 bat 文件,该文件运行良好,但我更愿意从 maya 运行 cmd.exe 并执行命令列表,而不先创建该 bat。

我一直在阅读“subprocess.popen()”,但我不知道如何让它遍历每一行,运行该命令,然后在完成后移动到下一个,即:

1) 运行 maya render.exe 并将场景另存为 jpeg

2) ffmpeg jpgs 到 mov

我已经缩短了目录,但本质上是这样的:

`C:\PROGRA~1\Autodesk\Maya2015\bin\Render.exe -r hw2 -s 100 -e 200 -of jpg -fnc 7 -x 1920 -y 1080 -rd 
"C:\RENDER"
"C:\SCENE.ma" ffmpeg -y -r 25 -start_number 0100 -i C:\SCENE_%%04d.jpg C:\SCENE.mov

我怎么能做到这一点?

谢谢!

最佳答案

first_process = subprocess.Popen(r'RenderCommand', stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True )
first_out,first_err = first_process.communicate()
first_exicode = first_process.returncode
print(first_out)
if str(first_exicode) != '0':
print(first_err)

second_process = subprocess.Popen(r'SecondCommand', stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True )
second_out,second_err = second_process.communicate()
second_exicode = second_process.returncode
print(second_out)
if str(second_exicode) != '0':
print(second_err)

third_process = subprocess.Popen(r'ConvertCommand', stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True )
third_out,third_err = third_process.communicate()
third_exicode = third_process.returncode
print(third_out)
if str(third_exicode) != '0':
print(third_err)

重要提示:

在执行每个 Popen() 期间,Maya 的 GUI 将被卡住。这可能真的很不方便,特别是如果您有很多帧要渲染。

我建议您将您的 Render 和 Convert 调用与您的主脚本分开以避免这种情况。

你可以这样做:

在你的主脚本中(你会调用你的 Popen() :
subprocess.Popen(r'mayapy.exe R:\paul\Trash\render_and_convert.py 50 100 150', False)
此命令不会卡住 Maya 的 GUI。您可以将主脚本中的参数传递给 render_and_convert.py (文件路径、开始/结束帧等...)

render_and_convert.py:
import sys
import subprocess
import maya.standalone as std
std.initialize(name='python')
import maya.cmds as cmds
import maya.mel as mel

# store the arguments in variables
first_argument = sys.argv[1] # =50
second_argument = sys.argv[2] # =100
third_argument = sys.argv[3] # =150

first_process = subprocess.Popen(r'RenderCommand ' + first_argument + ' ' + second_argument,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True )
first_out,first_err = first_process.communicate()
first_exicode = first_process.returncode
print(first_out)
if str(first_exicode) != '0':
print(first_err)

second_process = subprocess.Popen(r'SecondCommandWithoutArgs', stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True )
second_out,second_err = second_process.communicate()
second_exicode = second_process.returncode
print(second_out)
if str(second_exicode) != '0':
print(second_err)

third_process = subprocess.Popen(r'ConvertCommand ' + third_argument,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True )
third_out,third_err = third_process.communicate()
third_exicode = third_process.returncode
print(third_out)
if str(third_exicode) != '0':
print(third_err)

关于python - 在 Python 中运行来自 maya 的 cmd.exe 命令列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34295749/

59 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com