Simple node with Maya API
To understand how the API for creating node works a little bit, I thought I would just make a node that takes a numeric input from one node and passes it directly to another node.
NODE1 -> MY_NODE -> NODE2
Here is what I came up with:
Code:
#include <stdio.h>
#include <math.h>
#include <maya/MFnPlugin.h>
#include <maya/MIOStream.h>
#include <maya/MPxNode.h>
#include <maya/MString.h>
#include <maya/MTypeId.h>
#include <maya/MPlug.h>
#include <maya/MDataBlock.h>
#include <maya/MDataHandle.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFloatVector.h>
class ExportToFileNode : public MPxNode
{
public:
ExportToFileNode();
virtual ~ExportToFileNode();
virtual MStatus compute( const MPlug&, MDataBlock& );
virtual void postConstructor();
static void * creator();
static MStatus initialize();
// Id tag for use with binary file format
static MTypeId id;
private:
// Input attributes
static MObject aInData;
// Output attributes
static MObject aOutData;
};
// static data
MTypeId ExportToFileNode::id( 0x8100d );
// Attributes
MObject ExportToFileNode::aInData;
MObject ExportToFileNode::aOutData;
#define MAKE_INPUT(attr) \
CHECK_MSTATUS( attr.setKeyable(true) ); \
CHECK_MSTATUS( attr.setStorable(true) ); \
CHECK_MSTATUS( attr.setReadable(true) ); \
CHECK_MSTATUS( attr.setWritable(true) );
#define MAKE_OUTPUT(attr) \
CHECK_MSTATUS( attr.setKeyable(true) ); \
CHECK_MSTATUS( attr.setStorable(true) ); \
CHECK_MSTATUS( attr.setReadable(true) ); \
CHECK_MSTATUS( attr.setWritable(true) );
void ExportToFileNode::postConstructor( )
{
setMPSafe(true);
}
// DESCRIPTION: kind of like constructor
//
ExportToFileNode::ExportToFileNode()
{
}
// DESCRIPTION: kind of like destructor
//
ExportToFileNode::~ExportToFileNode()
{
}
// DESCRIPTION:
// creates an instance of the node
void * ExportToFileNode::creator()
{
return new ExportToFileNode();
}
MStatus ExportToFileNode::initialize()
{
FILE *out;
out = fopen( "c:\\init.txt", "w" );
fprintf( out, "Hello init\n");
MFnNumericAttribute nAttr;
// Input attributes
aInData = nAttr.create("Input1", "In1", MFnNumericData::kFloat);
MAKE_INPUT(nAttr);
CHECK_MSTATUS( nAttr.setDefault(.75) );
// Output attributes
aOutData = nAttr.create("Output1", "Out1", MFnNumericData::kFloat);
MAKE_OUTPUT(nAttr);
// Add attributes to the node database.
CHECK_MSTATUS( addAttribute(aInData) );
CHECK_MSTATUS( addAttribute(aOutData) );
// All input affect the output color
// CHECK_MSTATUS( attributeAffects(aInData) );
return MS::kSuccess;
}
MStatus ExportToFileNode::compute(const MPlug& plug, MDataBlock& block)
{
FILE *out;
out = fopen( "c:\\compute.txt", "w" );
fprintf( out, "computing\n");
block.outputValue( aOutData ) = block.inputValue( aInData );
return MS::kSuccess;
}
MStatus initializePlugin( MObject obj )
{
const MString UserClassify( "utility/general" );
MFnPlugin plugin(obj, PLUGIN_COMPANY, "4.5", "Any");
CHECK_MSTATUS( plugin.registerNode("ExportToFile", ExportToFileNode::id,
&ExportToFileNode::creator,
&ExportToFileNode::initialize,
MPxNode::kDependNode,
&UserClassify ) );
FILE *out;
out = fopen( "c:\\initplugin.txt", "w" );
fprintf( out, "Hello init plugin\n");
return MS::kSuccess;
}
MStatus uninitializePlugin( MObject obj )
{
MFnPlugin plugin( obj );
CHECK_MSTATUS( plugin.deregisterNode( ExportToFileNode::id ) );
FILE *out;
out = fopen( "c:\\uninitplugin.txt", "w" );
fprintf( out, "un init\n");
return MS::kSuccess;
}
I put some file outputs all around so I can see when things are executed.
Here are my questions:
1) the file "initplugin.txt" is created as soon as i load the plugin in maya, but "Hello init plugin" is not written to the file until i close maya??
2) the network I setup in hypershade is a samplerInfo node with the pointWorldX attribute as the input to my nodes Input1, and my nodes Output1 connected to a Blinn node's R color channel. When I export the network and reimport it and look at the network, the samplerInfo node is gone!
3) what does CHECK_MSTATUS do?
4) "compute.txt" is never written, even after I render the scene??
Any input you have for me will be much appreciated!
Thanks,
David