Python in Maya

Python scripting can be used for many tasks in Maya, from running simple commands to developing plug-ins, and several different Maya-related libraries are available targeting different tasks. The following is a brief overview of the Python libraries shipped with Maya:

maya.cmds
This is a Python wrapper for MEL commands, and can be used in place of MEL. For more information, see Using Python and the Python Commands Reference.
pymel.core
Pymel is an alternative wrapper for MEL, developed by a third party. It is shipped with Maya, but not supported by Autodesk. It organizes commands differently, and takes an object-oriented approach compared to the procedural approach of maya.cmds. For more information, see Using PyMEL and the PyMel Reference.
maya.OpenMaya
This is a Python wrapper for the Maya C++ API, and referred to as Python API 1.0. It is suitable for developing plug-ins, and other tasks that require functionality not exposed by MEL. To understand the exposed classes, you should refer to the conceptual topics and the "C++ API Reference" in the Maya Developer Help. For more information, see "Maya Python API 1.0" in the Maya Developer Help.
maya.api.OpenMaya
This is a Python wrapper for the Maya C++ API, and referred to as Python API 2.0. This wrapper has better performance and is more "Pythonic" than the Python API 1.0. It is also a newer API, and is still under development, so not all classes exposed in 1.0 are available. For more information, see "Maya Python API 2.0" and "Maya Python API 2.0 Reference" in the Maya Developer Help.

Using maya.cmds

Autodesk Maya supports the use of Python-style scripting wherever you used to use MEL commands. The implementation of Python scripting in Maya provides the same access to native Maya commands as is provided through MEL. That is, all of the built-in Maya commands, such as sphere, ls, and so on, are accessible through Python.

Commands that are written as MEL scripts—which are actually MEL global procedures (procs)—are accessible using a call to access MEL (maya.mel.eval). For more information, see MEL/Python communication.

PySide version 1.2 is included with Maya. It is built with Python 2.7 and Maya Qt version 4.8.6.

Note: There are certain MEL built-ins that are not available in Python, though they generally have Python counterparts. These include math functions (abs, sin, cos, ...) and string functions (match, gmatch, tokenize...). (The set of MEL built-in functions can be considered the MEL runtime library.)

Python comes with a wide variety of standard modules that provide similar functionality. Refer to Python documentation for information on what is available and how to use these functions in your Python scripts.

Building PyQt for Maya 2016

When building PyQt for Autodesk Maya 2016, you must use the Microsoft 2012 Visual Studio Update 4 compiler. This is different from the behavior in Maya 2012.

For instructions on how to build a copy of the PyQt modules, see the PyQt section under autodesk.com/maya-docs.

Python Command Reference

There is a Python command reference similar to the MEL command reference. For details on all Python commands, refer to the Python command reference documentation in Maya Help.

You can access the help by selecting Help > Python Command Reference or open the Maya Help (Help > Maya Help), and when the Help appears, click CommandsPython at the bottom of the navigation frame.

Version

Maya installs Python with your Maya installation. Maya uses Python version 2.7.6 on all supported platforms. The standalone Python shell for Maya is named mayapy.exe on Windows and mayapy on Linux and Mac OS X.

External resources

For information about getting started with Python, including reference material and resources, see:

Initializing the Maya Environment in and for Python

Maya runs any Python commands in the userSetup.py file whenever it starts up. You can use this file to set up your working environment or execute commonly used Python commands such as importing the maya.cmds module.

The userSetup.py script is executed during the initialization and setup phase of Maya; therefore, only commands which set up your working environment and have no dependencies on Maya functionality can be successfully run in this script.

Note:

You can use maya.utils.executeDeferred() to delay code execution until after the Maya scene is initialized. For more information, see maya.utils.

  1. Create a file named userSetup.py in the following folder:
    • Windows: <drive>:\Documents and Settings\<username>\My Documents\maya\<Version>\scripts
    • Mac OS X: ~/Library/Preferences/Autodesk/maya/<version>/scripts
    • Linux: ~/maya/<version>/scripts
  2. In the userSetup.py file, type the commands you want Maya to run on start up; for example, import maya.cmds as mc.
    Note:

    Make sure you save the file with the right extension (.py).

Adding items to your Python path

To add items to your path in Python, do one of the following:

  1. Set PYTHONPATH in your Maya.env file, or in your environment before you run Maya
  2. Append to sys.path in your userSetup.py or other script once Maya is running.

Here is an example of appending sys.path

import sys sys.path.append( '/Users/jdoe/maya/Scripts' )
Note: userSetup.py must be located in the initial python path (sys.path) in order to be run automatically at startup. The default Python path can be added to by setting the environment variable PYTHONPATH.

MEL and Python importing

If you have a MEL script in your path, you do not need to source it before accessing any single global procedure it contains with the same name. However, Python requires you to import a script explicitly before accessing any classes or functions it contains; for example:

# This will access the function "bar" in the file "foo.py"
import foo
foo.bar()