1

I need to conform some maya scenes we receive from a client to make them compatible to our pipeline. I'd like to batch that action, obviously, and I'm asked to launch the process from within Maya.
I've tried two methods already (quite similar to each other), which both work, but the problem is that the Maya GUI freezes until the process is complete. I'd like for the process to be completely transparent for the user so that they can keep workind, and only a message when it's done.
Here's what I tried and found until now:
This tutorial here : http://www.toadstorm.com/blog/?p=136 led me to write this and save it:

filename = sys.argv[1]

def createSphere(filename):
    std.initialize(name='python')
    try:
        mc.file(filename, open=True, pmt=False, force=True)
        sphere = mc.polySphere() [0]
        mc.file(save=True, force=True)
        sys.stdout.write(sphere)
    except Exception, e:
        sys.stderr.write(str(e))
        sys.exit(-1)
    if float(mc.about(v=True)) >= 2016.0:
        std.uninitialize()

createSphere(filename)

Then to call it from within maya that way:

mayapyPath = 'C:/Program Files/Autodesk/Maya2016/bin/mayapy.exe'
scriptPath = 'P:/WG_MAYA_Users/lbouet/scripts/createSphere.py'
filenames = ['file1', 'file2', 'file3', 'file4']

def massCreateSphere(filenames):
    for filename in filenames:
        maya = subprocess.Popen(mayapyPath+' '+scriptPath+' '+filename,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        out,err = maya.communicate()
        exitcode = maya.returncode
        if str(exitcode) != '0':
            print(err)
            print 'error opening file: %s' % (filename)
        else:
            print 'added sphere %s to %s' % (out,filename)

massCreateSphere(filenames)

It works fine, but like I said, freezes Maya GUI until the process is over. And it's just for creating a sphere, so not nearly close to all the actions I'll actually have to perform on the scenes.
I've also tried to run the first script via a .bat file calling mayabatch and running the script, same issue.
I found this post (Running list of cmd.exe commands from maya in Python) who seems to be exactly what I'm looking for, but I can't see how to adapt it to my situation ?
From what I understand the issue might come from calling Popen in a loop (i.e. multiple times), but I really can't see how to do otherwise... I'm thinking maybe saving the second script somewhere on disk too and calling that one from Maya ?

Community
  • 1
  • 1

1 Answers1

0

In this case subprocess.communicate() will block until the child process is done, so it is not going to fix your problem on its own.

If you just want to kick off the processes and not wait for them to complete -- 'fire and forget' style -- you can just use threads, starting off a new thread for each process. However you'll have to be very careful about reporting back to the user -- if you try to touch the Maya scene or GUI from an outside thread you'll get mysterious, undebuggable errors. print() is usually ok but maya.cmds() is not. If you're only printing messages you can probably get away with maya.utils.executeDeferred() which is discussed in this question and in the docs.

Community
  • 1
  • 1
theodox
  • 12,028
  • 3
  • 23
  • 36
  • Yeah, that's what I realized. When I launch it to treat various files, without the `subprocess.communicate()` it opened as many consoles as needed, did the job, printed the output on the console and closed as quick as it opened. By saying `maya.cmds` is not okay, you mean you don't recommend to use that method to do some changes on a maya scene ? Thanks for the links, I'll go read them right now. – tic-tac-orange Mar 23 '16 at 09:21
  • Basically, you should not try to issue anything from `maya.cmds` from a thread other than the main one unless it's being wrapped in `maya.utils.executeDeferred`. This is a bit of an oversimplification, but it's a good rule of thumb. You can still use `cmds` in your _other_ maya instance the usual way -but the in the one the user sees, don't touch cmds from a thread – theodox Mar 23 '16 at 15:51
  • Thanks for your replies. I have to admit that english not being my primary language plus learning python by myself make some concepts still quite difficult to grasp but I'll keep digging. I think for now I'll just keep going with the subprocess in the main thread because I do need some outputs. – tic-tac-orange Mar 25 '16 at 09:42