Maya API How-To #02

Back · Next Maya

How can I get a MObjectArray of a particular type of DAG/DG node?

Here's an example of how I've extend the MObjectArray class to populate itself with DAG/DG nodes matching a specified type.

//------------------------------------------------------------
// MObjectArrayHelper declaration
//------------------------------------------------------------

class MObjectArrayHelper: public MObjectArray
{
  public:

    MObjectArrayHelper(void);
    virtual ~MObjectArrayHelper(void);

    MStatus   getDGOfType( MFn::Type );
    MStatus   getDAGOfType( MFn::Type );
};
//------------------------------------------------------------
// getDGOfType
//
// Fills MObjectArray with array of nodes matching given type.
//
// * My findings suggested that the MGlobal::getSelection* functions
// could ONLY for DAG nodes and not DG nodes.  Thus, I've created
// this function as a member of a MObjectArray-derived class.
//------------------------------------------------------------

MStatus MObjectArrayHelper::getDGOfType( MFn::Type type )
{
  for ( MItDependencyNodes listIter( type ); !listIter.isDone(); listIter.next() )
  {
    if ( MS::kSuccess != append( listIter.item() ) )
      return MS::kFailure;
  }

  return MS::kSuccess;
}

//------------------------------------------------------------
// getDAGOfType
//
// Fills MObjectArray with array of DAG nodes matching given type.
//------------------------------------------------------------

MStatus MObjectArrayHelper::getDAGOfType( MFn::Type type )
{
  for ( MItDag listIter( MItDag::kDepthFirst, type ); !listIter.isDone(); listIter.next() )
  {
    if ( MS::kSuccess != append( listIter.item() ) )
      return MS::kFailure;
  }

  return MS::kSuccess;
}

Related How-To's