NodeData::Init() / Alloc and Free Manual

Table of Contents

About

New instances of classic plugin classes are typically created using a static "Alloc" function. The initial values of this instance are defined in an "Init" function. When an instance is deleted, its "Free" function is called.

Note
The undo system will allocate new instances of a plugin class when a scene object is changed.
"Alloc"/"Free" functions should generally be used instead of class constructor/destructor, as they provide means to return an error if anything goes wrong.

Alloc

A static "Alloc" function creates a new instance of a plugin class. This "Alloc" function is referenced in the corresponding "Register" function (see General Plugin Information Manual).

// This is a plugin class with a static "Alloc" function:
class ObjectDataGenerator : public ObjectData
{
public:
static NodeData* Alloc(void)
{
return NewObjClear(ObjectDataGenerator);
}
};
// This is a "Register" function referencing that static "Alloc" function:
RegisterObjectPlugin(123456, "Example Generator", OBJECT_GENERATOR, ObjectDataGenerator::Alloc, "Ogenerator", nullptr, 0);
Bool RegisterObjectPlugin(Int32 id, const maxon::String &str, Int32 info, DataAllocator *g, const maxon::String &description, BaseBitmap *icon, Int32 disklevel)
Definition: c4d_nodedata.h:39
Definition: c4d_objectdata.h:169
#define OBJECT_GENERATOR
Generator object. Produces a polygonal or spline representation on its own. (e.g. primitive cube)
Definition: ge_prepass.h:938

Init

Each class derived from NodeData can implement NodeData::Init(). This "Init" function is called every time when an instance of the plugin class is allocated. In this "Init" function one can define the default values of parameters and internal data instead of using a class constructor.

// This example implements NodeData::Init() in a plugin class.
// It sets the default values of the plugin's parameters.
virtual Bool Init(GeListNode* node)
{
if (node == nullptr || !SUPER::Init(node))
return false;
// get the "real" object
BaseObject* const obj = static_cast<BaseObject*>(node);
// get data container
BaseContainer& data = obj->GetDataInstanceRef();
// set default parameter values
data.SetBool(EXAMPLE_GENERATOR_PARAMETER_BOOL, true);
data.SetInt32(EXAMPLE_GENERATOR_PARAMETER_VALUE, 123);
Definition: c4d_basecontainer.h:47
void SetBool(Int32 id, Bool b)
Definition: c4d_basecontainer.h:498
void SetInt32(Int32 id, Int32 l)
Definition: c4d_basecontainer.h:505
Definition: c4d_baseobject.h:225
Represents a C4DAtom that resides in a 4D list.
Definition: c4d_baselist.h:1831
PyObject * obj
Definition: complexobject.h:60
maxon::Bool Bool
Definition: ge_sys_math.h:55
struct _node node
Definition: node.h:10
Note
If NodeData::Init() returns false the object won't be created.
If an entity should be modified when it was created the best solution is to catch the MSG_MENUPREPARE message.
There's no guarantee NodeData::Init() is called only once. In fact it may very well be called multiple times.

Free

Each class derived from NodeData can implement NodeData::Free(). This function is called when an instance gets freed. Implement this function to free internal data.

virtual void Free(GeListNode* node)
{
DeleteMem(_memory);
GeListHead::Free(_branchHead);
}
static void Free(GeListHead *&v)
void DeleteMem(T *&p)
Definition: defaultallocator.h:257

Further Reading