#include <string.h>
#include <maya/MIOStream.h>
#include <math.h>
#include <maya/MPxNode.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFnCompoundAttribute.h>
#include <maya/MFnPlugin.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MTypeId.h>
#include <maya/MPlug.h>
#include <maya/MDataBlock.h>
#include <maya/MDataHandle.h>
#include <maya/MArrayDataHandle.h>
#define McheckErr(stat,msg) \
if ( MS::kSuccess != stat ) { \
cerr << msg; \
return MS::kFailure; \
}
class weightList : public MPxNode
{
public:
weightList();
virtual ~weightList();
static void* creator();
static MStatus initialize();
virtual MStatus compute( const MPlug&, MDataBlock&);
public:
static MObject aWeightsList;
static MObject aWeights;
static MObject aBias;
static MTypeId id;
private:
};
MTypeId weightList::id( 0x81035 );
MObject weightList::aWeightsList;
MObject weightList::aWeights;
MObject weightList::aBias;
weightList::weightList() {}
weightList::~weightList() {}
void* weightList::creator()
{
return new weightList();
}
MStatus weightList::initialize()
{
MStatus status;
MFnNumericAttribute numAtt;
aBias = numAtt.create( "bias", "b", MFnNumericData::kFloat);
addAttribute(aBias);
aWeights = numAtt.create("weights", "w", MFnNumericData::kFloat, -1000.0, &status);
numAtt.setKeyable(true);
numAtt.setArray(true);
numAtt.setReadable(true);
numAtt.setUsesArrayDataBuilder(true);
addAttribute(aWeights);
MFnCompoundAttribute cmpAttr;
aWeightsList = cmpAttr.create("weightsList", "wl", &status);
cmpAttr.setArray(true);
cmpAttr.addChild(aWeights);
cmpAttr.setReadable(true);
cmpAttr.setUsesArrayDataBuilder(true);
addAttribute(aWeightsList);
attributeAffects(aBias, aWeightsList);
return MStatus::kSuccess;
}
MStatus weightList::compute( const MPlug& plug, MDataBlock& block)
{
MStatus status = MS::kSuccess;
unsigned i, j;
MObject thisNode = thisMObject();
MPlug wPlug(thisNode, aWeights);
for( i = 0; i < 3; i++) {
status = wPlug.selectAncestorLogicalIndex( i, aWeightsList );
MDataHandle wHandle = wPlug.constructHandle(block);
MArrayDataHandle arrayHandle(wHandle, &status);
McheckErr(status, "arrayHandle construction failed\n");
MArrayDataBuilder arrayBuilder = arrayHandle.builder(&status);
McheckErr(status, "arrayBuilder accessing/construction failed\n");
for( j = 0; j < i+2; j++) {
MDataHandle handle = arrayBuilder.addElement(j,&status);
McheckErr(status, "addElement to arrayBuilder failed\n");
float val = 1.0f*(i+j);
handle.set(val);
}
status = arrayHandle.set(arrayBuilder);
McheckErr(status, "set arrayBuilder failed\n");
wPlug.setValue(wHandle);
wPlug.destructHandle(wHandle);
}
MArrayDataHandle arrayHandle = block.inputArrayValue(aWeightsList, &status);
McheckErr(status, "arrayHandle construction for aWeightsList failed\n");
unsigned count = arrayHandle.elementCount();
for( i = 0; i < count; i++) {
arrayHandle.jumpToElement(i);
MDataHandle eHandle = arrayHandle.inputValue(&status).child(aWeights);
McheckErr(status, "handle evaluation failed\n");
MArrayDataHandle eArrayHandle(eHandle, &status);
McheckErr(status, "arrayHandle construction for aWeights failed\n");
unsigned eCount = eArrayHandle.elementCount();
for( j = 0; j < eCount; j++) {
eArrayHandle.jumpToElement(j);
float weight = eArrayHandle.inputValue(&status).asFloat();
McheckErr(status, "weight evaluation error\n");
fprintf(stderr, "weightList[%u][%u] = %g\n",i,j,weight);
}
}
arrayHandle = block.inputArrayValue(aWeightsList, &status);
McheckErr(status, "arrayHandle construction for aWeightsList failed\n");
count = arrayHandle.elementCount();
for( i = 0; i < count; i++) {
arrayHandle.jumpToArrayElement(i);
MDataHandle eHandle = arrayHandle.inputValue(&status).child(aWeights);
McheckErr(status, "handle evaluation failed\n");
MArrayDataHandle eArrayHandle(eHandle, &status);
McheckErr(status, "arrayHandle construction for aWeights failed\n");
unsigned eCount = eArrayHandle.elementCount();
for( j = 0; j < eCount; j++) {
eArrayHandle.jumpToArrayElement(j);
float weight = eArrayHandle.inputValue(&status).asFloat();
McheckErr(status, "weight evaluation error\n");
fprintf(stderr, "weightList[%d][%d] = %g\n",i,j,weight);
}
}
return status;
}
MStatus initializePlugin( MObject obj )
{
MStatus result;
MFnPlugin plugin( obj, PLUGIN_COMPANY, "3.0", "Any");
result = plugin.registerNode( "weightList",
weightList::id,
weightList::creator,
weightList::initialize);
return result;
}
MStatus uninitializePlugin( MObject obj)
{
MStatus result;
MFnPlugin plugin( obj );
result = plugin.deregisterNode( weightList::id );
return result;
}