2.0就是他们重新给Maya设计的Python API,支持Python的特性,优化运行速度,更加Pythonic.
这里我要介绍的是MSelectionList,要能正常运行下面的Python代码,你需要Maya2012 HotFix 1以上版本,om代表1.0的版本模块,newom是2.0.
MSelectionList是存储在MAYA中被选中的物体集,其实不是当前选中的物体也行.
获取当前选中的五体物体
import maya.OpenMaya as om # API 1.0 import maya.api.OpenMaya as newom # API 2.0 # API 1.0 selList = om.MSelectionList() om.MGlobal.getActiveSelectionList(selList) # API 2.0 mySel = newom.MGlobal.getActiveSelectionList()
MSelectionList selList; MGlobal::getActiveSelectionList(selList);
你可以看到1.0的用法跟C++的基本一样,如果你只想通过API来打印所选择的物体名称
nodes = [] selList.getSelectionStrings(nodes) nodes # Result: [u'pCube1'] # mySel.getSelectionStrings() # Result: (u'pCube1',) #
获取节点
node = om.MObject() selList.getDependNode(0, node) myNode = mySel.getDependNode(0)
可以看到API 2.0更加Pythonic,其它的方法也都差不多一样的用法,具体请看文档.