Maya Python API 2.0 Reference

Maya Python API 2.0 Reference

Overview

The Maya Python API 2.0 is a new version of the Maya Python API which provides a more Pythonic workflow and improved performance. Both the new and old APIs can co-exist in the same script but objects from the two different APIs cannot be used interchangeably.

The Python API 2.0 has a number of advantages over the original API:

  • Array types are full Python sequences, including slice support.
  • Methods which take Maya array parameters will usually also take native Python sequences, such as arrays and tuples. Exceptions are made in some case for performance reasons. The outputs of methods are usually returned in their return values, not through their parameter lists. Exceptions are made in some cases for performance reasons.
  • Methods which return multiple values (e.g. MFnFluid.getResolution) return them as a tuple or list, eliminating the need for MScriptUtil.
  • Object attributes are preferred over rather than set/get methods. For example you can now write array.sizeIncrement=64.
  • There are more types of exceptions used when methods fail. Not everything is a RuntimeError, as was the case in the old API.
  • The new API is generally faster than the old. Up to three times faster in some cases.

Using the Python API 2.0

The new Python API modules are found in maya.api.

For example:

1 import maya.api.OpenMaya as om
2 import maya.api.OpenMayaAnim as omanim
3 import maya.api.OpenMayaRender as omrender

Module names are the same as in the old API with the exception that the proxy classes (i.e. those beginning with MPx) no longer have their own module but reside in the same modules as other related classes. This more closely resembles the C++ API.

Class names are the same as in the old Python API and the C++ API. Method names are mostly the same with some differences where it affects the workflow. Some methods which simply get or set values on an object have been replaced with Python object attributes.

New and old API classes can be used within the same script, but their objects are not interchangeable. Thus, you can do this:

1 import maya.api.OpenMaya as newOM
2 import maya.OpenMaya as oldOM
3 
4 newAttrObj = newOM.MObject()
5 oldNodeObj = oldOM.MObject()
6 ...
7 newAttrFn = newOM.MFnAttribute(newAttrObj)
8 oldNodeFn = oldOM.MFnDependencyNode(oldNodeObj)
9 
10 # OKAY: Print names from old and new function sets.
11 print("Attribute name is %s.%s" % (oldNodeFn.name(), newAttrFn.name))

but not this:

1 import maya.api.OpenMaya as newOM
2 import maya.OpenMaya as oldOM
3 
4 newAttrObj = newOM.MObject()
5 oldNodeObj = oldOM.MObject()
6 ...
7 # BAD: Passing an old API MObject to a new API method.
8 newPlug = newOM.MPlug(oldNodeObj, newAttrObj)

Given that the class and method names are mostly identical between the two APIs there is a lot of potential for confusion so it's best not to mix them if you can avoid it.

When writing a plugin that uses the new API, the plugin must define a variable called maya_useNewAPI so that Maya will know to pass it objects from the new API rather than the old one. For example,

1 maya_useNewAPI = True

We encourage users to provide feedback on this new Python API through the online Suggestion site available from the Maya Help Menu and through the Autodesk Developer Network.