Tyler Thornock
Technical Animator
Home Tutorials Tools Rigs About/Resume
Maya - Query Soft Selection

Opacity, everything can benefit from falloff and blending! Soft select and reflection are two extremely useful features that should be harnessed by any script you can. Imagine, maya’s copySkinWeights using the soft selection to blend weights, or perhaps when adding, scaling, or smoothing weights to a skin influence or painted attribute, or when matching points on one mesh to another… the list could go on. The ability to base the falloff distance on Volume or Surface can be extremely useful and I have integrated this into numerous tools.

So, blah blah how do you get the opacity per vert and reflection? Enter, MRichSelection and its functions getSelection and getSymmetry which return a MSelectionList, but with a twist. The MObject representing the components actually contains the opacity value (somewhat hidden) for that component. NOTE: this example does not currently include support for the Symmetry option.

So here is a working example for querying soft selection opacities:

from maya import cmds
from maya.api import OpenMaya


def get_soft_selection(opacity_mult=1.0):
    all_dags, all_comps, all_opacities = [], [], []

    # if soft select isn't on, return
    if not cmds.softSelect(query=True, sse=True):
        return all_dags, all_comps, all_opacities

    try:
        # get currently active soft selection
        rich_sel = OpenMaya.MGlobal.getRichSelection()
    except:
        raise Exception('Error getting soft selection.')

    rich_sel_list = rich_sel.getSelection()
    sel_count = rich_sel_list.length()

    for x in range(sel_count):
        try:
            shape_dag, shape_comp = rich_sel_list.getComponent(x)
        except TypeError:
            # nodes like multiplyDivides will error
            continue
        if shape_comp.isNull():
            continue

        comp_opacities = {}
        comp_fn = OpenMaya.MFnSingleIndexedComponent(shape_comp)
        try:
            # get the secret hidden opacity value for each component (vert, cv, etc)
            comp_ids = comp_fn.getElements()
            for i, comp_id in enumerate(comp_ids):
                weight = comp_fn.weight(i)
                comp_opacities[comp_id] = weight.influence * opacity_mult
        except:
            log.warning('Soft selection appears invalid, skipping for shape "%s".' % shape_dag.partialPathName())

        all_dags.append(shape_dag)
        all_comps.append(shape_comp)
        all_opacities.append(comp_opacities)

    return all_dags, all_comps, all_opacities

all_dags, all_comps, all_opacities = get_soft_selection()

Now that you have the opacities the hard part is converting scripts and tools that weren’t designed for blending… but once you start supporting it you’ll love it!