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

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()

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:

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

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!