简体   繁体   English

在Python中从Maya运行cmd.exe命令的列表

[英]Running list of cmd.exe commands from maya in Python

I am writing a maya python script to batch render a scene into jpgs then use ffmpeg to turn them into a mov. 我正在编写一个maya python脚本,以将场景批量渲染为jpg,然后使用ffmpeg将其变为mov。 Currently the script saves a string of commands as a bat file which works fine but I'd prefer to just run cmd.exe from maya and execute the list of commands without creating that bat first. 目前,该脚本将一串命令保存为bat文件,该文件可以正常工作,但我宁愿仅从maya运行cmd.exe并执行命令列表,而无需先创建该bat。

I've been reading about "subprocess.popen()" but I can't figure out how to get it to iterate through each line, run that command, then move onto the next one when done ie: 我一直在阅读有关“ subprocess.popen()”的信息,但我无法弄清楚如何使其遍历每一行,运行该命令,然后在完成后移至下一个命令,即:

1) Run maya render.exe & save scene as jpegs 1)运行maya render.exe并将场景另存为jpegs

2) ffmpeg jpgs to mov 2)将ffmpeg jpgs移动

I've shortened the directories but its essentially this: 我已经缩短了目录,但本质上是这样的:

`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

How would I be able to do this? 我将如何做到这一点?

Thanks! 谢谢!

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)

Important note: 重要的提示:

During the execution of each Popen(), Maya's GUI will be frozen. 在每个Popen()执行期间,Maya的GUI将被冻结。 This can be really inconvenient especially if you have a lot of frames to render. 这真的很不方便,特别是如果您要渲染很多帧。

I'd recommand you to separate your Render and Convert calls from your main script to avoid this. 我建议您将Render和Convert调用与主脚本分开,以避免发生这种情况。

You can do it this way: 您可以这样操作:

In your main script (where you would have called your Popen() : 在您的主脚本中(您将在其中调用Popen()

subprocess.Popen(r'mayapy.exe R:\\paul\\Trash\\render_and_convert.py 50 100 150', False)

This command will not freeze Maya's GUI. 该命令不会冻结Maya的GUI。 You can pass arguments from your main script to render_and_convert.py (file paths, start/end frames, etc...) 您可以将参数从主脚本传递到render_and_convert.py (文件路径,开始/结束帧等)。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM