Skip to content

Capture variables in expressions #12

@serguei-k

Description

@serguei-k

It would be great to capture parts of an expression as a variable that could be reused inline.

Here is a motivating example:

# get plane normal
normal = eval_expression('axis(plane.matrix, 1)', 'proj')

# project vector to plane
eval_expression('src.translate - ({normal} * dot(src.translate, {normal}))'.format(normal=normal), 'proj')

The normal gets used twice in expression, therefore it needs to be evaluated separately if it is to be reused. What if we could do something like this instead:

eval_expression('src.translate - (normal * dot(src.translate, axis(plane.matrix, 1)->normal))', 'proj')

Exact syntax is TBD.
One thing to consider is that capture would need to happen on the first use of the expression segment as per operation order.

Alternative proposal is to detect duplicate sections by comparing the AST nodes and reuse output internally, so you would still write:

eval_expression('src.translate - (axis(plane.matrix, 1) * dot(src.translate, axis(plane.matrix, 1)))', 'proj')

Feedback welcomed!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions