Displaying the currently selected components with MSelectionList

The MGlobal::getActiveSelectionList() offers a convenient way of accessing the currently selected objects in the view. However, if you wish to find the selected components then you need to use the MSelectionList's getDagPath() method. It returns the dagPath of the seleted object and
also the selected components of it.
Once we have the dagPath of the selected objects and its selected components, we can use the
corresponding MIt classes to iterate over the individual elements of the component. For example, the below code lists the CVs of the currently selected NURBS surface for the selected components on it.

     MSelectionList selList;

    MGlobal::getActiveSelectionList(selList);

    cout << "Current Selection List has " << selList.length() << " objects" << endl;      

    MDagPath dagPath;
    MObject  selComponent;
    MFnDagNode nodeFn;

    for(unsigned int i=0; i < selList.length(); ++i)
    {
        // Get the selected Component
        selList.getDagPath(i, dagPath, selComponent);
        
        nodeFn.setObject(dagPath);
        
        // Display the name of the Object selected
        cout << nodeFn.name().asChar();
        
        // Component could be NULL if no individual components are selected
        if(selComponent.isNull()) cout << "Component is NULL !!" << endl;

        // Use MItCurveCV or MItMeshVertex or MItMeshPolygon etc... based on the type of Object.
        // You can use nodeFn::hasFn() to check if the selected object is of some type
        MStatus  stat;
        MItSurfaceCV itCV(dagPath, selComponent, true, &stat);

        if(stat == MStatus::kSuccess)
        {
            while(!itCV.isDone())
            {
                MPoint pt = itCV.position();
                cout << "{" << pt.x << "," << pt.y <<"," << pt.z << "}" << endl;
                itCV.next();
            }
        }            
    }

Comments

  • Anonymous
    October 16, 2008
    fantastic... the code snippet is too usefull...very good.. regards David NVIDIA corp.