MYPLUGINOBJECT to a BaseObject



  • On 27/08/2013 at 11:23, xxxxxxxx wrote:

    User Information:
    Cinema 4D Version:   R14 
    Platform:   Windows  ;   
    Language(s) :     C++  ;

    ---------
       Hi Folks,

    how do I turn something like this:

      
    // Below = reference to my ObjectData plugin from inside   
    // another DescriptionToolData plugin. The reference is valid.   
    MYPLUGINOBJECT MyPlugin;   
    

    to a BaseObject? As In:

      
    BaseObject *obj = (BaseObject* )MyPlugin;   
    // I'm sure the above isn't right,   
    // though it does compile!   
    

    How is this done properly?

    WP.



  • On 27/08/2013 at 11:30, xxxxxxxx wrote:

    Is MYPLUGINOBJECT a subclass of ObjectData? Then to create a new object

    BaseObject\* obj = BaseObject::Alloc(THE_PLUGINID_OF_MYPLUGINOBJECT);
    

    or to retrieve the BaseObject* linked to the ObjectData instance

    MYPLUGINOBJECT\* plugin_obj = // ...
    BaseObject\* obj = (BaseObject\* ) plugin_obj->Get();
    
    
    // assuming
    class MYPLUGINOBJECT : public ObjectData 
    

    It is important to understand that an instance of ObjectData is not the same as an
    instance of BaseObject. The BaseObject is a wrapper for ObjectData instances.

    ObjectData\* data = (ObjectData\* ) obj->GetNodeData();
    BaseObject\* obj_2 = (BaseObject\* ) data->Get();
    
    
    obj == obj_2
    obj != data
    

    -Niklas



  • On 27/08/2013 at 12:25, xxxxxxxx wrote:

    Thanks Niklas,

    I was wishing to "retrieve the BaseObject" link, so you're second way was what I was seeking!

      
    BaseObject* obj = (BaseObject* ) plugin_obj->Get();   
    

    WP.



  • On 28/08/2013 at 00:47, xxxxxxxx wrote:

    Just as a completion to what Niklas wrote, I recommend using static_cast instead of the C-Style cast. It has the nice property that first of all it will throw a compiling error if the cast is not valid and it can be identified in code much better because it will be color-coded.



  • On 28/08/2013 at 07:35, xxxxxxxx wrote:

    Howdy,

    If I may ask, what is the reason for doing?:

    MYPLUGINOBJECT MyPlugin;
    

    Adios,
    Cactus Dan



  • On 28/08/2013 at 08:17, xxxxxxxx wrote:

    Hi Dan,

    I'm using it at the class-level of a tool plugin so it can be passed around in various functions to retrieve the node data (does that make sense?).

    WP.

    EDIT: come to think of it, I may have gone in a small circle with this...



  • On 28/08/2013 at 09:30, xxxxxxxx wrote:

    Howdy,

    Yes, that makes perfect sense, but the way I would do it would be to only use a pointer as a member variable in the ToolData class:

    MYPLUGINOBJECT* MyPlugin;
    

    Then in the function where you need to assign an address to the pointer:

    BaseObject *op = doc->GetActiveObject(); // or the BaseObject pointer passed to the ToolData function
    MyPlugin = static_cast<MYPLUGINOBJECT*>(op->GetNodeData());
    if(!MyPlugin) return FALSE; // exit if the cast failed
    

    This way you won't need to pass it around to other functions and you can simply check to make sure it is valid as the first line of each function:

    if(!MyPlugin) return FALSE; // exit if the pointer is NULL
    

    EDIT:
    Don't forget to initialize the pointer in ToolData::InitTool() :

    MyPlugin = NULL;
    

    Adios,
    Cactus Dan


Log in to reply