On 21/04/2016 at 13:24, xxxxxxxx wrote:
User Information:
Cinema 4D Version: 14
Platform: Mac ;
Language(s) : C++ ;
---------
Hello, I'm working with Object Plugin that has materials associated with it and that are added to the scene with MSG_MENU_PREPARE. I've been looking at Matthias' code from this thread https://plugincafe.maxon.net/topic/5051/4975_question and I copied the code below.
I want to modify it so that each instance of the plugin has it's own material, instead of sharing a single material. If I call the plugin again then the new instance of the plugin gets a new material, but if the plugin is copied and pasted within the same scene file it doesn't create another material and both objects share the same already existing material. Where should I start looking to accomplish this?
class MySky : public ObjectData
{
public:
virtual Bool Init(GeListNode *node);
virtual Bool Read(GeListNode *node, HyperFile *hf, LONG level);
virtual Bool Write(GeListNode *node, HyperFile *hf);
virtual Bool CopyTo(NodeData *dest, GeListNode *snode, GeListNode *dnode, LONG flags, AliasTrans *trn);
virtual Bool Message(GeListNode *node, LONG type, void *data);
virtual BaseObject *GetVirtualObjects(BaseObject *op, HierarchyHelp *hh);
static NodeData *Alloc(void) { return gNew MySky; }
//the BaseLink containing the link to the material
//must be handled through CopyTo, Read, Write
AutoAlloc<BaseLink> matlink;
};
Bool MySky::Init(GeListNode *node)
{
if (!matlink) return FALSE;
return TRUE;
}
Bool MySky::Read(GeListNode *node, HyperFile *hf, LONG level)
{
return matlink->Read(hf);
}
Bool MySky::Write(GeListNode *node, HyperFile *hf)
{
return matlink->Write(hf);
}
Bool MySky::CopyTo(NodeData *dest, GeListNode *snode, GeListNode *dnode, LONG flags, AliasTrans *trn)
{
return matlink->CopyTo(((MySky* )dest)->matlink, flags, trn);
}
Bool MySky::Message(GeListNode *node, LONG type, void *data)
{
switch (type)
{
//create the material after the menu entry of the object has been called
//save the material pointer as BaseLink
case MSG_MENUPREPARE:
{
BaseDocument *doc = (BaseDocument* )data;
Material *mat = (Material* )matlink->GetLink(doc, Mmaterial);
if (!mat)
{
mat = Material::Alloc();
if (!mat) return FALSE;
doc->InsertMaterial(mat, NULL, NULL);
matlink->SetLink(mat);
}
}
break;
//must be checked because CINEMA 4D can have several documents open
//objects and their materials can be copied across these docuemnts
//the MarkMaterials structure contains the old and new material
case MSG_MULTI_MARKMATERIALS:
{
Material* mat;
MarkMaterials* mm = (MarkMaterials* )data;
BaseDocument* doc = GetDocument(node);
if (mm)
{
mat = (Material* )matlink->GetLink(doc, Mmaterial);
if (mat == mm->omat)
{
doc->StartUndo();
doc->AddUndo(UNDO_CHANGE_SMALL, node);
matlink->SetLink(mm->nmat);
doc->EndUndo();
}
}
else
{
mat = (Material* )matlink->GetLink(doc, Mmaterial);
if (mat) mat->SetBit(BIT_MATMARK);
}
}
break;
}
return TRUE;
}
BaseObject* MySky::GetVirtualObjects(BaseObject *op, HierarchyHelp *hh)
{
Bool dirty = op->CheckCache(hh) || op->IsDirty(DIRTY_DATA);
if (!dirty) return op->GetCache(hh);
BaseObject *sky = NULL;
TextureTag *ttag = NULL;
sky = BaseObject::Alloc(Osky);
if (!sky) goto Error;
//create the texture tag for the material
ttag = (TextureTag* )sky->MakeTag(Ttexture, NULL);
if (!ttag) goto Error;
//set the material
ttag->SetMaterial((Material* )matlink->GetLink(hh->GetDocument(), Mmaterial));
return sky;
Error:
blDelete(sky);
blDelete(ttag);
return NULL;
}