簡體   English   中英

Shell腳本完成運行后,繼續Maya Python腳本

[英]Continue Maya Python Script After Shell Script Finished Running

我試圖從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>"

我不確定還有什么其他選擇,因此任何建議將不勝感激。

終端打開並開始執行外部.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.

但最重要的是,首先檢查您嘗試通過子流程運行的命令是否在執行后立即返回(就像打開終端並退出,而不是等待腳本執行並完成一樣)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM