deletedMsgCmd.cpp
#include <maya/MFnPlugin.h>
#include <maya/MStatus.h>
#include <maya/MPxCommand.h>
#include <maya/MMessage.h>
#include <maya/MNodeMessage.h>
#include <maya/MDGMessage.h>
#include <maya/MGlobal.h>
#include <maya/MArgList.h>
#include <maya/MArgDatabase.h>
#include <maya/MSyntax.h>
#include <maya/MSelectionList.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MDGModifier.h>
#include <maya/MPlugArray.h>
#include <maya/MPlug.h>
#include <maya/MCallbackIdArray.h>
class deletedMessage : public MPxCommand
{
public:
virtual MStatus doIt( const MArgList& );
static void *creator();
static MSyntax newSyntax();
static void removeCallbacks();
static void aboutToDeleteCB(MObject& node, MDGModifier& modifier,
void* clientData);
static void preRemovalCB(MObject& node, void* clientData);
static void removeCB(MObject& node, void* clientData);
private:
static MCallbackIdArray callbackIds;
static bool nodeRemovedCBRegistered;
};
MCallbackIdArray deletedMessage::callbackIds;
bool deletedMessage::nodeRemovedCBRegistered = false;
MStatus deletedMessage::doIt( const MArgList& args )
{
MStatus status = MS::kSuccess;
MArgDatabase argData(syntax(), args);
MSelectionList objects;
argData.getObjects(objects);
for (unsigned int i = 0; i < objects.length(); i++) {
MObject node;
objects.getDependNode(i, node);
callbackIds.append(
MNodeMessage::addNodeAboutToDeleteCallback (node, aboutToDeleteCB, NULL, &status)
);
if (!status) {
MGlobal::displayWarning("Could not attach about to delete callback for node.");
continue;
}
callbackIds.append(
MNodeMessage::addNodePreRemovalCallback (node, preRemovalCB, NULL, &status)
);
if (!status) {
MGlobal::displayWarning("Could not attach pre-removal callback for node.");
continue;
}
if (!nodeRemovedCBRegistered)
{
callbackIds.append(
MDGMessage::addNodeRemovedCallback(removeCB, "dependNode", NULL, &status)
);
if (!status) {
MGlobal::displayWarning("Could not attach node removal callback.");
continue;
}
nodeRemovedCBRegistered = true;
}
}
return status;
}
void* deletedMessage::creator()
{
return new deletedMessage;
}
MSyntax deletedMessage::newSyntax() {
MSyntax syntax;
syntax.setObjectType(MSyntax::kSelectionList);
syntax.setMinObjects(1);
syntax.useSelectionAsDefault(true);
return syntax;
}
void deletedMessage::aboutToDeleteCB(MObject& node, MDGModifier& modifier, void* clientData) {
MFnDependencyNode nodeFn(node);
MGlobal::displayInfo(MString("About to delete callback for node: ") + nodeFn.name());
}
void deletedMessage::preRemovalCB(MObject& node, void* clientData) {
MFnDependencyNode nodeFn(node);
MGlobal::displayInfo(MString("Pre-removal callback for node: ") + nodeFn.name());
}
void deletedMessage::removeCB(MObject& node, void* clientData) {
MFnDependencyNode nodeFn(node);
MGlobal::displayInfo(MString("Removal callback node: ") + nodeFn.name());
}
void deletedMessage::removeCallbacks() {
MMessage::removeCallbacks(callbackIds);
}
MStatus initializePlugin(MObject obj)
{
MFnPlugin pluginFn(obj, PLUGIN_COMPANY, "6.0");
MStatus status;
status = pluginFn.registerCommand("deletedMessage", deletedMessage::creator, deletedMessage::newSyntax);
if( !status)
status.perror("register Command failed");
return status;
}
MStatus uninitializePlugin ( MObject obj )
{
MFnPlugin pluginFn(obj);
MStatus status = MS::kSuccess;
deletedMessage::removeCallbacks();
status = pluginFn.deregisterCommand("deletedMessage");
return status;
}