MSelectionList

 
 
 

MSelectionList provides you with methods to add and remove objects from the selection list, as well as walk through the objects on the list.

For example, the following simple plug-in prints the names of all selected DAG nodes. If you create geometry and then select it, this plug-in prints the name of each selected object.

Simple plug-in example

#include <maya/MSimple.h>
#include <maya/MGlobal.h>
#include <maya/MString.h>
#include <maya/MDagPath.h>
#include <maya/MFnDagNode.h>
#include <maya/MSelectionList.h>
#include <maya/MIOStream.h>
MStatus pickExample::doIt( const MArgList& )
{
    MDagPath node;
    MObject component;
    MSelectionList list;
    MFnDagNode nodeFn;
    MGlobal::getActiveSelectionList( list );
    for ( unsigned int index = 0; index < list.length(); index++ )
    {
        list.getDagPath( index, node, component );
        nodeFn.setObject( node );
        cout << nodeFn.name().asChar() << " is selected" << endl;
    }
    return MS::kSuccess;
}
DeclareSimpleCommand( pickExample, "Autodesk", "1.0" );

Walking through the list of selected objects is quite simple. If you create geometry and then select it, this plug-in prints the name of each selected object.

The setObject() method on MFnDagNode is inherited by all function sets from MFnBase and is used to set the object the function set will operate on. Usually this is done through the function set's constructor, but if the function set has already been created and you want to change the object the function set operates on, you use setObject(). This is far more efficient than creating and destroying function sets each time you need one.

Try selecting a few CVs and then calling this plug-in. Notice that you do not get the name of the CV output, but rather the name of the parent object (the curve, surface, or mesh). Also notice that the number of objects selected is not the same as the number of names printed.

The Maya selection architecture simplifies the selection of object components such as CVs. Rather than putting each component object (for instance, each CV) onto the selection list, the parent object is put on the list and the components are grouped together.

For example, if several CVs of nurbSphereShape1 were selected, the list.getDagPath() call above would return an MDagPath to nurbSphereShape1 and an MObject grouping all the selected CVs together. The MDagPath and the MObject could then be passed to an MItSurfaceCV iterator to examine the selected CVs.

As long as you continue to select components of only one object, the object appears on the selection list only once. However, if you pick some components of one object, and then some components of another object, and then more components of the first object, the first object would actually appear on the selection list twice. This is so that you can determine the order in which objects were selected.