五、selecting with the API

1. 命令通常从selection list中得到input, 调用MGlobal::getActiveSelectionList(MSelectionList &dest, bool orderedSelectionIfAvailabel=false).得到MSelectionList和MItSelectionList来编辑选择列表。

2. 一个打印所有DAG nodes的例子:

#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" );

如果你建造几何体,然后选中它,这个例子会打印出它的名字。

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. 如果一个function set要操作不同的object,使用这个办法。

如果你选择了一个object的很多component,这个object也只会输出一次。

如果你选择了两个object的components, components多的那个object会在selection list appear两次。

3.MItSelectionList: lets you filter the objects on the selection list to only see objects of a particular type.

MGlobal::getActiveSelectionList( list );
for ( MItSelectionList listIter( list ); !listIter.isDone(); listIter.next() )
{
    listIter.getDagPath( node, component );
    nodeFn.setObject( node );
    cout << nodeFn.name().asChar() << "%s is selected” << endl;
}

第二部分的代码可以被这段代码取代。

如果你只想Iterator查看具体某一种object:

  MItSelectionList listIter( list, MFn::kNurbsSurface )

4. The MFn::Type enumeration is used throughout the API to indicate item types.

5. Another way is to use MGlobal::selectByName(). This finds all objects matching a pattern and adds them to the active selection list. For example:

MGlobal::selectByName( "*Sphere*" );

原文地址:https://www.cnblogs.com/bubbler/p/5152817.html