#include <nodeCreatedCBCmd.h>
#include <maya/MFnPlugin.h>
#include <maya/MObject.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MFnDagNode.h>
#include <maya/MDGMessage.h>
#include <maya/MArgDatabase.h>
#include <maya/MSyntax.h>
#include <maya/MGlobal.h>
#define kRegisterFlagLong "-register"
#define kRegisterFlag "-r"
#define kUnregisterFlagLong "-unregister"
#define kUnregisterFlag "-u"
#define kFilterFlagLong "-filter"
#define kFilterFlag "-f"
#define kFullDagPathFlagLong "-fullDagPath"
#define kFullDagPathFlag "-fdp"
MCallbackId nodeCreatedCB::sId;
MStringArray nodeCreatedCB::sMelProcs;
MIntArray nodeCreatedCB::sFullDagPath;
MStatus nodeCreatedCB::doIt( const MArgList& args )
{
MStatus stat = MS::kSuccess;
MArgDatabase argData( syntax(), args );
if ( argData.isFlagSet( kRegisterFlag ) ) {
MString proc;
argData.getFlagArgument( kRegisterFlag, 0, proc );
stat = registerMelProc( proc, argData.isFlagSet( kFullDagPathFlag ) );
} else if ( argData.isFlagSet( kUnregisterFlag ) ) {
MString proc;
argData.getFlagArgument( kUnregisterFlag, 0, proc );
stat = unregisterMelProc( proc );
} else if ( argData.isFlagSet( kFilterFlag ) ) {
MString filter;
argData.getFlagArgument( kFilterFlag, 0, filter );
stat = changeFilter( filter );
}
if ( stat.error() ) {
MGlobal::displayError( stat.errorString() );
}
return stat;
}
MStatus nodeCreatedCB::registerMelProc( MString melProc, bool fullDagPath )
{
MStatus stat = MS::kSuccess;
if ( melProc.length() == 0 ) {
stat = MS::kFailure;
stat.perror("invalid mel callback: " + melProc);
return stat;
}
nodeCreatedCB::sMelProcs.append( melProc );
nodeCreatedCB::sFullDagPath.append( fullDagPath );
return stat;
}
MStatus nodeCreatedCB::unregisterMelProc( MString melProc )
{
MStatus stat = MS::kFailure;
int numProcs = nodeCreatedCB::sMelProcs.length();
for ( int i = 0; i < numProcs; i++ ) {
if ( nodeCreatedCB::sMelProcs[i] == melProc ) {
nodeCreatedCB::sMelProcs.remove(i);
nodeCreatedCB::sFullDagPath.remove(i);
stat = MS::kSuccess;
break;
}
}
if ( !stat ) {
stat.perror(melProc + " is not a registered callback.");
}
return stat;
}
MStatus nodeCreatedCB::changeFilter( MString filter )
{
MStatus stat = MS::kSuccess;
MDGMessage::removeCallback( nodeCreatedCB::sId );
nodeCreatedCB::sId = MDGMessage::addNodeAddedCallback( nodeCreatedCB::sCallbackFunc, filter );
return stat;
}
void nodeCreatedCB::sCallbackFunc( MObject& node, void* clientData )
{
int numProcs = nodeCreatedCB::sMelProcs.length();
for ( int i = 0; i < numProcs; i++ ) {
MString melCmd = nodeCreatedCB::sMelProcs[i];
MString nodeName;
if ( nodeCreatedCB::sFullDagPath[i] &&
node.hasFn( MFn::kDagNode ) ) {
MFnDagNode dagObj( node );
nodeName = dagObj.fullPathName();
} else {
MFnDependencyNode dn( node );
nodeName = dn.name();
}
melCmd += " \"" + nodeName + "\"";
MGlobal::executeCommand( melCmd );
}
}
MSyntax nodeCreatedCB::newSyntax()
{
MSyntax syntax;
syntax.addFlag( kRegisterFlag, kRegisterFlagLong, MSyntax::kString );
syntax.addFlag( kUnregisterFlag, kUnregisterFlagLong, MSyntax::kString );
syntax.addFlag( kFilterFlag, kFilterFlagLong, MSyntax::kString );
syntax.addFlag( kFullDagPathFlag, kFullDagPathFlagLong );
return syntax;
}
void* nodeCreatedCB::creator()
{
return new nodeCreatedCB;
}
MStatus initializePlugin( MObject obj )
{
MFnPlugin plugin( obj, PLUGIN_COMPANY, "6.0" );
MStatus stat;
stat = plugin.registerCommand( "nodeCreatedCB",
nodeCreatedCB::creator,
nodeCreatedCB::newSyntax );
if ( !stat )
stat.perror("registerCommand");
nodeCreatedCB::sId = MDGMessage::addNodeAddedCallback( nodeCreatedCB::sCallbackFunc );
return stat;
}
MStatus uninitializePlugin( MObject obj )
{
MFnPlugin plugin( obj );
MStatus stat;
stat = plugin.deregisterCommand( "nodeCreatedCB" );
if ( !stat )
stat.perror("deregisterCommand");
MDGMessage::removeCallback( nodeCreatedCB::sId );
nodeCreatedCB::sMelProcs.clear();
nodeCreatedCB::sFullDagPath.clear();
return stat;
}