issueing commands to mayapy.exe

209 views
Skip to first unread message

Juan Cristóbal Quesada

unread,
Feb 21, 2018, 8:53:11 AM2/21/18
to python_in...@googlegroups.com
Hi,
i would like to run several maya scripts from the command line and i would like each script to be executed by the same mayapy.exe instance.

the intention is to run this command line from another python script via subprocess.Popen... so the desired behaviour is to have a single instance of the mayapy.exe subprocess (as a detached process) and issue different scripts to this.

More generically... is it possible to run different commands to the same process? something like:

p1 = subprocess.Popen([interpreter.exe,script1,arg1,arg2])

p2 = subprocess.Popen([interpreter.exe,script2,arg3,arg2])

and have script2 be ran by the same process as script1? (not just another instance?)


Juan Cristóbal Quesada

unread,
Feb 21, 2018, 8:55:16 AM2/21/18
to python_in...@googlegroups.com
guess what im asking is if there you can specify the process id for example to the Popen method and have it use the process if it already exists or create one if it doesnt

Marcus Ottosson

unread,
Feb 21, 2018, 8:58:39 AM2/21/18
to python_in...@googlegroups.com

Yes, when you import my_script, it is effectively “run”. So what you can do is launch 1 instance of mayapy, and pass it a single script that imports the others.

$ mayapy my_script.py

my_script.py

import other_script1
import other_script2

guess what im asking is if there you can specify the process id for example to the Popen method and have it use the process if it already exists or create one if it doesnt

This probably isn’t what you’re looking for. Yes, you can get the process ID from a Popen instance, but unless you devise an interprocess communication server/client between your processes, you haven’t got that many options left for telling the mayapy you find to run another script.


Justin Israel

unread,
Feb 24, 2018, 8:24:34 PM2/24/18
to python_in...@googlegroups.com
Just to expand on this answer a little bit, there are a few different options with varying complexity.

Your subprocess can open a commandPort, and your main process can issue commands to it via a socket connection.

The Maya subprocess can start reading from stdin and you can issue instructions to it by writing to the stdin of your Process object.

You can use some non-maya interprocess communication like jsonrpc, ZMQ, or some other RPC or message bus. But this is probably overkill of you don't already have them as available libraries.

If you have redis, Rabbitmq, or some other task queue already running, you could make it the broker for pushing in requests from the main process and having the subprocess read them out to execute.

CommandPort or talking to stdin of the subprocess are probably the easiest choices.


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCFwBwRq-rzjgu9hbg8G7BDWkXLNR5VGTAN55SzMjbX3Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Juan Cristóbal Quesada

unread,
Feb 25, 2018, 4:06:44 AM2/25/18
to python_in...@googlegroups.com

socket connection is the first thing i thought of, but that implies coding some socket listening logic in the child process, was hoping to avoid that.

im exploring some other possibilities like talking via pipes which is what i think you are talking when issuing commands to the stdin, or the other one ive seen is using shared memory between the two processes with some sort of datastructure like a queue.

Ive read some interprocess communication in Operating Systems CS subject but of all, the option im more familiar with is using sockets... and i think for what im trying to do is a bit overkill.

Thanks to both.

Reply all
Reply to author
Forward
0 new messages