Maya API How-To #33

Back · Previous Maya

How do I select a new object that I've created using MDagModifier?

When you create a node via MDagModifier, Maya doesn't automatically select the new node, as it would do using MEL's "createNode". For example:

MDagModifier dagModifier;

dagModifier.createNode( "locator", MObject::kNullObj );

// Node is created, but not automatically selected.
//
dagModifier.doIt();

To select the node, your first instinct will be to use the returned MObject and add it to a MSelectionList:

MObject newNode = dagModifier.createNode( "locator", MObject::kNullObj );
status = dagModifier.doIt();

MSelectionList list;
list.add( newNode );

MGlobal::setActiveSelectionList( list );

However, this doesn't work. In fact, you'll need to find the DAG path for the node, and add that to the MSelectionList:

MObject newNode = dagModifier.createNode( "locator", MObject::kNullObj );
status = dagModifier.doIt();

MDagPath dagPath = MDagPath::getAPathTo( newNode );

MSelectionList list;
list.add( dagPath );

MGlobal::setActiveSelectionList( list );

02 Apr 2006