4

So I have hundreds of maya files that have to be run with one script. So I was thinking why do I even have to bother opening maya, I should be able to do it from python shell (not the python shell in maya, python shell in windows)

So the idea is:

fileList = ["....my huge list of files...."]
for f in fileList:
    openMaya
    runMyAwesomeScript

I found this:

C:\Program Files\Autodesk\Maya201x\bin\mayapy.exe
maya.standalone.initialize()

And it looks like it loads sth, because I can see my scripts loading from custom paths. However it does not make the maya.exe run.

Any help is welcome since I never did this kind of maya python external things.

P.S. Using maya 2015 and python 2.7.3

Nathan Arthur
  • 8,287
  • 7
  • 55
  • 80
Jozef Plata
  • 350
  • 4
  • 11
  • *" However it does not make the maya.exe run"* - `maya.standalone.initialize()` initializes a "headless" Maya standalone. This seems to be exactly what you want? Or maybe you just want to run Maya in "batch mode"? – UnholySheep Jul 03 '17 at 13:07
  • Hm... I was expecting the GUI and viewport to popup. Hm... So I guess it works. Ok so now that I know how to run maya from C:\Program Files\Autodesk\Maya201x\bin\mayapy.exe How can I run this from windows python shell? Becasue the idea is, so that any user can just fire up the script to do all the job, and dont need to manually run C:\Program Files\Autodesk\Maya201x\bin\mayapy.exe – Jozef Plata Jul 03 '17 at 13:25
  • That is documented on the autodesk help page: https://knowledge.autodesk.com/support/maya/learn-explore/caas/CloudHelp/cloudhelp/2015/ENU/Maya/files/Python-Python-from-an-external-interpreter-htm.html – UnholySheep Jul 03 '17 at 13:36
  • There are some useful ideas for your issue: https://stackoverflow.com/questions/27437733/use-external-python-script-to-open-maya-and-run-another-script-inside-maya – Andy Jazz Jul 03 '17 at 18:57

1 Answers1

6

You are on the right track. Maya.standalone runs a headless, non-gui versions of Maya so it's ideal for batching, but it is essentially a command line app. Apart from lacking GUI it is the same as regular session, so you'll have the same python path and

You'll want to design your batch process so it doesn't need any UI interactions (so, for example, you want to make sure you are saving or exporting things in a way that does not throw dialogs at the user).

If you just want a commandline-only maya, this will let you run an session interactively:

mayapy.exe -i -c "import maya.standalone; maya.standalone.initialize()"

If you have a script to run instead, include import maya.standalone and maya.standalone.initialize() at the top and then whatever work you want to do. Then run it from the command line like this:

mayapy.exe "path/to/script.py"

Presumably you'd want to include a list of files to process in that script and have it just chew through them one at a time. Something like this:

import maya.standalone
maya.standalone.initialize()
import maya.cmds as cmds
import traceback

files = ['path/to/file1.ma'. '/path/to/file2.ma'.....]

succeeded, failed = {}

for eachfile in files:
    cmds.file(eachfile, open=True, force=True)
    try:
        # real work goes here, this is dummy
        cmds.polyCube()  
        cmds.file(save=True)
        succeeded[eachfile] = True
    except:
        failed[eachfile] = traceback.format_exc()

print "Processed %i files" % len(files)
print "succeeded:"
for item in succeeded: 
       print "\t", item

print "failed:"
for item, reason in failed.items():
    print "\t", item
    print "\t", reason

which should do some operation on a bunch of files and report which ones succeed and which fail for what reason

theodox
  • 12,028
  • 3
  • 23
  • 36
  • Thank you @theodox that helps a bit. However Im still confused. Because I want to run the whole thing from external python IDE (Im using PyCharm). I think I set up the PYTHONPATH environment variable properly. But have no idea how to set up MAYA_LOCATION environment variable? Is that a system variable, python or maya? Dunno Because I dont want to manually type in each time I need to run the script in the command line. I want to be able to run it from python IDE to test it out. – Jozef Plata Jul 04 '17 at 15:26
  • MAYA_LOCATION is set inside Mayapy when running. You will need to set pychharm to use Mayapy as an interpreter as well. As long as you run from mayapy you'll be in the same paths/environment you have in a regular Maya session – theodox Jul 04 '17 at 17:32