Maya Python API 2.0 Basic


#1

Hi, i m newbie to Python API. Below is old Python API code (To get object Attribute), So tell me how to do same thing in Python API 2.0 ??

import maya.OpenMaya as om

#1.Create Selection LIst
mSel = om.MSelectionList()
mSel.add('pCube1')
 
#2.Create MObject & MDagPath
mDag = om.MDagPath()
mObj = om.MObject() 

#3.Get Dependency Node & Dag Path
mSel.getDagPath(0,mDag)
mSel.getDependNode(0,mObj)

print mDag.fullPathName()
print mObj.apiTypeStr()

#4.MEsh Function Set
mFnMesh = om.MFnMesh(mDag)
print mFnMesh.fullPathName()

#5.Dependency Node Function Set
mFnDependNode = om.MFnDependencyNode(mObj)
print mFnDependNode.name()

#6.Get Connection Of Shape Node
mPlugArray = om.MPlugArray()
mFnMesh.getConnections(mPlugArray)

mPlugArray.length()
mPlugArray[0].name()
mPlugArray[1].name()

mPlugArray2 = om.MPlugArray()
mPlugArray[1].connectedTo(mPlugArray2,True, False)
mPlugArray2.length()

print mPlugArray2[0].name()

mObj2 = mPlugArray2[0].node()

mFnDependNode2 = om.MFnDependencyNode(mObj2)

print mFnDependNode2.name()

width = mFnDependNode2.findPlug('width')
height = mFnDependNode2.findPlug('height')

width.asInt()
height.asInt()

subWidth = mFnDependNode2.findPlug('subdivisionsWidth')
subHeight = mFnDependNode2.findPlug('subdivisionsHeight')

subWidth.setInt(20)
subHeight.setHeight


#2

It is almost the same with a few improvements:

 import maya.[b]api[/b].OpenMaya as om

The rest is mostly the same with the exception that you do not need to create objects for the use as arguments later. So instead of:

#2.Create MObject & MDagPath
mDag = om.MDagPath()
mObj = om.MObject() 
#3.Get Dependency Node & Dag Path
mSel.getDagPath(0, mDag)
mSel.getDependNode(0, mObj)

You can do this:

#3.Get Dependency Node & Dag Path
mDag = mSel.getDagPath(0)
mObj = mSel.getDependNode(0)