Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Updating ObjectData plugin only on timeline change

    Cinema 4D SDK
    r20 python
    3
    6
    883
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • G
      glasses
      last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • M
        m_adam
        last edited by

        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)

        • Q&A New Functionality
        • How to Post Questions

        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.

        MAXON SDK Specialist

        Development Blog, MAXON Registered Developer

        1 Reply Last reply Reply Quote 0
        • fwilleke80F
          fwilleke80
          last edited by fwilleke80

          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

          www.frankwilleke.de
          Only asking personal code questions here.

          1 Reply Last reply Reply Quote 0
          • M
            m_adam
            last edited by

            @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.

            MAXON SDK Specialist

            Development Blog, MAXON Registered Developer

            1 Reply Last reply Reply Quote 0
            • fwilleke80F
              fwilleke80
              last edited by

              @m_adam Right, I forgot about that shortcut.

              www.frankwilleke.de
              Only asking personal code questions here.

              1 Reply Last reply Reply Quote 0
              • G
                glasses
                last edited by

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

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post