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.pywrites it fromtools/workflows/02_parametric_recompute.py. Edit the workflow, not this page;python3 tools/userdocs.py --checkfails 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 back | Value |
|---|---|
| Source.Length | 10 mm |
| Derived.Result | 20 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 back | Value |
|---|---|
| Source.Length | 15 mm |
| Derived.Result | 30 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 back | Value |
|---|---|
| Open documents | [] |