How to wait for maya to finish loading after calling maya.exe with subprocess.Popen

How to wait for maya to finish loading after calling maya.exe with subprocess.Popen

landonjpginn
Participant Participant
2,541 Views
6 Replies
Message 1 of 7

How to wait for maya to finish loading after calling maya.exe with subprocess.Popen

landonjpginn
Participant
Participant

Hello, 
Im calling maya.exe with subprocess.Popen()
and i cant figure out how to wait for maya to finish loading before sending it commands. 
currently it either waits for maya to close to continue to the next line of code, 
or I have it open a socket and based on the stdout feedback i just check if "Device is available" is in the print out ( last readout has it in there..)

I know im stictching this together pretty sloppily, but Im trying to learn how to call maya outside of maya, but still be able to talk to it. 

If anyone has good resources how to accomplish this, i would appreciate the feedback.

Thanks

import shlex
import subprocess

cmd = r'''"C:/Program Files/Autodesk/Maya2018/bin/maya.exe" -noAutoloadPlugins -command "commandPort -n \":7333\" -stp \"python\";"'''
args = shlex.split(cmd)
print(args)
process = subprocess.Popen(args, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
    output = process.stdout.readline()
    if process.poll() is not None:
        break
    if output:
        print(output.strip())
        if "Device is available" in str(output.strip()):
            maya = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            maya.connect(('localhost', 7333))
            maya.send('import maya.cmds as cmds;cmds.file("{0}", o=True, f=True);'.format(asset_cln_ver).encode('utf-8'))
            # Actions that require maya UI and not mayapy.exe
            maya.send('cmds.file(rename="{0}");cmds.file(save=True,type="mayaAscii")'.format(asset_cln_top).encode('utf-8'))
            maya.close()
rc = process.poll()

 

0 Likes
2,542 Views
6 Replies
Replies (6)
Message 2 of 7

tdHendrix
Collaborator
Collaborator

Try to wrap the Maya cmds using evalDeferred and see if that works.

 

Here's the command:

https://download.autodesk.com/us/maya/2011help/CommandsPython/evalDeferred.html


Greg Hendrix - Senior Technical Animator
LinkedIn : Twitter
0 Likes
Message 3 of 7

negow
Advocate
Advocate

evalDeferred would probably never get called, since Maya would never end up in an idle state before being asked to shutdown.

 

What you should be able to do however, is shutdown Maya *from within Maya*. E.g. that command you're sending over, finish that command with e.g. `sys.exit(0)`.

0 Likes
Message 4 of 7

landonjpginn
Participant
Participant

Well im trying to send commands into maya like opening files, and running some other methods ive written. 
shutting it down can work, but it closes maya before it finishes loading, and well. i need maya open. 

evalDeferred didnt help really at all. And i tried that before.

What im kind of doing until i find a better solution is  running Popen(), and then popping upa tkinter window with a "connect" button which can let a user connect to maya from the tkinter UI once maya is done loading. 

however, for some reason, it allows me to make calls like cmds.polySphere() 
but not things like:

cmd = """
if "path/folder" in sys.path:
    sys.path.append("path.folder")

import module

module.func()
""".encode(utf-8)

proc.send(cmd)


Nothing happens at all.
which adds to the confusion. 

0 Likes
Message 5 of 7

negow
Advocate
Advocate

I think you misunderstood my suggestion (or I'm misunderstanding your intention).

 

Do not shut down Maya from the parent script, shut it down from the snippet you pass into Maya. That way, only once your snippet is finished and is actually calling `sys.exit(0)` will it actually close. Or don't close it, keep it open and keep sending commands. Close it with a separate snippet.

0 Likes
Message 6 of 7

tdHendrix
Collaborator
Collaborator

It's been a while since messing with subprocess, but here's a snippet of code of what I tried last time I was messing with it. Figured I'd post in case it helps you out in anyway.

 

import subprocess


def execute(command):
    process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    output = ''

    for line in iter(process.stdout.readline, ""):
        print line,
        output += line

    process.wait()
    exitCode = process.returncode

    if (exitCode == 0):
        return output
    else:
        raise Exception(command, exitCode, output)


execute(r'"C:\Program Files\Autodesk\Maya2020\bin\mayapy.exe" C:\temp\test.py')

 

Here's what's in test.py:

import maya.standalone
maya.standalone.initialize(name='python')
import maya.cmds as cmds

cmds.file('C:/temp/test.ma', open=True, force=True)
cmds.polyCube()
cmds.rotate(0, 45, 0)

 

 


Greg Hendrix - Senior Technical Animator
LinkedIn : Twitter
0 Likes
Message 7 of 7

mcw0
Advisor
Advisor

Not familiar with subprocess.  But it sounds like "maya -prompt" is what you want to be running.  This opens a Maya session that accepts commands from a commandline.

0 Likes