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

    How to change the value here using python

    Cinema 4D SDK
    2024 python
    2
    8
    1.1k
    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.
    • F
      freeter_e
      last edited by

      1.jpg

      I don't know where to get this object() ( I mean this simulation scene object)

      ferdinandF 1 Reply Last reply Reply Quote 0
      • ferdinandF
        ferdinand @freeter_e
        last edited by ferdinand

        Hey @freeter_e,

        Thank you for reaching out to us. It is shown in the very screen you show us: object() returns a BaseDocument, specifically the active one. This is the bit weird way how the script log 'writes' its 'scripts'.

        The simulation scene parameters are just part of the data container of a document, you can find the documented here.

        Cheers,
        Ferdinand

        import c4d
        
        doc: c4d.documents.BaseDocument  # The currently active document.
        op: c4d.BaseObject | None  # The primary selected object in `doc`. Can be `None`.
        
        def main() -> None:
            """Called by Cinema 4D when the script is being executed.
            """
            # Other than done in your script, we do not have to grab the active document. It is
            # already predefined in each Script Manager script.
            
            # Read and write parameters of the document.
            isCpuSim: bool = doc[c4d.PBDSCENE_DEVICE] == c4d.PBDSCENE_DEVICE_CPU
            step: int = doc[c4d.PBDSCENE_SUBSTEPS]
            print(f"This scene is simulated with the {'CPU' if isCpuSim else 'GPU'} and {step} substeps.")
            
            doc[c4d.PBDSCENE_SUBSTEPS] = 45
            
        if __name__ == '__main__':
            main()
        

        MAXON SDK Specialist
        developers.maxon.net

        F 2 Replies Last reply Reply Quote 1
        • F
          freeter_e @ferdinand
          last edited by

          @ferdinand said in How to change the value here using python:

          doc[c4d.PBDSCENE_SUBSTEPS] = 45

          @ferdinand
          It's not work.
          substeps is still 20

          1 Reply Last reply Reply Quote 0
          • F
            freeter_e @ferdinand
            last edited by

            doc[c4d.PBDSCENE_SUBSTEPS] = 45
            

            Execute the script and the result feels like it changes the value here
            2.jpg

            ferdinandF 1 Reply Last reply Reply Quote 0
            • ferdinandF
              ferdinand @freeter_e
              last edited by

              Hello @freeter_e,

              please have a look at Support Procedures: Asking Questions. We understand that there can be language barriers but we cannot communicate in the this one-liner postings. Please consolidate your postings and make yourself understood, otherwise we will not be able to help you.

              My script changes the simulation step size of the active document, because that is what you showed in your script. The simulation scene which is shown/embedded in the document, is an object that is actually attached to a scene hook of the document. The user cannot physically interact with it.

              Cheers,
              Ferdinand

              MAXON SDK Specialist
              developers.maxon.net

              1 Reply Last reply Reply Quote 0
              • F
                freeter_e
                last edited by

                sorry @ferdinand

                Like in the first half of the video, this can be done in the "bullet".
                I would like to realize it in "simulation" as well.

                So I want to know
                bullet → doc.FindSceneHook(180000100)
                simulation → ?????

                I'm sorry for the trouble. I hope it's understandable.

                ferdinandF 1 Reply Last reply Reply Quote 0
                • ferdinandF
                  ferdinand @freeter_e
                  last edited by ferdinand

                  Hey @freeter_e,

                  okay, now I understand what you want to do. In this case you have to go indeed over the hook. Find a code example below, which explains the a bit techy subject.

                  Cheers,
                  Ferdinand

                  """Demonstrates how to access the global default simulation scene of the simulation hook.
                  """
                  
                  import c4d
                  
                  doc: c4d.documents.BaseDocument  # The currently active document.
                  op: c4d.BaseObject | None  # The primary selected object in `doc`. Can be `None`.
                  
                  c4d.SHsimulation: int = 1057220 # The ID of the simulation hook.
                  
                  def main() -> None:
                      """Called by Cinema 4D when the script is being executed.
                      """
                      # We must get the branches of the simulation hook and there the object branch to get hold of the
                      # global default simulation scene. A branch is a way to express a generic relation between two 
                      # nodes, it is Cinema's way of expressing entity relationships beyond a simple parent-child 
                      # hierarchy. The actual relationship is between a node (GeListNode) and a branch head 
                      # (GeListhHead) which then can have traditional children. In a simplified manner, a scene can 
                      # look like this:
                      #
                      #   Document 'MyScene'
                      #   ├── Objects Branch (Obase)
                      #   │   ├── Object1
                      #   │   │   └── Tags Branch (Tbase)
                      #   │   │       ├── Tag1
                      #   │   │       └── Tag2
                      #   │   ├── Object2
                      #   │   └── Object3
                      #   ├── Materials Branch (Mbase)
                      #   │   ├── Material1
                      #   │   │   └── Shaders Branch (Xbase)
                      #   │   │       ├── Shader1
                      #   │   │       └── Shader2
                      #   │   ├── Material2
                      #   │   └── Material3
                      #   └── Scene Hooks Branch (ID_BS_HOOK)
                      #       └── Simulation Hook
                      #          └── Object Branch (Obase)
                      #              └── Simulation Scene
                  
                      # Get the simulation hook of the active document, this will reach for us into the scene hooks
                      # branch and return the simulation hook if it exists.
                      simulationHook: c4d.BaseList2D | None = doc.FindSceneHook(c4d.SHsimulation)
                      if not simulationHook:
                          raise RuntimeError("Cannot find the simulation hook.")
                  
                      # Now we must pry out the object branch from the simulation hook. The branches of a node can
                      # be accessed via the GetBranchInfo() method. This method returns a list of dictionaries, each
                      # dictionary contains information about a branch, the relevant information for us is the branch
                      # head and the branch ID. The branch ID is a unique identifier for the branch, the branch head is
                      # the GeListHead object that represents the branch. 
                      branches: list[dict] = simulationHook.GetBranchInfo()
                      data: dict | None = next(n for n in branches if n["id"] == c4d.Obase) # We look for the object branch.
                      if not data:
                          raise RuntimeError("Encountered malformed simulation hook with missing object branch.")
                      
                      # A branch is guaranteed to have have a branch head (so we can just blindly access it), but a
                      # branch head may not have any children (should not be the case for the simulation hook).
                      simulationScene: c4d.BaseObject | None = data["head"].GetFirst()
                      if not simulationScene:
                          raise RuntimeError("Encountered malformed simulation hook with missing object branch children.")
                  
                      print (simulationScene)
                  
                      simulationScene[c4d.PBDSCENE_SUBSTEPS] = 45
                      c4d.EventAdd()
                  
                  if __name__ == '__main__':
                      main()
                  
                  

                  MAXON SDK Specialist
                  developers.maxon.net

                  1 Reply Last reply Reply Quote 1
                  • F
                    freeter_e
                    last edited by

                    @ferdinand
                    Thanks so much for such a detailed response.
                    Learned a lot.
                    My problem is solved.
                    Thank you very much.

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