spline distance as a dynamic Text Spline

"""
Python-Tag für Cinema 4D 2026.3
Abstand zwischen erstem und letztem Punkt eines Splines in cm
-> dynamisch als Text im Text-Spline (auch während Animation / Rendering).

Einrichtung:
  1. Tag auf das Text-Spline-Objekt legen (Rechtsklick > Programmier-Tags > Python).
  2. Am Tag zwei Benutzerdaten anlegen (Benutzerdaten > Benutzerdaten hinzufügen):
       ID 1: Typ "Link"     Name "Spline"
       ID 2: Typ "Integer"  Name "Nachkommastellen"  (z. B. 1)
  3. Deinen Spline in das Link-Feld ziehen.
  4. Diesen Code in den Tag einfügen.
"""
import c4d

# Umrechnung Dokument-Einheit -> cm
TO_CM = {
    c4d.DOCUMENT_UNIT_KM:    100000.0,
    c4d.DOCUMENT_UNIT_M:     100.0,
    c4d.DOCUMENT_UNIT_CM:    1.0,
    c4d.DOCUMENT_UNIT_MM:    0.1,
    c4d.DOCUMENT_UNIT_MICRO: 0.0001,
    c4d.DOCUMENT_UNIT_NM:    0.0000001,
    c4d.DOCUMENT_UNIT_MILE:  160934.4,
    c4d.DOCUMENT_UNIT_YARD:  91.44,
    c4d.DOCUMENT_UNIT_FOOT:  30.48,
    c4d.DOCUMENT_UNIT_INCH:  2.54,
}

DEZIMAL_KOMMA = True   # True = "12,5 cm", False = "12.5 cm"
SUFFIX = " cm"


def get_userdata(tag, idx, default=None):
    try:
        val = tag[c4d.ID_USERDATA, idx]
        return default if val is None else val
    except Exception:
        return default


def main():
    text_obj = op.GetObject()
    if text_obj is None:
        return

    spline = get_userdata(op, 1)
    if spline is None:
        return
    decimals = max(0, int(get_userdata(op, 2, 1)))

    # Parametrische / deformierte Splines in echte Punkte umwandeln
    pts_obj = spline if spline.CheckType(c4d.Opoint) else spline.GetRealSpline()
    if pts_obj is None or pts_obj.GetPointCount() < 2:
        return

    mg = spline.GetMg()
    p_a = mg * pts_obj.GetPoint(0)
    p_b = mg * pts_obj.GetPoint(pts_obj.GetPointCount() - 1)
    dist = (p_b - p_a).GetLength()   # Weltabstand in Szeneneinheiten

    # Projektskalierung berücksichtigen -> echte cm
    scale, unit = doc[c4d.DOCUMENT_DOCUNIT].GetUnitScale()
    dist_cm = dist * scale * TO_CM.get(unit, 1.0)

    txt = "{:.{d}f}".format(dist_cm, d=decimals)
    if DEZIMAL_KOMMA:
        txt = txt.replace(".", ",")
    txt += SUFFIX

    # Nur schreiben, wenn sich etwas ändert (verhindert Endlos-Updates)
    if text_obj[c4d.PRIM_TEXT_TEXT] != txt:
        text_obj[c4d.PRIM_TEXT_TEXT] = txt

Setup guide: spline distance as a dynamic Text Spline (Cinema 4D 2026.3)

  1. Add the Python tag. Right-click your Text Spline object › Programming Tags › Python.
  2. Add User Data to the Python tag (not to the spline). Select the tag, then go to User Data › Add User Data… and create the two entries in this order so the IDs are correct: ID 1 – “Spline”
    • Data Type: Link
    • Interface: Link
    • Optional: drag a spline object into the Accept field so the link only takes splines.
    ID 2 – “Decimals”
    • Data Type: Integer
    • Interface: Integer (or Integer Slider)
    • Default 1, Min 0, Max 4
  3. Link your spline. Drag the 2-point spline from the Object Manager into the “Spline” link field on the tag.
  4. Paste the code. Open the tag’s code editor, replace everything with the contents of spline_abstand_tag.py, and click Compile or close the editor.
  5. Done. The text now shows the distance between the first and last spline point and updates on every frame, including during rendering.

Notes

  • The distance is measured in world coordinates, so moving, scaling or animating the spline (including PLA) is handled.
  • The output is always in real centimeters, even if the project scale is set to m or mm.
  • Decimal separator: set DEZIMAL_KOMMA = False at the top of the script to get 12.5 cm instead of 12,5 cm.
  • If the text lags one frame behind (for example when the points are driven by constraints), set the Python tag’s Priority to Generators or higher.
  • You can style the font, size and alignment normally on the Text Spline; the script only changes the text content.

Weitere Beiträge