MSelectionList - Selection within the Maya API

 

   

 

 

 

 

Selecting Nodes By Name

Selecting objects within the Maya API is a relatively simple thing to achieve. MGlobal contains a number of static functions that allow us to query and set the currently selected items.

The enum MGlobal::ListAdjustment defines how the selection will work. The following are the valid enum values which can be provided to most selection functions.

MGlobal::kReplaceList
MGlobal::kXORWithList
MGlobal::kAddToList
MGlobal::kRemoveFromList

Probably the simplest method of selecting objects in by is to use the MGlobal::selectByName() function.

Maya uses the MSelectionList class to manipulate the current (Active in maya speak) selection list. As a basic example, to retrieve the currently selected objects, we simply need to query the Active selection list via MGlobal.

 

 

 


 // select "pCube1" and replace the current selection list
 MGlobal::selectByName("pCube1",MGlobal::kReplaceList);

 // add "pCubeShape1" to the current selection list
 MGlobal::selectByName("pCubeShape1",MGlobal::kAddToList);

 

 

 

Selecting MObject's

In addition, we can also use MGlobal::select() to select nodes from their MObjects. ie,

 

 

 


 /// \brief	this function simply selects all nodes in the scene of the specified type
 /// \param	type	-	the maya type of the nodes to select
 ///
 void SelectItems(MFn::Type type) {

	// iterate over all nodes of the specified type
	MItDependencyNodes it(type);
	while(!it.isDone())
	{
		// get the object the iterator is referencing
		MObject obj = it.item();
		
		// select the node
		MGlobal::select( obj, MGlobal::kAddToList );
	
		// move to next item
		it.next();
	}
 }

 

 

 

Retrieving the Active Selection List

Maya uses the MSelectionList class to manipulate the current (Active in maya speak) selection list. We can query what items are currently selected by using the function MGlobal::getActiveSelectionList(). This will simply provide us with an MSelectionList which we can traverse....

 

 

 


 // get a list of the currently selected items 
 MSelectionList selected;
 MGlobal::getActiveSelectionList(selected);

 // iterate through the list of items returned
 for( int i=0; i<selected.length(); ++i )
 {
	MObject obj;
	
	// returns the i'th selected dependency node
	selected.getDependNode(i,obj);
	
	// Attach a function set to the selected object
	MFnDependencyNode fn(obj);

	// write the object name to the script editor
	MGlobal::displayInfo( fn.name().asChar() );
 }

 

 

 

So then, selection may all seem fairly straight forward, however there is an unfortunate subtlety. Within maya, when you select a mesh, you are infact selecting it's transform.

The problem we have then is that we are unlikely to retrieve say a selection list containing transforms. Therefore, we should check the children of any selected transforms to find any shape objects we are expecting to be selected. ie,

 

 

 


 /// \brief	This function is a simple utility function that retrieves the selected 
 ///		objects of the requested type. 
 /// \param	objects	-	the returned list of objects
 /// \param	type	-	the type of objects you want returned
 ///
 void GetSelected(MObjectArray& objects,MFn::Type type) {

	 // get the current selection list from maya
	 MSelectionList selected;
	 MGlobal::getActiveSelectionList(selected);

	 // iterate through all selected items
	 for( int i=0; i<selected.length(); ++i )
	 {
		MObject obj;
		
		// returns the i'th selected dependency node
		selected.getDependNode(i,obj);

		// if the selected object is of the type we are looking for
		if( obj.hasFn(type) ) {
			objects.append(obj);
		}
		else
		// if the selected object is a transform, check it's kids
		if( obj.hasFn(MFn::kTransform) ) {
		
			MFnTransform fn(obj);
			
			// loop through each child of the transform
			for(int j=0;j< fn.childCount();++j)
			{
				// retrieve the j'th child of the transform node
				MObject child = fn.child(j);
			
				// if the child is of the type we are looking for,
				// append it to the list
				if( child.hasFn(type) )
					objects.append(child);
			}
		}
	}
 }

 

 

 

Setting the Active Selection List

In addition to being able to select individual items, we can also construct a list of items which we can select in one go. For this we simply use the MGlobal::setActiveSelectionList() function.

 

 

 


 /// \brief	this function simply selects all nodes in the scene of the specified type
 /// \param	type	-	the maya type of the nodes to select
 ///
 void SelectItems(MFn::Type type) {

	// get the current selection list from maya
	MSelectionList selected;

	MItDependencyNodes it(type);
	while(!it.isDone())
	{
		selected.add( it.item() );
	
		it.next();
	}

	// set the currently selected items
	MGlobal::setActiveSelectionList(selected);
 }

 

 

 

Related Topics

MPxCommand - Adding Custom mel functions

MSyntax - simplifying argument parsing for your mel functions

Iterating the Maya Scene

Accessing Parenting Information

MPlug - Manually getting and setting node attributes