我试图从Maya场景中提取纹理,在Maya外部运行一个shell脚本,对这些脚本进行样式转换,然后,将生成的图像重新导入Maya。
我很难尝试以这种方式编写脚本,以使Maya暂停执行Python代码,直到关闭外壳并处理图像为止。我尝试使用子流程并跟踪它们的ID,以便尝试建立一个循环来检查该进程是否仍在运行,但是看起来这些子流程的作业ID仅在Maya关闭后才消失。 到目前为止,这就是我的代码。我要跟踪的部分是“ os.system()”执行。
import maya.cmds as cmds
import os,sys
import subprocess
# Set environment paths for the mayapy environment #
os.environ["PYTHONHOME"] = "/usr/bin/python2.7/"
os.environ["PYTHONPATH"] = "/usr/lib64/python2.7/"
projectDir = cmds.workspace( q=True, rd=True )
print projectDir
# Collecting textures #
sceneTextures = []
collectedTextures = []
texturePaths = []
textureArgs = ""
sceneTextures.append(cmds.ls(textures=True))
for i in range(0,len(sceneTextures[0])):
if "CNV" in sceneTextures[0][i]:
collectedTextures.append(sceneTextures[0][i])
print "The following textures have been collected:\n"
for i in range(0,len(collectedTextures)):
texturePaths.append(cmds.getAttr(collectedTextures[i]+'.fileTextureName'))
print collectedTextures[i]
print texturePaths[i]
textureArgs+= " " + texturePaths[i]
# This calls the shell script that processess the textures #
os.system("gnome-terminal -x "+projectDir +"StyleTransfer.sh " + projectDir + " " + str(textureArgs))
##### Process complete - Textures are being reimported #####
##### TODO : Check if the script finished processing the textures (terminal closed) - Reimport them and assign to the corresponding nodes.
编辑:
正如我所提到的,使用子进程并没有太大帮助,因为我无法获得有关打开的终端的任何信息:
process = subprocess.Popen(['gnome-terminal'],shell = True)
process_id = process.pid
content = commands.getoutput('ps -A | grep ' + str(process_id))
print content
# Any of these or manually closing the terminal
process.terminate()
process.kill()
os.kill(process.pid, signal.SIGKILL)
content = commands.getoutput('ps -A | grep ' + str(process_id))
print content
# The "content" variable will print exactly the same thing before and
after closing the terminals:
"20440 pts/2 00:00:00 gnome-terminal <defunct>"
我不确定还有其他选择,因此任何建议都会受到赞赏。
答案 0 :(得分:0)
在终端打开并开始执行外部.sh文件之后,os.system(..)
是否立即返回。通常,除非进程退出,否则os.system
才返回。它取决于您要通过此命令执行的shell命令。
如果要通过子流程模块进行操作。
import subprocess
# If you are in Linux environment this will help you in splitting the final command.
import shlex
shell_cmd = '....'
shell_cmd = shlex.split(shell_cmd)
process = subprocess.Popen(shell_cmd)
# Wait till the process exits
process.communicate()
if process.returncode != 0:
# Process exited with some error
return
#process completion successful
# Now do the rest of the job.
但是最重要的是,首先检查您尝试通过子进程运行的命令,该命令是否在执行后立即返回(就像打开终端并退出,而不是等待脚本执行并完成一样)