Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Get a list of soft selection weights in Autodesk Maya
import maya.cmds as cmds
import maya.OpenMaya as omo
# (Open Maya Old)
def soft_selection_weights():
''' create and return a list of the soft selection weights '''
#TODO: Would be nice to rewrite this using the new API. Low priority.
#TODO: Debug on multiple selections
# temporary hack. Turn off symmetry when reading MRichSelection until I learn to use symmetry.
# as far as my tests go, this maintains the symmetrical selection but reads it like a whole selection.
# otherwise, only one half will be reading by MRichSelection. How does getSymmetry() work?
symmetryOn = cmds.symmetricModelling(q=True, symmetry=True)
if symmetryOn:
cmds.symmetricModelling(e=True, symmetry=False)
selection = omo.MSelectionList()
softSelection = omo.MRichSelection()
dagPath = omo.MDagPath()
selection.getDagPath(0, dagPath)
component = omo.MObject()
geoIter = omo.MItGeometry(dagPath)
pointCount = geoIter.exactCount()
#TODO: MFloatArray and MDoubleArray had strange inconsistencies. But a list might be slow.
weightArray = [0.0] * pointCount
iter = omo.MItSelectionList(selection, omo.MFn.kMeshVertComponent)
#NOTE: since I commented out the while loop, this should just work on the first selected transform.
#while not iter.isDone():
iter.getDagPath(dagPath, component)
fnComp = omo.MFnSingleIndexedComponent(component)
if fnComp.hasWeights():
for i in range(fnComp.elementCount()):
element = fnComp.element(i)
weight = fnComp.weight(i).influence()
weightArray[element] = weight
# Put the symmetry back to the way it was.
cmds.symmetricModelling(e=True, symmetry=symmetryOn)
return weightArray
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.