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

    Setting an object to the target section of a Transform Constraint Tag

    Cinema 4D SDK
    2023 python windows
    3
    4
    947
    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.
    • E
      EugeneNUA
      last edited by EugeneNUA

      Hi everyone!

      I'm trying to set up the constraint tag, so this will pick the movement from another object. I need to do it strictly with Transform Constraint Tag.

      I was playing around with code and different parameters, but one is driving me crazy, I just can't figure out how to set an object as a target. Likewise, I used SearchObject, I got GUID nothing is going to help.

      I digged through the forums and found out that the UI ID of the target matters (10001), but I find no samples how to use the ID properly in Python scripts. Appreciate any help!

      I work on Windows 11
      Cinema 4D 2023.2.2

      import c4d
      from c4d import gui
      
      doc = c4d.documents.GetActiveDocument()
      
      target_obj = doc.SearchObject("Test")
      
      
      def addConstraint():
          objects = doc.GetActiveObjects(1)
          print(objects)
          my_obj = doc.SearchObject("Test")
          
          for obj in objects:
              tag = obj.MakeTag(c4d.Tcaconstraint)
              print(f"Your tag: {tag}")
              my_obj = doc.SearchObject("Test")
              tag()[c4d.ID_CA_CONSTRAINT_TAG_PSR] = True
              tag()[c4d.ID_CA_CONSTRAINT_TAG_PSR_LINK] = my_obj
              tag()[c4d.ID_CA_CONSTRAINT_TAG_PSR_P_OFFSET] = c4d.Vector(5,0,0)
              tag()[c4d.ID_CA_CONSTRAINT_TAG_PSR_MAINTAIN] = True
              tag.SetName("Boo")
              c4d.EventAdd()
      addConstraint()
      
      ferdinandF 1 Reply Last reply Reply Quote 0
      • ferdinandF
        ferdinand @EugeneNUA
        last edited by ferdinand

        Hello @EugeneNUA,

        Welcome to the Plugin Café forum and the Cinema 4D development community, it is great to have you with us!

        Getting Started

        Before creating your next postings, we would recommend making yourself accustomed with our Forum and Support Guidelines, as they line out details about the Maxon SDK Group support procedures. Of special importance are:

        • Support Procedures: Scope of Support: Lines out the things we will do and what we will not do.
        • Support Procedures: Confidential Data: Most questions should be accompanied by code but code cannot always be shared publicly. This section explains how to share code confidentially with Maxon.
        • Forum Structure and Features: Lines out how the forum works.
        • Structure of a Question: Lines out how to ask a good technical question. It is not mandatory to follow this exactly, but you should follow the idea of keeping things short and mentioning your primary question in a clear manner.

        About your First Question

        You mostly got it right, your major oversight was that you assumed the link field to be a static parameter (which it is not). Find out more on how to discover parameter IDs in Python Console Manual: Drag and Drop. Find my example attached below.

        Cheers,
        Ferdinand

        Result:
        07afea20-fe1e-4c3d-bd29-de867c309b58-image.png

        Code:

        """Adds a constraint tag with a PSR transform targeting an object named 'link' to the currently
        selected object.
        
        Must be run as a Script Manager script.
        """
        
        import c4d
        
        doc: c4d.documents.BaseDocument # The active document
        op: c4d.BaseObject # The selected object in #doc, can be #None.
        
        def main() -> None:
            """
            """
            # Search for the object to link in the constraint.
            link: c4d.BaseObject = doc.SearchObject("link")
            if not op or not link:
                print ("Please select an object and provide an object named 'link'.")
                return
        
            tag: c4d.BaseTag = op.MakeTag(c4d.Tcaconstraint)
            if not tag:
                raise MemoryError("Could not allocate tag.")
            
            tag[c4d.ID_CA_CONSTRAINT_TAG_PSR] = True
            # ID_CA_CONSTRAINT_TAG_PSR_LINK is just a stride, i.e., offset, not an actual ID as a constraint
            # can have multiple links and the IDs must be therefore dynamic. If you only want one link 
            # (or even multiple) I would just hardcode the dynamic ID (10001 will be present on the freshly 
            # initialized tag). See
            #   https://developers.maxon.net/docs/py/2024_0_0/manuals/manual_py_console.html#drag-and-drop
            # for how I found out that 10001 is the relevant ID here.
            tag[10001] = link
            tag[c4d.ID_CA_CONSTRAINT_TAG_PSR_P_OFFSET] = c4d.Vector(5,0,0)
            tag[c4d.ID_CA_CONSTRAINT_TAG_PSR_MAINTAIN] = True
        
            c4d.EventAdd()
        
        
        if __name__ == "__main__":
            main()
        

        MAXON SDK Specialist
        developers.maxon.net

        E 1 Reply Last reply Reply Quote 1
        • E
          EugeneNUA @ferdinand
          last edited by

          Hello! @ferdinand

          Proud to be a part of the community.

          Wow, how close the solution to the problem was, but I couldn't find any mention of it anywhere.

          Thank you so much for the ready-made solution and quick help!

          1 Reply Last reply Reply Quote 0
          • S
            SmetK
            last edited by

            @ferdinand Just ran into the same problem. Thank you for the solution!

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