Maya API How-To #03

Back · Previous · Next Maya

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

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

//------------------------------------------------------------
// MStringArrayHelper declaration
//------------------------------------------------------------

class MStringArrayHelper: public MStringArray
{
  public:

    enum PathType
    {
      kFullPathName = 0,
      kPartialPathName
    };

    MStringArrayHelper(void);
    virtual ~MStringArrayHelper(void);

    MStatus   getDGOfType( MFn::Type );
    MStatus   getDAGOfType( MFn::Type, MStringArrayHelper::PathType pathType );
};

//------------------------------------------------------------
// getDGOfType
//
// Fills MStringArray with array of node names 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 MStringArray-derived class to
// return a string array.
//------------------------------------------------------------

MStatus MStringArrayHelper::getDGOfType( MFn::Type type )
{
  MObject             dNode;
  MFnDependencyNode   dNodeFn;

  for ( MItDependencyNodes listIter( type ); !listIter.isDone(); listIter.next() )
  {
    dNode = listIter.item();
    dNodeFn.setObject( dNode );
    if ( MS::kSuccess != append( dNodeFn.name() ) )
      return MS::kFailure;
  }

  return MS::kSuccess;
}

//------------------------------------------------------------
// getDAGOfType
//
// Fills MSelectionList with array of node names of given type.
//
// My findings suggest that the MGlobal::getSelection* functions
// can be used ONLY for DAG nodes, and not DG nodes.  For DG node
// support, see MStringArrayHelper::getDGOfType()
//------------------------------------------------------------

MStatus MStringArrayHelper::getDAGOfType( MFn::Type type, MStringArrayHelper::PathType pathType )
{
  MObject           dNode;
  MFnDagNode        nodeFn;

  for ( MItDag listIter( MItDag::kDepthFirst, type ); !listIter.isDone(); listIter.next() )
  {
    switch ( pathType )
    {
    case kFullPathName:
      {
        if ( MS::kSuccess != append( listIter.fullPathName() ) )
          return MS::kFailure;
      }
    case kPartialPathName:
      {
        if ( MS::kSuccess != append( listIter.partialPathName() ) )
          return MS::kFailure;
      }
    }
  }

  return MS::kSuccess;
}

Related How-To's