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
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    trigger script when viewport camera is moved

    Cinema 4D SDK
    python 2023
    2
    3
    496
    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.
    • T
      thomas
      last edited by thomas

      Hey!

      I am working on a python plugin that sends renders to a webserver. It works well right now through a "render and send image" button in a dialog window the plugin creates.
      I would love to send images when the viewport camera is moved or something changes in the scene with a visual impact, with a little timeout.
      basically the same as the redshift IPR, it updates only when something relevant to the scene changes.

      ChatGPT helped me find one way to do this by listening to messages.

      class SceneChangeListener(c4d.plugins.MessageData):
          
          BASE_URL = "http://127.0.0.1:8000/inputs"  # Base part of the URL
      
          def __init__(self, plugin):
              self.last_render_time = 0
              self.plugin = plugin
      
          def CoreMessage(self, id, bc):
              # print(f"core message received: {id}")
              if id == c4d.EVMSG_CHANGE:
                  print(f"my message received:{id}")
                  current_time = time.time()
                  if current_time - self.last_render_time >= 0.6:  # 300ms delay
                      print(f"CoreMessage render triggered:{id}")
                      self.last_render_time = current_time
                      self.plugin.render(url=self.BASE_URL)
                      # Trigger your render function here
      
              return True
      

      this works but it gets triggered a lot for anything.

      Next step is filtering a bit by messages. Ideally only when something that changes the main view, or I listen to only the messages I want to respond to, such as material manager, timeline, camera moves.

      I played around with a few more messages I found here:
      https://developers.maxon.net/docs/py/2024_2_0/consts/MSG_C4DATOM_PLUGINS.html. None of the few I tried were really that useful.

      I guess these dont exist? Happy to try another approach.

      i_mazlovI 1 Reply Last reply Reply Quote 0
      • i_mazlovI
        i_mazlov @thomas
        last edited by

        Hi @thomas,

        You can use c4d.plugins.MessageData for creating the timer that would handle the "little timeout" part for you.

        For the editor camera being changed you can access if through the BaseDraw as shown in How to get the active editor/object camera.

        Generally speaking, I'd suggest using the SceneHookData plugin type and register it using the RegisterSceneHookPlugin function. This would give you more flexibility in checking different aspects of the document to treat them as a trigger for you to send data to the server (e.g. you could use EXECUTIONPRIORITY for plugging into directly where you need in the Scene Execution Pipeline, and the EXECUTIONFLAGS::CAMERAONLY could be something particularly of your interest). The downside is that scene hooks aren't exposed to python, so you need to develop C++ plugin to use it.

        Cheers,
        Ilia

        MAXON SDK Specialist
        developers.maxon.net

        1 Reply Last reply Reply Quote 0
        • T
          thomas
          last edited by

          @i_mazlov thanks so much!
          some super useful insights here. I think I will have eventually to move to C++

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