[Maya C++ API] Get selected vertices of a mesh

MSelectionList selection_list;
MGlobal::getActiveSelectionList(selection_list);
MItSelectionList iter(selection_list);
for ( ; !iter.isDone(); iter.next()) // For each selected object
{
    MObject component;
    MDagPath item;
    mayaCheck( iter.getDagPath(item, component) );
    
    MGlobal::displayInfo( item.fullPathName() );
    
    if( component.isNull() )
        continue; // No vertices selected.
    
    MStatus status;
    MItMeshVertex vert_it(item, component, &status);
    if( status == MS::kFailure)
        continue; // the component is not a vertex (perhaps face, edge etc.)
    
    for ( ; !vert_it.isDone() ; vert_it.next() )
    {
        int af = vert_it.index(&status);
        mayaCheck(status);
        MGlobal::displayInfo(MString(" index\n")  + af );
    }
}

Get selected vertices of a specific shape:

bool check_selected_vertex_of(const MDagPath& path, MObject& component)
{
    MStatus status;
    MSelectionList selection_list;
    mayaCheck( MGlobal::getActiveSelectionList(selection_list) );
    MItSelectionList iter(selection_list, MFn::kInvalid, &status);
    mayaCheck(status);
    for ( ; !iter.isDone(); iter.next())
    {
        MDagPath curr_selection;
        mayaCheck( iter.getDagPath( curr_selection, component ) );
        if( curr_selection == path &&
            !component.isNull() &&
            component.apiType() == MFn::kMeshVertComponent)
        {
            return true;
        }
    }
    return false;
}

int get_nb_vertices(MObject mesh_shape)
{
    MStatus status;
    MItMeshVertex mesh_it(mesh_shape, &status);
    mayaCheck(status);
    int nb_verts = mesh_it.count(&status);
    mayaCheck(status);
    return nb_verts;
}

std::vector<int> get_selected_vertices(MObject mesh_shape)
{
    MStatus status;
    std::vector<int> vertex_list;
    MObject component;
    MDagPath path = get_MDagPath(mesh_shape);
    if( check_selected_vertex_of(path, component) )
    {
        vertex_list.reserve( get_nb_vertices(mesh_shape) );
        MItMeshVertex vert_it(path, component, &status);
        mayaCheck( status );
        int i = 0;
        for ( ; !vert_it.isDone() ; vert_it.next() ){
            int vidx = vert_it.index(&status);
            mayaCheck(status);
            vertex_list.push_back( vidx );
            ++i;
        }
    }
    return vertex_list;
}

more here

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: