在Shell脚本完成运行后继续Maya Python脚本

2024-10-04 05:23:19 发布

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

我试图从Maya场景中提取纹理,在Maya外部运行一个shell脚本,在这些脚本上进行样式转换,然后,生成的图像应导入回Maya中。你知道吗

我很难尝试以这样的方式编写脚本:Maya暂停Python代码的执行,直到shell关闭并处理图像。我尝试使用子进程并跟踪它们的ID,以便可以尝试创建一个循环来检查进程是否仍在运行,但看起来这些子进程的作业ID只有在Maya关闭后才会消失。 到目前为止,我的代码就是这样的。我要追踪的部分是”操作系统()“执行。你知道吗

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

我不知道还有什么其他的选择,所以任何建议都将不胜感激。你知道吗


Tags: thetrueoscontentshellprocessterminalprint
1条回答
网友
1楼 · 发布于 2024-10-04 05:23:19

Doesos.system(..)在终端打开并开始执行外部.sh文件后立即返回。通常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.

但最重要的是,首先检查您要在子进程中运行的命令是否在执行后立即返回(比如只打开终端并退出,而不是等待脚本执行并完成)

相关问题 更多 >