Updating ObjectData plugin only on timeline change



  • At the moment my objectData plugin uses GetVirtualObjects() to update. It is calling a lot of my code multiple times even when I move the view. I'm fairly new to all this but I want to find a more efficient way to update my plugin. I only want it to update when the timeline changes forward or backward a frame in the timeline. How would I do this?
    Cheers mates for any thoughts here.



  • Hi @glasses welcome in the plugincafe community.

    Before starting, here few rules to follow to post new topics (I've Setup and moved your topic in the correct category)

    Regarding your question, I would like to ask for your code, since normally the whole object data shouldn't be rebuilt for each redraw if you handle cache correctly.

    Cheers,
    Maxime.



  • You probably don't handle caching yet.

    The simplest way of doing that is:

    def GetVirtualObjects(self, op, hh):
        dirty = op.CheckCache(hh) or op.IsDirty(c4d.DIRTY_DATA)
        if dirty is False:
            return op.GetCache(hh)
    

    In this case, the already built cache will be returned, except if either there is no cache built yet, or if op is dirty at DIRTY_DATA.

    If you want to update only on time changes, you want to memorise the current time, and compare the memorised time to the current time (before memorising it, of course). If they're equal, return the cache.

    class myGenerator(plugins.ObjectData):
        def __init__(self):
            self.lastTime = c4d.BaseTime()
    
        def GetVirtualObjects(self, op, hh):
            dirty = op.CheckCache(hh)
    
            doc = op.GetDocument()
            currentTime = doc.GetTime()
            dirty = dirty or (currentTime != self.lastTime)
    
            if dirty is False:
                return op.GetCache(hh)
    
            self.lastTime = currentTime
    
            # Do your generator stuff
            # ...
    

    Greetings,
    Frank



  • @fwilleke80 thanks for your inputs.

    As a side note if you check only for the op dirty state with (dirty = op.CheckCache(hh) or op.IsDirty(c4d.DIRTY_DATA)) it's preferred to use self.SetOptimizeCache(True)in Python since it will optimize it drastically. And should be enough for the user to not rebuild everything at each redraw and will be way more optimized than checking for dirty manually in python each time.
    See ObjectData OptimizeCache.

    Cheers,
    Maxime.



  • @m_adam Right, I forgot about that shortcut.



  • Fantastic, Thank you for the suggestions, ObjectDataOptimizeCache(true) does what I need.


Log in to reply