简体   繁体   中英

Continue Maya Python Script After Shell Script Finished Running

I am trying to extract the textures from a Maya scene, run a shell script outside of Maya that does style transfer on those and after that, the resulted images should be imported back in Maya.

I have a hard time trying to write the script in such way that Maya suspends the execution of the Python code until the shell is closed and the images are processed. I tried to use subprocesses and to track their IDs so I can try to make a loop which checks if the process is still running, but it looks like the job IDs of those subprocesses only dissapear after Maya closes. This is how my code looks so far. The part that I am trying to track is that "os.system()" execution.

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.

EDIT :

As I have mentioned, using the subprocesses did not help too much as I couldn't get any information about the terminals that were opened:

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

I am not sure what other options there may be so any suggestion would be really appreciated.

Does os.system(..) is returning immediately after the terminal opens and starts executing your external .sh file. Usually os.system does not return until unless the process exits. Its depend upon the shell command you are trying to execute through this.

If you want to do it through subprocess module.

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.

But most importantly, check first if the command your are trying to run through subprocess, is returning immediately after the execution(Like just opening the terminal and exiting, not waiting for the script to execute and completed)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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