# Maya Python API 2.0

## Contents

- Overview
- Using The New API
- Class Documentation

## 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:

    	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.

## Classes

The following classes have been implemented in the new API. Clicking on the class name will display its documentation. The class documentation focuses primarily on the parameter and return values of the methods. For additional information on the class you should refer to the documentation of the corresponding C++ class in the main Maya API documentation.

MAngle MArgDatabase MArgList MArgParser MArray MBoundingBox MCallbackIdArray MCallbackId MColorArray MColor MDagModifier MDagPathArray MDagPath MDGContext MDGModifier MDistance MDoubleArray MEulerRotation MFloatArray MFloatMatrix MFloatPointArray MFloatPoint | MFloatVectorArray MFloatVector MFnAttribute MFnBase MFnComponent MFnComponentListData MFnCompoundAttribute MFnDagNode MFnData MFnDependencyNode MFnDoubleArrayData MFnDoubleIndexedComponent MFnEnumAttribute MFnGenericAttribute MFnGeometryData MFn MFnIntArrayData MFnLightDataAttribute MFnMatrixAttribute MFnMatrixData MFnMeshData MFnMesh | MFnMessageAttribute MFnNumericAttribute MFnNumericData MFnNurbsCurveData MFnNurbsSurfaceData MFnPlugin MFnPointArrayData MFnSingleIndexedComponent MFnStringArrayData MFnStringData MFnTransform MFnTripleIndexedComponent MFnTypedAttribute MFnUInt64ArrayData MFnUnitAttribute MFnVectorArrayData MGlobal MIntArray MMatrix MMeshIsectAccelParams MMeshSmoothOptions MNodeClass | MObjectArray MObject MPlugArray MPlug MPointArray MPoint MPxCommand MQuaternion MRampAttribute MSelectionList MSpace MSyntax MTime MTransformationMatrix MTypeId MUint64Array MUintArray MVectorArray MVector MWeight

© 2011 Autodesk, Inc. All rights reserved.