I’m reviving a post from exactly 4 years ago ⌛️. It was severely flawed since it was one of my earliest post while I’m still learning my ways around Maya.
Standalone Module
1 | import maya.standalone |
Using mayapy
Interpreter
I strongly recommend using
mayapy
in order to stay consistent with the behaviour of GUI maya.
Prefer using mayapy
: Maya’s own python interpreter, this ensures
all the libraries and plugins are compatible, it also saves some trouble configuring
environments.
Using External Python Interpreter
When using external interpreter for whatever reason, we’ll need to check:
-
The python interpreter should match the maya python version
- e.g. (python27 for Maya 2018)
-
System environment variable will need to set up in order to find maya modules
- Otherwise, we may encounter the following:
1
ImportError: No module named maya.standalone
To configure the environment for our external interpreter:
1 | import sys |
Making Our First Standalone Script
After maya.standalone
has initialized, we can import maya.cmds
as usual.
Let’s add a test function to our standalone script.
1 | import maya.cmds as cmds |
This test function opens a maya file on my desktop and import a maya file and save out as another file.
Execution
Here I’m using mayapy
as example, we can of course using the external python
interpreter, but again, it takes extra steps.
Execute from Command Line
-
make sure to add
mayapy
to our system environment variable.- in system variable: add
C:\Program Files\Autodesk\Maya2018\bin
toPath
- in system variable: add
-
open command prompt, and enter
mayapy [directoryTo/firstStandalone.py]
Execute using subprocess
We can do it without going to command line, but by executing another .py
using subprocess
1 | import subprocess |
Here I’m removing the environment setup in firstStandlone.py
, and add a wrapper:
1 | import logging |
Standalone Script with Arguments
Sometimes, we would want to pass arguments to our standalone script
Using sys.argv
1 | import sys |
Example from Toadstorm Nerdblog:
1 | import subprocess |
Using argparser
In the commandline launch script, add argment:
1 | import subprocess |
And then in the wrapper, use the ArgumentParser()
1 | import argparse |
Bonus: Integrate UI Elements from Qt
It is easy to integrate UI in a standalone tool by creating a QApplication
instance,
and for Maya GUI, Maya has QApplication
instance already created.
but for maya standalone script, we need to make sure to create a QApplication
instance
before the standalone initialization:
1 | import sys |
Depending on our tool need, usually standalone script wouldn’t block the
main event loop, so we just exit when finished, therefore, no need to add sys.exit(app.exec_())
Bonus: Copying GUI Maya Environment
To get the exact environment as the GUI Maya version (this could be useful if there’s any startup scripts that modifies the environment)
In the maya session, parse out the environment and save it as a dictionary:
1 | import os |
And in the commandline setup .py, apply the environment from the dictionary:
1 | env_dict_copy_from_maya = {'TEST': 'THIS IS A TEST'} |
Reference
Xingyu Lei - Save Time and Boost Efficiency: Learn to Run Maya in Batch Mode
Stack Overflow - maya standalone with pyside2
Tech-Artist Org - Import maya.standalone problem
Stack Overflow - use external python script to open maya and run another script inside maya
Stack Overflow - How to use an external interpreter for Maya?