sending commands to mayapy to run in the background, keeping current Maya session free

2,862 views
Skip to first unread message

sRheinfrank

unread,
May 9, 2009, 9:53:43 PM5/9/09
to python_inside_maya
Hi---I want to be able to capture arguments from a UI in a Maya
session and send them to Mayapy to run in the background to keep from
locking up the current session while waiting for the process to
finish---

Ideally, I could access mayapy through the Script Editor (through a
system call) and also pass it arguments and the right module to
process them and still be able to work in Maya while it's running in
the background... I figure using the maya.standalone module would be a
vital part of this, what I'm unclear on is just how to get it going
purely by running a command from within Maya....

Thanks!!
-Steve

Sylvain Berger

unread,
May 11, 2009, 1:53:20 PM5/11/09
to python_in...@googlegroups.com
I manage to do this... it is a bit complex to explain, so i'll try my best.

1. Create a command calling mayapy.exe -m <script to run> <arguments>
2. Create the script you are calling with mayapy.exe, in this script you can grab the arguments sent to the script using sys.argv
example:
import sys
# store the arguments in variables
argument1 = sys.argv[1]
argument2 = sys.argv[2]
etc.

3. call the mayapy.exe command using subprocess. Here is part of my script... Note that I am capturing the stdout and stderr of mayaPy so I can diagnose problem more easily... you don't have to capture them.

import subprocess
cmdAndArgs = 'mayapy.exe -m mayaPy.avol.batchExportInit '+objExportedScene+' '+avolObj.NodeName+' '+camExportedScene+' '+str(createPreviewRender)
tempFolder = os.path.dirname(objExportedScene)
# Create log file
sdtoutFile = r'%s\stdout.txt' %tempFolder
stderrFile = r'%s\sterr.txt' %tempFolder
outptr = file(sdtoutFile, 'w')
errptr = file(stderrFile, 'w')
# Call the subprocess using convenience method
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags = subprocess.STARTF_USESHOWWINDOW
retval = subprocess.call(cmdAndArgs, startupinfo=startupinfo, bufsize=0, executable=None, stdin=None, stdout=outptr, stderr=errptr)
# Close log handles
errptr.close()
outptr.close()


This works fine for me... From maya I am exporting an object, I start a maya in background using mayapy.exe, I cleanup the object, save it, etc.  In my case maya is loked because I am waiting for the stdout of the subprocess, but if you send the process and don't wait for it, maya should be free.

Hope this helps
--
They say, "Evil prevails when good men fail to act." What they ought to say is, "Evil prevails."
Nicolas Cage as Yuri Orlov in Lord of War.

Juan Pablo

unread,
Nov 17, 2016, 6:21:43 PM11/17/16
to Python Programming for Autodesk Maya, sylvain...@gmail.com
Hi I'm having a similar situation. In my case I import obj files and apply materials but running it with mayapy it can't find the shading group of each material which has the same name as the material ie: BlueGlass and BlueGlassSG.

mtherrell

unread,
Jun 12, 2017, 7:59:59 PM6/12/17
to Python Programming for Autodesk Maya, sylvain...@gmail.com
Sylvain, 

I would like to be able to get a maya standalone instance from within the Maya script editor, run a python function in that maya standalone instance, sending it arguments, and capture its return value in the maya script editor.

is there a way to return an instance of maya standalone in the script editor something like this:

import get_maya_inst as gmi
 
my_args = 'anything'
 
maya_inst = gmi.maya_inst()

maya_inst.import_module('my_module_name')

return_val = maya_inst.my_module_name.my_awesome_function(my_args)

maya_inst = None #maya_inst released / quit through garbage collection
 
print(return_val)

Otherwise,

Is there a way you could post a more generic and step by step demo of your example on how to achieve the original goals of sReinfrank's question?

Justin Israel

unread,
Jun 12, 2017, 9:26:54 PM6/12/17
to python_in...@googlegroups.com, sylvain...@gmail.com
On Tue, Jun 13, 2017 at 12:00 PM 'mtherrell' via Python Programming for Autodesk Maya <python_in...@googlegroups.com> wrote:
Sylvain, 

I would like to be able to get a maya standalone instance from within the Maya script editor, run a python function in that maya standalone instance, sending it arguments, and capture its return value in the maya script editor.

is there a way to return an instance of maya standalone in the script editor something like this:

import get_maya_inst as gmi
 
my_args = 'anything'
 
maya_inst = gmi.maya_inst()

maya_inst.import_module('my_module_name')

return_val = maya_inst.my_module_name.my_awesome_function(my_args)

maya_inst = None #maya_inst released / quit through garbage collection
 
print(return_val)

This can definitely be achieved if one wanted to write an abstraction over that previous example of using subprocess. The abstraction would launch the subprocess and provide a higher-level interface for running arbitrary python commands that get communicated to the running process.

 
--
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/90f30d03-b0cc-4bbc-9092-8d5a9b7b6af6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages