Maya Training

Introduction to Hard Surface Modeling

Maya Training

Interiors and Furniture Vol 1 & 2 - Modeling & Cloth

Maya Training

Shading, Lighting and Rendering the Bedroom in MR

Maya Training

Robot volume one - Modeling with animation in mind

Maya Training

Spach-Alspaugh House the complete courseware

Maya Training

Burt The Cartoon Dinosaur Vol 01 - Modeling

New Maya Training

Robot Volume 02 - Hybrid Rigging
You are here > Home > SimplyMaya Community Forums
Loading

Welcome to Simply Maya

Please Sign in or Sign up for an account

Member Login

Lost your password?

Not a member yet? Sign Up!

Old 30-12-2007, 04:22 PM   #1
daviddoria
Registered User
 
Join Date: Dec 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
daviddoria is offline   Reply With Quote
Old 30-12-2007, 11:20 PM   #2
t1ck135
Registered User
 
t1ck135's Avatar
 
Join Date: May 2004
Location: UK
Posts: 1,991
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

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
t1ck135 is offline   Reply With Quote
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
New Moderators on Simply Maya Miss_Nova Site News & Announcements 21 17-05-2011 01:24 AM
Maya Hair/Polygon Error ingram_no.7 Members Lounge 13 10-12-2010 11:27 AM
Simply Maya Resources Online Miss_Nova Site News & Announcements 1 08-12-2010 09:36 PM
LayoutTools for Maya 2011 is here... ctbram Members Lounge 1 06-12-2010 11:32 PM
Open a Lightwave file in Maya 2011 radiant777 Maya Basics & Newbie Lounge 2 04-12-2010 03:45 AM

Thread Tools





A little bit about who we are
Links you might find useful
Catch up with SimplyMaya
SimplyMaya specialises in Maya tutorials. We offer over 1,000 individual Maya training videos, ranging from basic Maya tutorials through to intermediate Maya tutorials. Our tutorials are created by instructors with industry experience and are designed to get you up and running in Maya quickly without making it seem like hard work.

Copyright © 1999-2015 SimplyMaya - vBulletin® Copyright © 2000-2015, Jelsoft Enterprises Ltd.