Optimize GetAndCheckHierarchyClone



  • On 12/12/2017 at 11:14, xxxxxxxx wrote:

    Hello plugincafe! :)
    I'm trying to create object plug that will take its child and perform some manipulations with its clone.
    This is how the code looks like:

    ** _    def GetVirtualObjects(self, op, hh) :**
    ** _        first = op.GetDown()
    **
    ** _        if not first: return_**
    ** _
    **
    ** _        dic = op.GetAndCheckHierarchyClone(hh, first, c4d.HIERARCHYCLONEFLAGS_ASPOLY, False)
    **
    ** _
    **
    ** _        if not dic["dirty"] and not op.IsDirty(c4d.DIRTY_DATA) and op.GetCache() :
    **
    ** _            return op.GetCache().GetClone()_**
    ** _
    **
    ** _        obj = dic["clone"]
    **
    ** _        **
    ** _        #Modeling commands with obj...
    **

    I read in docs that GetAndCheckHierarchyClone will generate new cache if something is changed but it constantly generates new cache even if nothing is changed in object plugin or in its child object.
    I want to generate new cache only if plugin object or if its child is changed.
    Is it possible to optimize this code or I need to use something else?

    Cheers! :)

    -Merk



  • On 12/12/2017 at 21:56, xxxxxxxx wrote:

    Previously I was using this method:

    1. **    def GetVirtualObjects(self, op, hh) :**
    2. **        **
    3. **        child = op.GetDown()**
    4. **        if not child:**
    5. **            return**
    6. **
      **
    7. **        dirty = op.CheckCache(hh) or op.IsDirty(c4d.DIRTY_DATA) or child.IsDirty(c4d.DIRTYFLAGS_DATA) or child().IsDirty(c4d.DIRTYFLAGS_MATRIX) or op.GetDown() == None or op.IsDirty(c4d.DIRTYFLAGS_CHILDREN) or child.GetDown() is not None and child.GetDown().IsDirty(c4d.DIRTY_DATA)**
    8. **        **
    9. **        if dirty is False: **
    10. **         op.GetDown().Touch()**
    11. **         return op.GetCache(hh)**
    12. **         **
    13. **     if dirty is True and op.GetDown() : **
    14. **     op.GetDown().Touch()**
    15. **    **
    16. **        if op.GetDown() is None:**
    17. **            return c4d.BaseObject(c4d.Onull)**
    18. **        obj = op.GetDown()**
    19. **
      **
    20. ** #Modeling**

    This was my first attempt at creating object plugin but the code is not clear at all. I think there should be a better way of optimizing cache.



  • On 13/12/2017 at 22:43, xxxxxxxx wrote:

    Download plugin: https://www.dropbox.com/s/mcr652kkt08zx96/Test Plugin 0.3.rar?dl=0



  • On 14/12/2017 at 00:24, xxxxxxxx wrote:

    Hi Merk, thanks for writing us.

    With reference to your question consider that when you are using the BaseObject::GetAndCheckHierarchyClone() and the dirtiness is false, you're expected to return the "clone" item from the dictionary returned by GetAndCheckHierarchyClone() and not the cache obtained by BaseObject::GetCache() as in the code below:

      
          orig = op.GetDown()  
          if orig is None:  
              return c4d.BaseObject(c4d.Onull)  
      
          gch = op.GetAndCheckHierarchyClone(hh, orig, c4d.HIERARCHYCLONEFLAGS_ASPOLY, True)  
          gchDirty = gch["dirty"]  
          gchClone = gch["clone"]  
      
          if not gchDirty:  
              return gchClone  
      
          # do something else by using the cloned children  
          # ...  
    

    It also relevant to note that using the GetCache() in combination with GetAndCheckHierarchyClone() will invalidate the cache object resulting in a non-alive cache whose existence should be forced using the C4DAtom::GetClone().

    Best, Riccardo



  • On 14/12/2017 at 00:46, xxxxxxxx wrote:

    Thanks for explanation :)
    I have another question regarding this code.
    Is it possible to perform modeling operations with gchClone ?
    for example, extrude polygons.

    I tried this code:

    1. **    def GetVirtualObjects(self, op, hh) :**
    2. **        orig = op.GetDown()**
    3. **        if orig is None:**
    4. **            return c4d.BaseObject(c4d.Onull)**
    5. **        gch = op.GetAndCheckHierarchyClone(hh, orig, c4d.HIERARCHYCLONEFLAGS_ASPOLY, True)**
    6. **        gchDirty = gch["dirty"]**
    7. **        gchClone = gch["clone"]**
    8. **        if not gchDirty:**
    9. **            return gchClone**
    10. **        #Extrude**
    11. **        settings = c4d.BaseContainer()**
    12. **        settings[c4d.MDATA_EXTRUDE_OFFSET] = op[10000]**
    13. **        res = c4d.utils.SendModelingCommand(command = c4d.ID_MODELING_EXTRUDE_TOOL,**
    14. **                                        list = [gchClone],**
    15. **                                        mode = c4d.MODELINGCOMMANDMODE_POLYGONSELECTION,**
    16. **                                        bc = settings,**
    17. **                                        doc = op.GetDocument())**
    18. **        return gchClone**


  • On 14/12/2017 at 00:59, xxxxxxxx wrote:

    Sorry, I just noticed that allchildren is set to True :)


Log in to reply