In addition to supporting Python within the Maya application, Maya also supports using the Python modules of Maya from a standalone Python interpreter. This is useful when doing batch processing, or when accessing the functionality of Maya from another application that uses Python (such as MotionBuilder or Composite).
We provide a standalone Python interpreter which is configured correctly for importing Maya functionality. Run the mayapy executable as follows:
where <version> denotes the Maya version; for example: Maya2016
You should specify an absolute path for MAYA_LOCATION instead of starting mayapy from within the \bin directory.
Once the Python interpreter is loaded, Maya must be loaded and initialized. To do so, type the following in the window:
import maya.standalone maya.standalone.initialize( name='python' )
These commands take a significant amount of time to execute as they are loading all of the Maya's libraries and initializing the scene.
The initialize routine takes only one parameter (name), and it is optional. The name parameter tells Maya what the name of the application is. The default value for name is python.
Once Maya is loaded and initialized, all Maya functionality should be available (maya.cmds, maya.OpenMaya, and so on). However, running Maya inside a Python interpreter is equivalent to running Maya in batch mode. That means that all normal Maya batch mode restrictions are in place, including the disabling of the UI commands.
Maya sets the PYTHONHOME environment variable within Maya but clears the variable outside of Maya (that is, when using an external interpreter).
try: import maya.standalone maya.standalone.initialize() except: pass
If you are in Maya's internal Python interpreter, the initialize() call will throw an exception, which will be safely caught.
It is possible to use Maya from a Python interpreter other than the one that ships with Maya. However, it is recommended that you use the same version number of Python as is included in Maya. To use another interpreter, it is necessary to set up the correct environment.
To set up the environment for a Python interpreter
On Mac OS X, the MAYA_LOCATION must be set to point into the application bundle (that is, ../files/Maya.app/Contents).
To set up the environment without the script
1. Add ../files/Maya.app/Contents/MacOS to the DYLD_LIBRARY_PATH so that Python can find Maya's shared libraries.
2. Add ../files/Maya.app/Contents/Frameworks to the DYLD_FRAMEWORK_PATH so that Python can find the frameworks that Maya depends upon.
3. Set MAYA_NO_BUNDLE_RESOURCES to some value. This tells Maya to look up resources via MAYA_LOCATION rather than by looking in the main bundle. (When Maya is run in batch this way, then the main bundle is not Maya's main bundle.)