genericAttributeNode.cpp
#include <string.h>
#include <maya/MIOStream.h>
#include <math.h>
#include <maya/MPxNode.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFnGenericAttribute.h>
#include <maya/MFnPlugin.h>
#include <maya/MTypeId.h>
#include <maya/MPlug.h>
#include <maya/MDataBlock.h>
#include <maya/MDataHandle.h>
#include <maya/MGlobal.h>
class genericAttributeNode : public MPxNode
{
public:
genericAttributeNode();
virtual ~genericAttributeNode();
virtual MStatus compute( const MPlug& plug, MDataBlock& data );
static void* creator();
static MStatus initialize();
static MStatus addComplexFloatGenericAttribute( MObject& attrObject, const char *longName, const char *shortName );
public:
static MObject gInputInt;
static MObject gOutputFloat_2Float_3Float;
static MTypeId id;
};
MTypeId genericAttributeNode::id( 0x81031 );
MObject genericAttributeNode::gInputInt;
MObject genericAttributeNode::gOutputFloat_2Float_3Float;
genericAttributeNode::genericAttributeNode() {}
genericAttributeNode::~genericAttributeNode() {}
MStatus genericAttributeNode::compute( const MPlug& plug, MDataBlock& data )
{
MStatus returnStatus;
if ( plug == gOutputFloat_2Float_3Float )
{
MDataHandle inputDataInt = data.inputValue( gInputInt );
int inputInt = inputDataInt.asInt();
MDataHandle outputData = data.outputValue( plug );
bool isGenericNumeric = false;
bool isGenericNull = false;
if ( outputData.isGeneric( isGenericNumeric, isGenericNull ) )
{
if ( inputInt == 1 )
outputData.setGenericBool( false, true );
else if ( inputInt == 2 )
outputData.setGenericBool( true, true );
else if ( inputInt == 3 )
outputData.setGenericChar( 127, true );
else if ( inputInt == 4 )
outputData.setGenericDouble( 3.145, true );
else if ( inputInt == 5 )
outputData.setGenericFloat( (float)9.98, true );
else if ( inputInt == 6 )
outputData.setGenericShort( 3245, true );
else if ( inputInt == 7 )
outputData.setGenericInt( 32768, true );
else if ( inputInt == 8 )
{
MFnNumericData numericData;
MObject obj = numericData.create( MFnNumericData::k2Float, &returnStatus );
CHECK_MSTATUS( returnStatus );
returnStatus = numericData.setData( (float)1.5, (float)6.7 );
CHECK_MSTATUS( returnStatus );
outputData.set( obj );
}
else if ( inputInt == 9 )
{
MFnNumericData numericData;
MObject obj = numericData.create( MFnNumericData::k3Float, &returnStatus );
CHECK_MSTATUS( returnStatus );
returnStatus = numericData.setData( (float)2.5, (float)8.7, (float)2.3345 );
CHECK_MSTATUS( returnStatus );
outputData.set( obj );
}
else if ( inputInt == 10 )
{
outputData.setGenericInt( 909, true );
}
outputData.setClean();
data.setClean( gOutputFloat_2Float_3Float );
}
}
else
{
return MS::kUnknownParameter;
}
return MS::kSuccess;
}
void* genericAttributeNode::creator()
{
return new genericAttributeNode();
}
MStatus genericAttributeNode::addComplexFloatGenericAttribute(
MObject& attrObject,
const char *longName, const char *shortName )
{
MFnGenericAttribute gAttr;
attrObject = gAttr.create( longName, shortName );
MStatus status = gAttr.addAccept(MFnNumericData::kFloat);
CHECK_MSTATUS( status );
status = gAttr.addAccept(MFnNumericData::k2Float);
CHECK_MSTATUS( status );
status = gAttr.addAccept(MFnNumericData::k3Float);
CHECK_MSTATUS( status );
gAttr.setWritable(false);
gAttr.setStorable( false );
status = addAttribute( attrObject );
CHECK_MSTATUS( status );
return MS::kSuccess;
}
MStatus genericAttributeNode::initialize()
{
MFnNumericAttribute nAttr;
MStatus status;
gInputInt = nAttr.create( "gInputInt", "gii",
MFnNumericData::kInt, 0, &status );
nAttr.setStorable(true);
nAttr.setKeyable(true);
CHECK_MSTATUS( status );
status = addAttribute( gInputInt );
CHECK_MSTATUS( status );
if ( !addComplexFloatGenericAttribute( gOutputFloat_2Float_3Float,
"gOutputFloat_2Float_3Float", "gof2f3f" ) )
return MS::kFailure;
status = attributeAffects( gInputInt, gOutputFloat_2Float_3Float );
CHECK_MSTATUS( status );
return MS::kSuccess;
}
MStatus initializePlugin( MObject obj )
{
MStatus status;
MFnPlugin plugin( obj, PLUGIN_COMPANY, "7.0", "Any");
status = plugin.registerNode( "genericAttributeNode",
genericAttributeNode::id, genericAttributeNode::creator,
genericAttributeNode::initialize );
if (!status) {
status.perror("registerNode");
return status;
}
return status;
}
MStatus uninitializePlugin( MObject obj)
{
MStatus status;
MFnPlugin plugin( obj );
status = plugin.deregisterNode( genericAttributeNode::id );
if (!status) {
status.perror("deregisterNode");
return status;
}
return status;
}