Python from an external interpreter
 
 
 

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.

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' )

Note

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.

Note

Maya sets the PYTHONHOME environment variable within Maya but clears the variable outside of Maya (that is, when using an external interpreter).

TipTo find whether you are in Maya's internal Python interpreter, you can execute the following code block:
		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.

Using a different Python interpreter

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

  1. Add the site-packages directory of Maya to your PYTHONPATH environment variable, or add it to sys.path inside of Python once the interpreter is initialized (but before Maya is initialized). This directory is:
    • Windows: ../Python/Lib/site-packages
    • Mac OS X: ../files/Maya.app/Contents/Frameworks/Python.framework/Versions/Current/lib/python2.4/site-packages
    • Linux: ../files/lib/python2.4/site-packages
  2. Set the MAYA_LOCATION environment variable to point to Maya's install location, so that Maya can find its resources.

    On Mac OS X, the MAYA_LOCATION must be set to point into the application bundle (that is, ../files/Maya.app/Contents).

  3. (Linux) Add the lib directory of the Maya distribution to the LD_LIBRARY_PATH so that Python can find the shared libraries when importing Maya.
    TipOn Mac OS X, the mayapy executable is a shell script which can be copied and modified to use a different Python installation.

    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.)