[Maya C++ API] Set skinning weight attributes

void set_skinning_weights(
        MObject skin_cluster_node,
        const std::vector< std::map<int/*joint id*/, float/*joint weight*/> >& weights)
{
    MStatus status;
    MFnDependencyNode deformer_node(skin_cluster_node, &status);
    mayaCheck(status);
    MPlug weight_list_plug = deformer_node.findPlug("weightList", true, &status);
    mayaCheck(status);
    for( unsigned i = 0; i < weight_list_plug.numElements(); i++ )// For each vertex
    {
        // weightList[i]
        MPlug ith_weights_plug = weight_list_plug.elementByPhysicalIndex( i );

        // weightList[i].weight
        MPlug plug_weights = ith_weights_plug.child(0); // access first compound child

        // First reset values to zero:
        int nb_weights = plug_weights.numElements();
        for( int j = 0; j < nb_weights; j++ ) { // for each joint
            MPlug weight_plug = plug_weights.elementByPhysicalIndex( j );
            // weightList[i].weight[j]
            mayaCheck( weight_plug.setValue( MObject() ) );
        }

        for(const std::pair<anim::bone::Id, float>& value : weights[i]) {
            MPlug weight_plug = plug_weights.elementByLogicalIndex( value.first );

            // weightList[i].weight[value.second]
            mayaCheck( weight_plug.setValue( value.second ) );
        }
    }
}

No comments

All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.
Spam bot question: