在Python中运行来自maya的cmd.exe命令的列表 [英] Running list of cmd.exe commands from maya in Python

查看:1687
本文介绍了在Python中运行来自maya的cmd.exe命令的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写一个玛雅人python脚本批处理渲染一个场景到jpgs然后使用ffmpeg将它们变成一个mov。目前该脚本将一个命令字符串保存为一个bat文件,它工作正常,但我更喜欢只是从maya运行cmd.exe并执行命令列表,而不创建该bat首先。



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



1)运行maya render.exe&保存场景为jpegs



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.maffmpeg -y -r 25 -start_number 0100 -i C:\SCENE _ %% 04d.jpg C:\SCENE.mov

解决方案

pre> 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)
如果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)
如果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将被冻结。这可能是非常不方便的,特别是如果你有很多帧来渲染。



我建议你分开你的渲染和转换调用从你的主脚本,以避免这个



在您的主脚本中(

在那里你会调用你的 Popen()



subprocess.Popen 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

#存储参数变量
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)
如果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)
如果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)


I am writing a maya python script to batch render a scene into jpgs then use ffmpeg to turn them into a 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.

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:

1) Run maya render.exe & save scene as jpegs

2) ffmpeg jpgs to mov

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. 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.

You can do it this way:

In your main script (where you would have called your 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. You can pass arguments from your main script to render_and_convert.py (file paths, start/end frames, etc...)

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中运行来自maya的cmd.exe命令的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆