Welcome to MSDN Blogs Sign in | Join | Help

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();
            }
        }            
    }
Published Thursday, August 21, 2008 11:10 PM by P.Gopalakrishna

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Displaying the currently selected components with MSelectionList

fantastic...

the code snippet is too usefull...very good..

regards

David

NVIDIA corp.

Thursday, October 16, 2008 1:51 PM by david_Nvidia

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker