Maya for 3D Printing - Rapid Prototyping
In this course we're going to look at something a little different, creating technically accurate 3D printed parts.
# 1 30-12-2007 , 04:22 PM
Registered User
Join Date: Dec 2007
Posts: 12

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

# 2 30-12-2007 , 11:20 PM
t1ck135's Avatar
Registered User
Join Date: May 2004
Location: UK
Posts: 1,991
looks like you're getting into some complicated but cool stuff there david.

1) and 4) I dont see any fclose commands after opening and writing to the file - maybe that has something to do with it

2) and 3) sorry no idea user added image

Simon


Examples of bTraffic - a traffic animation tool for Maya
bFlocking - a tool for Maya 8.5+ to generate flocking and swarming behaviours
Jan/Feb Challenge 2007 Entry and W.I.P
May/Jun Challenge 2006 Entry and W.I.P
Mar/Apr Challenge 2006 Entry and W.I.P
Jan/Feb Challenge 2006 Entry and W.I.P
Nov/Dec Challenge 2005 Entry and W.I.P
Sep/Oct Challenge 2005 Entry and W.I.P
Jul/Aug Challenge 2005 Entry
www.flash-fx.net
Posting Rules Forum Rules
You may not post new threads | You may not post replies | You may not post attachments | You may not edit your posts | BB code is On | Smilies are On | [IMG] code is On | HTML code is Off

Similar Threads