polyMessageCmd.cpp
#include <maya/MIOStream.h>
#include <maya/MPxCommand.h>
#include <maya/MFnPlugin.h>
#include <maya/MArgList.h>
#include <maya/MUintArray.h>
#include <maya/MSelectionList.h>
#include <maya/MGlobal.h>
#include <maya/MPlug.h>
#include <maya/MPolyMessage.h>
#include <maya/MDagPath.h>
#include <maya/MCallbackIdArray.h>
MCallbackIdArray callbackIds;
void userCB( MUintArray componentIds[], unsigned int count, void *clientData )
{
cout << "poly component id modified";
cout << endl;
if ( count != MPolyMessage::kLastErrorIndex )
return;
unsigned int i, id;
unsigned int kDeletedId = MPolyMessage::deletedId();
MUintArray vertexIds = componentIds[MPolyMessage::kVertexIndex];
for ( i = 0; i < vertexIds.length(); i++ )
{
id = vertexIds[i];
if ( id == kDeletedId )
cout << "vertex " << i << " deleted" << endl;
else if ( i != id )
cout << "vertex " << i << " " << id << endl;
}
MUintArray edgeIds = componentIds[MPolyMessage::kEdgeIndex];
for ( i = 0; i < edgeIds.length(); i++ )
{
id = edgeIds[i];
if ( id == kDeletedId )
cout << "edge " << i << " deleted" << endl;
else if ( i != id )
cout << "edge " << i << " " << id << endl;
}
MUintArray faceIds = componentIds[MPolyMessage::kFaceIndex];
for ( i = 0; i < faceIds.length(); i++ )
{
id = faceIds[i];
if ( id == kDeletedId )
cout << "face " << i << " deleted" << endl;
else if ( i != id )
cout << "face " << i << " " << id << endl;
}
}
class polyMessageCmd : public MPxCommand
{
public:
polyMessageCmd() {};
virtual ~polyMessageCmd();
MStatus doIt( const MArgList& args );
static void* creator();
};
polyMessageCmd::~polyMessageCmd() {}
void* polyMessageCmd::creator()
{
return new polyMessageCmd();
}
MStatus polyMessageCmd::doIt( const MArgList& )
{
MStatus stat;
MObject node;
MSelectionList list;
MCallbackId id;
MGlobal::getActiveSelectionList( list );
for ( unsigned int i=0; i<list.length(); i++ )
{
list.getDependNode( i, node );
MDagPath dp;
MObject shapeNode = node;
if ( MS::kSuccess == MDagPath::getAPathTo( node, dp ) )
if ( MS::kSuccess == dp.extendToShape() )
shapeNode = dp.node();
bool wantIdChanges[3];
wantIdChanges[MPolyMessage::kVertexIndex] = true;
wantIdChanges[MPolyMessage::kEdgeIndex] = true;
wantIdChanges[MPolyMessage::kFaceIndex] = true;
id = MPolyMessage::addPolyComponentIdChangedCallback( shapeNode,
wantIdChanges, 3,
userCB,
NULL,
&stat);
if ( stat ) {
callbackIds.append( id );
} else {
cout << "MPolyMessage.addCallback failed\n";
}
}
return stat;
}
MStatus initializePlugin( MObject obj )
{
MFnPlugin plugin(obj, PLUGIN_COMPANY, "6.0", "Any");
return plugin.registerCommand( "polyMessage", polyMessageCmd::creator );
}
MStatus uninitializePlugin( MObject obj)
{
for (unsigned int i=0; i<callbackIds.length(); i++ ) {
MMessage::removeCallback( (MCallbackId)callbackIds[i] );
}
MFnPlugin plugin( obj );
return plugin.deregisterCommand( "polyMessage" );
}