Anatomy of a shading node plug-in
 
 
 

Maya Shading node plug-ins include the header file <maya/MPxNode.h> and then derive from the class MPxNode. While this command has a rich set of methods, only a few are actually necessary to create a working shading node.

Constructor

Initializes elements of the new class itself.

Destructor

Deletes anything created by the class.

Creator

This static method is responsible for actually creating instances of your new class (which is derived from MPxNode). When you register a new object you are actually registering its creator() method which Maya can then call to allocate a new instance of an object. In virtually all cases, is should look like:

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

initializePlugin/uninitializePlugin

The first of these methods is called by Maya when the plug-in is loaded. Its purpose is to create an instance of the MFnPlugin class (initialized with the MObject passed to the routine) and call register methods in that class to inform Maya what it is capable of doing.

ImportantBoth initializePlugin() and uninitializePlugin() must be present in all plug-ins. If both or either is absent the plug-in will not be loaded.

initialize

All the attributes of your new node are declared as static MObject members of the derived class. The initialize method is responsible for making MFnAttribute calls to actually provide the type information on the attributes. In addition, it sets default values, ranges etc. Like the creator function, this is a static method on the class, and will only be called once by Maya.

Id String

One of the required attributes of your node has to be of the type MTypeID. This maps to Maya’s internal IFF flag, and must be unique. The value of this attribute is set in the MTypeID constructor.

For local node testing, you can use any identifier between 0x00000000 and 0x0007ffff, but for any node that you plan to use for permanent purposes, you should get a universally unique id from Autodesk Support. They will assign you a unique range that you can then manage on your own

compute method

This is just like the compute method for an internal dependency node. It is passed a Data Handle to a Data Block and is responsible for extracting its input attributes from the datablock in order to compute the new values of the requested output attributes.