#include <stdio.h>
#include <maya/MIOStream.h>
#include <maya/MGlobal.h>
#include <maya/MString.h>
#include <maya/MStringArray.h>
#include <maya/MEventMessage.h>
#include <maya/MFnPlugin.h>
#include <maya/MPxCommand.h>
#include <maya/MSyntax.h>
#include <maya/MArgDatabase.h>
#define kMessageFlag "m"
#define kMessageFlagLong "message"
static void eventCB(void * data);
typedef MCallbackId* MCallbackIdPtr;
static MCallbackIdPtr callbackId = NULL;
{
public:
eventTest();
~eventTest() override;
static void* creator();
private:
bool addMessage;
bool delMessage;
};
eventTest::eventTest()
: addMessage(false)
, delMessage(false)
{
events.clear();
}
eventTest::~eventTest()
{
}
void* eventTest::creator()
{
return (void *) (new eventTest);
}
{
return syntax;
}
{
if (argData.isFlagSet(kMessageFlag))
{
bool flag;
status = argData.getFlagArgument(kMessageFlag, 0, flag);
if (!status)
{
status.
perror(
"could not parse message flag");
return status;
}
if (flag)
{
addMessage = true;
}
else
{
delMessage = true;
}
}
status = argData.getObjects(events);
if (!status)
{
status.
perror(
"could not parse event names");
}
if (events.length() == 0)
{
events = eventNames;
}
return status;
}
{
status = parseArgs(args);
if (!status)
{
return status;
}
int * indices = new int [events.length()];
int i, j;
for (i = 0; i < (int)events.length(); ++i)
{
indices[i] = -1;
for (j = 0; j < (int)eventNames.
length(); ++j)
{
if (events[i] == eventNames[j])
{
indices[i] = j;
break;
}
}
}
for (i = 0; i < (int)events.length(); ++i)
{
j = indices[i];
if (j == -1)
{
MString(
"is not a valid event name\n"));
break;
}
if (addMessage && callbackId[j] == 0)
{
events[i],
eventCB,
(void *)(size_t)j,
&status);
if (!status)
{
status.
perror(
"failed to add callback for " + events[i]);
callbackId[j] = 0;
}
}
else if (delMessage && callbackId[j] != 0)
{
if (!status)
{
status.
perror(
"failed to remove callback for " + events[i]);
}
callbackId[j] = 0;
}
}
char tmpStr[128];
bool msgs;
for (i = 0; i < (int)events.length(); ++i)
{
j = indices[i];
if (j == -1)
{
continue;
}
msgs = (callbackId[j] != 0);
sprintf(tmpStr, "%-20s %s\n",
events[i].asChar(),
msgs ? "yes" : "no");
}
delete [] indices;
return status;
}
{
MFnPlugin plugin( obj, PLUGIN_COMPANY,
"3.0",
"Any");
if (!status)
{
return status;
}
int i;
for (i = 0; i < (int)eventNames.
length(); ++i)
{
if (eventNames[i] == "idle" ||
eventNames[i] == "idleHigh")
{
--i;
}
}
cout<<
"eventTest: "<<eventNames.
length()<<
" events are defined"<<endl;
callbackId =
new MCallbackId [eventNames.
length()];
if (!callbackId)
{
return MStatus(MS::kInsufficientMemory);
}
for (i = 0; i < (int)eventNames.
length(); ++i)
{
callbackId[i] = 0;
}
status = plugin.registerCommand("eventTest",
eventTest::creator,
eventTest::newSyntax);
if (!status)
{
status.
perror(
"registerCommand");
}
return status;
}
{
int i;
int len = eventNames.
length();
for (i = 0; i < len; ++i)
{
if (callbackId[i] != 0)
{
eventNames[i] +
"\n");
callbackId[i] = 0;
}
}
delete [] callbackId;
status = plugin.deregisterCommand("eventTest");
if (!status)
{
status.
perror(
"deregisterCommand");
}
return status;
}
static void eventCB(void * data)
{
int i = (int)(size_t)data;
if (i >= 0 && i < (
int)eventNames.
length())
{
eventNames[i] +
" occurred\n");
}
else
{
}
}