The process for me has been a combination of looking at existing code (including C++ examples that ship with maya), and looking at the documentation. Eventually you’ll be able to figure out how to extract the values you want just by looking at the docs (for most cases at least).
This may be overly detailed and known to you, but I’m just going to break down how I look at the docs to decide what I need to use to get the data I want, and how to access it.
Looking at the docs for MRichSelection, under Public Member Functions you will find:
MStatus getSelection (MSelectionList &selection) const
Returns a copy of the non-symmetry component of the rich selection.
In this case it says it returns the component(s) of the selection, which sounds like what we want.
The “MStatus” on the left is the return value. MStatus just means it returns an indication of whether the function succeeded or not. This is not used in the python API at all, and can be ignored. For me, seeing MStatus means that the function returns nothing meaningful, and instead it operates on objects that you pass into the function (often by changing the data in the passed object).
The (MSelectionList &selection) tells us that it takes a reference to an MSelectionList. MSelectionLists store objects and components, which would make sense for storing the affected components of a soft select.
From that I know that I need to create an empty MSelectionList and pass that to MRichSelection.getSelection:
mSelList = om.MSelectionList()
mRichSel = om.MRichSelection()
om.MGlobal.getRichSelection(mRichSel)
mRichSel.getSelection(mSelList)
Now we have an MSelectionList containing the data we want. Extracting it takes more work. We need to get the MDagPath to the selected object, and an MObject of the selection components. If we look at the docs for MSelectionList, we find:
MStatus getDagPath (unsigned int index, MDagPath &dagPath, MObject &component=MObject::kNullObj) const
Get the dag path and component (may be NULL) of the given element of the selection list.
This is a pretty standard pattern for getting selected data:
mDagPath = om.MDagPath()
mObject = om.MObject()
mSelList.getDagPath(0, mDagPath, mObject)
mObject now stores the components we want. In order to access the actual component IDs and their soft-select weight, we need to use MFnSingleIndexedComponent (vertices are single-indexed components, ie mesh.vtx[4], whereas double-indexed would be nurbsSpline.cv[0][1])
Figuring out what to do with the MFnSingleIndexedComponent is a little more complicated. In the docs we see:
int element (int index, MStatus *ReturnStatus=NULL) const
Returns the specified element from the component.
The return type is “int”, so we know the element method returns the component ID of the given index in the component array.
MFnSingleIndexedComponent inherits from MFnComponent, and if we look at the docs for MFnComponent we see:
int elementCount (MStatus *ReturnStatus=NULL) const
Returns the number of elements that this component contains.
We can use this to determine how many elements are in this component list. We will loop through that range and pass each index to the “element()” call to get the component IDs in this array.
We also see:
MWeight weight (int index, MStatus *ReturnStatus=NULL) const
Return the weight data for a given element within this component.
This will return the (in our case) soft selection weight percentage for the given component.
All in all:
Loop through each element index and extract the ID and weight.
# Create an MFnSingleIndexedComponent using the components extracted in the previous step
mFnCmpt = om.MFnSingleIndexedComponent(mObject)
weights = {}
# For each element in the component array, get the ID of that component and the soft-select weight of that component.
for i in range(mFnCmpt.elementCount()):
weights[mFnCmpt.element(i)] = mFnCmpt.weight(i).influence()
That was a fairly complex and nested example of extracting data, but shows the process I would go through. Here’s a more complete example that accounts for potential errors: http://www.charactersetup.com/tutorial_softSelect.html