Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Wire two objects together with an expression

Add an expression that reads one object’s length from another, recompute, then change the input and watch the dependent value follow.

Generated page. tools/userdocs.py writes it from tools/workflows/02_parametric_recompute.py. Edit the workflow, not this page; python3 tools/userdocs.py --check fails when the two disagree.

Requires: the FreeCAD facade; no workbench module and no GPU.

1. Two objects, one dependency

Expressions are evaluated on recompute, not on assignment. The document records the dependency edge, so it can order the objects without the script doing so.

doc = FreeCAD.newDocument("Parametric")

source = doc.addObject("App::FeaturePython", "Source")
source.addProperty("App::PropertyLength", "Length", "Base", "Input length")
source.Length = "10 mm"

derived = doc.addObject("App::FeaturePython", "Derived")
derived.addProperty("App::PropertyLength", "Result", "Base", "Computed")
derived.setExpression("Result", "Source.Length * 2")
doc.recompute()
Read backValue
Source.Length10 mm
Derived.Result20 mm
Derived.State[“Up-to-date”]

2. Change the input

Assigning to the source does not touch Derived; the next recompute does, because the dependency is stored in the document.

source.Length = "15 mm"
doc.recompute()
Read backValue
Source.Length15 mm
Derived.Result30 mm

3. Read the expression back

The expression is part of the document, not of the Python object.

expr = derived.ExpressionEngine

ExpressionEngine

[
  [
    "Result",
    "Source.Length * 2"
  ]
]

4. Close the document

FreeCAD.closeDocument('Parametric')
Read backValue
Open documents[]