3Ducttapes(); Yvo von Berg | CG Toolsmith blog | C++, Python

Simple deformer example OpenMaya API

Was working in Maya the other day, and I spent some time looking for a simple example that I can use as a starting point for a deformer. I’m currently prototyping my Tycoon bend deformer in Maya as well since prototyping is a bit faster without compiling C++ :)

Get all MPoints of the mesh

a MPoint() is a Maya api wrapper around a point. Here we initialize an api mesh object so we can make use of the method .getPoints(). MFnSet docs MPoint docs

DAG path: directed acyclic graph (DAG), path (path to a node): docs

self.mFnSet = om.MFnMesh(dag_path)
# kObject = object / local space
# to get world space use: kWorld
verts =  self.mfn_set.getPoints(space=om.MSpace.kObject)

Set / Update the new points of the mesh

# update all verts
self.mfn_set.setPoints(new_points, space=om.MSpace.kObject)

Full code example:

class YvoDeformer(object):

    def __init__(
        self
        ):
        self.selection = None
        self.mfn_set = None
    

    def deform(self):
        self.selection = cmds.ls(sl=True)
        if not self.selection:
            return

        # make duplicate and assign as selection
        self.selection = cmds.duplicate(self.selection[0])[0]
        cmds.select(self.selection)

        selection_list = om.MSelectionList()
        selection_list.add(self.selection)
        dag_path = selection_list.getDagPath(0)

        # assemble an open maya mesh set object using the dag_path
        self.mfn_set = om.MFnMesh(dag_path)
        verts =  self.mfn_set.getPoints(space=om.MSpace.kObject)
        new_points = om.MPointArray()

        for v in verts:
            # move this vertex in the local x axis
            v.x += 30.0
            
            # assign the new point to the list
            new_points.append(v)

        # make sure we don't set an empty array
        if not new_points:
            return 

        # update all verts
        self.mfn_set.setPoints(new_points, space=om.MSpace.kObject)