Code Snippet: Get geometry index on deformer
I needed a way to get the geometry index on a deformer based on the deforming shape name. Rather than trying to trace the connections in the dependency graph, Maya’s API has a much easier and faster method. This is also better than using the deformer command for the information since the command does not support returning full path names for comparison. So I thought I would share it to anyone who may want to add it to their own collection of tools.
import maya.OpenMaya as OM
import maya.OpenMayaAnim as OMA
import maya.cmds as cmds
def getDeformerGeometryIndex(deformer, geometry):
Return the geometry index for the deformer for the given geometry.
If multiple shapes exist in the base geometry, the first one is used.
if not cmds.objectType(geometry, isa='shape'):
shapes = cmds.listRelatives(geometry, ni=True, s=True, f=True)
if not shapes:
raise TypeError("Geometry '%s' does not have a shape" % geometry)
# get the first shape and use it
geometry = shapes
selList = OM.MSelectionList()
oDeformer = OM.MObject()
oGeo = OM.MObject()
fnDeformer = OMA.MFnGeometryFilter(oDeformer)