Published using Google Docs
NameNode

//NameNode.cpp

//working on any number string inputs node

//createNode nameNode;

//then hook up to its .string attribute, after adding node items in attribute editor or via a script (AEnewNonNumericMultiAddNewItem( nameNode , nameAttr )

//

/*

@author Nathaniel Anozie

ogbonnawork at gmail dot com

@note created: 07/12/2013

@note Modify at your own risk

@note Inspired by Rob Bateman's SimpleNode.cpp online example

@note Inspired by Brian Ewert for learning about creating string attribute (ewertb dot soundlinker dot com)

*/

//last updated: 07/12/2013 -- working on initial release

////for nodes, required

#include <maya/MPxNode.h>

#include <maya/MTypeId.h>

#include <maya/MPlug.h>

#include <maya/MDataBlock.h>

#include <maya/MDataHandle.h>

#include <maya/MString.h>

////

////for nodes, kinds of attributes

#include <maya/MFnNumericAttribute.h>

#include <maya/MFnNumericData.h>

//for nodes string stuff

#include <maya/MFnTypedAttribute.h>

#include <maya/MFnStringData.h>

#include <maya/MFnData.h>

////

//other stuff

//required always

#include <maya/MFnPlugin.h>

////--------for nodes required setup functions and stuff

class NameNode

:public MPxNode

{

    public:

        NameNode();

        virtual ~NameNode();

        static void * creator();

   

    //these are the main ones we make attibutes and set them in here

    public:    

        static MStatus initialize();

        virtual MStatus compute( const MPlug&, MDataBlock& );

    //

   

    public:

        const static MTypeId m_TypeId;    

        const static MString m_TypeName;

       

    private:  

        static MObject m_aString;

        static MObject m_aOutput;

};

NameNode::NameNode(){ }

NameNode::~NameNode(){}

void* NameNode::creator(){ return new NameNode(); }

////this is needed before we can use these holders to do stuff

MObject NameNode::m_aString;

MObject NameNode::m_aOutput;

////

////this is needed before we can make node in Maya

const MTypeId NameNode::m_TypeId(0x70032);

const MString NameNode::m_TypeName("nameNode");

////

////last setup stuff, note no mel command implemented yet

MStatus initializePlugin(MObject obj)

{

    MStatus status;

    MFnPlugin plugin( obj, "Nathaniel Anozie", "1.0", "Any");

    status = plugin.registerNode(NameNode::m_TypeName,

    NameNode::m_TypeId,

    NameNode::creator,

    NameNode::initialize,

    MPxNode::kDependNode);

   

    if(status != MS::kSuccess)

        return status;

   

    return status;

}

MStatus uninitializePlugin(MObject obj)

{

    MFnPlugin plugin( obj );

   

    MStatus status = plugin.deregisterNode( NameNode::m_TypeId);

   

    if(status != MS::kSuccess)

        return status;

   

    return status;

}

////

////--------

MStatus NameNode::initialize()

{

   

    /////------create string input

    //setup

    MFnTypedAttribute tAttr;

    MFnStringData fnStrData;

    MObject defaultStr;

   

    defaultStr = fnStrData.create( " Great Day " );

    m_aString = tAttr.create("string", "str", MFnData::kString, defaultStr );

    //

   

    //set properties

    CHECK_MSTATUS(tAttr.setArray(true)); //added multi options

    CHECK_MSTATUS(tAttr.setInternal(true)); //so we can track changes to it

    CHECK_MSTATUS(tAttr.setKeyable(false));

    CHECK_MSTATUS(tAttr.setStorable(true)); //used almost always if want to save it in Maya scene

    CHECK_MSTATUS(tAttr.setReadable(true));

    CHECK_MSTATUS(tAttr.setWritable(true));

    //CHECK_MSTATUS( tAttr.setWritable(true) )

   

    //add it

    addAttribute(m_aString);

    /////------

   

   

   

    /////------create float output

    //setup

    MFnNumericAttribute nAttr;

    m_aOutput = nAttr.create("ouput","o", MFnNumericData::kFloat, 0.0f);

    //

   

    //set properties

    nAttr.setDefault(0.0f);

    nAttr.setKeyable(false);  //i think this could be keyed since it is a float

    nAttr.setStorable(true);

    nAttr.setReadable(true);

    nAttr.setWritable(false);//important this is an output we dont want to write to it

   

    //add it

    addAttribute(m_aOutput);

    /////------

   

   

    //finishing

    //whenever input changes output should be evaluated

    attributeAffects(m_aString, m_aOutput);

   

   

    return MS::kSuccess; //assumes if went through to here everything was good

}

MStatus NameNode::compute( const MPlug& plug, MDataBlock& data)

{

    //make maya not compute if output not requested

    if( (plug != m_aOutput) && (plug.parent() != m_aOutput) )

        return MS::kUnknownParameter;

   

    //NO COMPUTATIONS SINCE THIS IS A STORAGE ONLY NODE SO NOTHING HERE :)

   

   

    //make maya not revevaluate plug

    data.setClean( plug );

   

    return MS::kSuccess;

}