Chris Sprance

Character Technical Director

Modo Select By Weights

I wrote a quick modo script because I had a need to select some verts if they had weights within a specific range.

select_by_weight.py
import lx
import lxu
import modo
import sys


def get_selected_weight_maps():
    """
    Get the selected weight maps from the scene
    :return: List of selected weight maps
    """
    scene = modo.Scene()
    vmaps = []
    v_map_indices = lx.eval('query layerservice vmaps ? all')

    for index in v_map_indices:
        vmap_type = lx.eval('query layerservice vmap.type ? %s' % str(index))
        vmap_sel = lx.eval('query layerservice vmap.selected ? %s' % str(index))
        vmap_name = lx.eval('query layerservice vmap.name ? %s' % str(index))

        if vmap_type == 'weight' and vmap_sel:
            vmaps.append(vmap_name)

    return [
        w
        for w in sum(
            [
                list(item.geometry.vmaps.weightMaps)
                for item in scene.selectedByType(lx.symbol.sTYPE_MESH)
            ],
            [],
        )
        if w.name in vmaps
    ]


def select_by_weight(min_t, max_t):
    """
    With a min and max value select all the verts where the weights are within the range
    :param min_t: The min value to select
    :param max_t: The max value to select
    :return: None This function has side effects only of selecting all verts within the range of min_t, max_t
    """
    scene = modo.Scene()
    # get the selected weights
    weights = get_selected_weight_maps()
    # for each weight map find all the weights over the threshold
    for weight_map in weights:
        for vert in weight_map._geometry.vertices:
            (weight,) = weight_map[vert.index]
            if min_t <= weight <= max_t:
                vert.select()


select_by_weight(0.9, 1.0)