Using the Python API 2.0

Using the Python API 2.0

The new Python API modules are found in maya.api. For example:

import maya.api.OpenMaya as om

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:

import maya.api.OpenMaya as newOM
import maya.OpenMaya as oldOM

newAttrObj = newOM.MObject()
oldNodeObj = oldOM.MObject()

#...

newAttrFn = newOM.MFnAttribute(newAttrObj)
oldNodeFn = oldOM.MFnDependencyNode(oldNodeObj)
	
# OKAY: Print names from old and new function sets.
print("Attribute name is %s.%s" % (oldNodeFn.name(), newAttrFn.name))

but not this:

import maya.api.OpenMaya as newOM
import maya.OpenMaya as oldOM

newAttrObj = newOM.MObject()
oldNodeObj = oldOM.MObject()

#...

# BAD: Passing an old API MObject to a new API method.
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 which uses the new API, the plugin must define a function called maya_useNewAPI so that Maya will know to pass it objects from the new API rather than the old one. E.g:

def maya_useNewAPI():
    pass

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.

For information on the Python 2.0 Class list, consult the Maya Python API 2.0 Reference.