Skip to content

Instantly share code, notes, and snippets.

@chris-lesage
Last active May 3, 2023 22:08
Show Gist options
  • Save chris-lesage/17834ce88917b9446cae553bd8e23a4d to your computer and use it in GitHub Desktop.
Save chris-lesage/17834ce88917b9446cae553bd8e23a4d to your computer and use it in GitHub Desktop.
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()
omo.MGlobal.getRichSelection(softSelection)
#softSelection.getSymmetry(selection)
softSelection.getSelection(selection)
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
#iter.next()
# 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