circleNode.cpp
#include <string.h>
#include <maya/MIOStream.h>
#include <maya/MPxNode.h>
#include <maya/MString.h>
#include <maya/MTypeId.h>
#include <maya/MPlug.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MVector.h>
#include <maya/MFnPlugin.h>
#include <maya/MDataBlock.h>
#include <maya/MDataHandle.h>
#include <math.h>
class circle : public MPxNode
{
public:
circle();
virtual ~circle();
virtual MStatus compute( const MPlug& plug, MDataBlock& data );
static void* creator();
static MStatus initialize();
public:
static MObject input;
static MObject sOutput;
static MObject cOutput;
static MObject frames;
static MObject scale;
static MTypeId id;
};
MTypeId circle::id( 0x80005 );
MObject circle::input;
MObject circle::sOutput;
MObject circle::cOutput;
MObject circle::frames;
MObject circle::scale;
void* circle::creator()
{
return new circle();
}
MStatus circle::initialize()
{
MFnNumericAttribute nAttr;
MStatus stat;
input = nAttr.create( "input", "in", MFnNumericData::kFloat, 0.0,
&stat );
CHECK_MSTATUS( stat );
CHECK_MSTATUS( nAttr.setStorable( true ) );
scale = nAttr.create( "scale", "sc", MFnNumericData::kFloat, 10.0,
&stat );
CHECK_MSTATUS( stat );
CHECK_MSTATUS( nAttr.setStorable( true ) );
frames = nAttr.create( "frames", "fr", MFnNumericData::kFloat, 48.0,
&stat );
CHECK_MSTATUS( stat );
CHECK_MSTATUS( nAttr.setStorable( true ) );
sOutput = nAttr.create( "sineOutput", "so", MFnNumericData::kFloat,
0.0, &stat );
CHECK_MSTATUS( stat );
CHECK_MSTATUS( nAttr.setWritable( false ) );
CHECK_MSTATUS( nAttr.setStorable( false ) );
cOutput = nAttr.create( "cosineOutput", "co", MFnNumericData::kFloat,
0.0, &stat );
CHECK_MSTATUS( stat );
CHECK_MSTATUS( nAttr.setWritable( false ) );
CHECK_MSTATUS( nAttr.setStorable( false ) );
CHECK_MSTATUS( addAttribute( input ) );
CHECK_MSTATUS( addAttribute( scale ) );
CHECK_MSTATUS( addAttribute( frames ) );
CHECK_MSTATUS( addAttribute( sOutput ) );
CHECK_MSTATUS( addAttribute( cOutput ) );
CHECK_MSTATUS( attributeAffects( input, sOutput ) );
CHECK_MSTATUS( attributeAffects( input, cOutput ) );
CHECK_MSTATUS( attributeAffects( scale, sOutput ) );
CHECK_MSTATUS( attributeAffects( scale, cOutput ) );
CHECK_MSTATUS( attributeAffects( frames, sOutput ) );
CHECK_MSTATUS( attributeAffects( frames, cOutput ) );
return MS::kSuccess;
}
circle::circle() {}
circle::~circle() {}
MStatus circle::compute (const MPlug& plug, MDataBlock& data)
{
MStatus returnStatus;
if (plug == sOutput || plug == cOutput) {
MDataHandle inputData = data.inputValue (input, &returnStatus);
CHECK_MSTATUS( returnStatus );
MDataHandle scaleData = data.inputValue (scale, &returnStatus);
CHECK_MSTATUS( returnStatus );
MDataHandle framesData = data.inputValue (frames, &returnStatus);
CHECK_MSTATUS( returnStatus );
float currentFrame = inputData.asFloat();
float scaleFactor = scaleData.asFloat();
float framesPerCircle = framesData.asFloat();
float angle = 6.2831853f * (currentFrame/framesPerCircle);
float sinResult = sinf (angle) * scaleFactor;
float cosResult = cosf (angle) * scaleFactor;
MDataHandle sinHandle = data.outputValue( circle::sOutput,
&returnStatus );
CHECK_MSTATUS( returnStatus );
MDataHandle cosHandle = data.outputValue( circle::cOutput,
&returnStatus );
CHECK_MSTATUS( returnStatus );
sinHandle.set( sinResult );
cosHandle.set( cosResult );
CHECK_MSTATUS( data.setClean( plug ) );
} else {
return MS::kUnknownParameter;
}
return MS::kSuccess;
}
MStatus initializePlugin ( MObject obj )
{
MStatus status;
MFnPlugin plugin( obj, PLUGIN_COMPANY, "4.5", "Any");
status = plugin.registerNode( "circle", circle::id,
circle::creator, circle::initialize );
if (!status) {
status.perror("registerNode");
return status;
}
return status;
}
MStatus uninitializePlugin( MObject obj)
{
MStatus status;
MFnPlugin plugin( obj );
status = plugin.deregisterNode( circle::id );
if (!status) {
status.perror("deregisterNode");
return status;
}
return status;
}