Optimized Python Expressions
Starting with the 2018.20000 series of builds, we have incorporated a custom Python expression execution engine into TouchDesigner. This engine will parse Python expressions that are in parameters. Assuming the members and methods used in that expression fall into the range of members and methods the engine is aware of, it will evaluate the expression without having to use Python's engine. This generally results in a the expression evaluating in about 1/4 of the time than it does if it was evaluated through the regular Python engine.
The goal of this feature isn't to evaluate every parameter expression using this engine, but to evaluate the most commonly used expressions. You can determine if an expression is evaluated using this engine by holding the mouse over the parameter name. It will show either (Optimized) or (Unoptimized) in the popup that will show up.
If an expression ever ends up causing an error, the engine will stop being used for that expression and the Python engine will be used instead. If the expression is changed, or on the next time the file is loaded, TouchDesigner will once again try to use the engine to evaluate the parameter.
What can the engine evaluate
When a Python expression is parsed it is broken up into tokens. A token can be for example a constant boolean value
True or a constant string. Tokens are also the methods, members and variable names that are part of an expression. The engine has a set of tokens it recognizes, and if the expression contains only those tokens, then it can evaluate the expression. If there are any tokens it does not recognize in the expression, then it will not try to evaluate the expression and the regular Python engine will be used.
As long as the expression contains only tokens recognized by the engine, the there is no limit to how large the expression and be or how complex the expression can be.
For example this expression can be evaluated by the optimized expression engine:
op(‘geo1’).par.tx + math.max(parent().par.tx + op(‘speed1’), 10) / int(op('table1')['header', 5])
The types of supported tokens are:
- the 'me' variable
- Booleans (True, False)
- Comparison operators:
- Unary operators:
- Boolean operators:
- Binary operators, for numerical and string operations:
- single line if expressions
- Global td module functions:
- Tdu Module functions:
- AbsTime Class methods and members:
- OP Class methods and members:
- TimeCOMP Class methods and members:
- CHOP Class methods and members:
operator to access a Channel Class object.
- TOP Class methods and members:
- Panel Class methods and members:
- Par Class methods and members:
- automatic casting to int/float/string
- Channel Class methods and members:
operator to get a single sample from a CHOP channel.
- automatic casting
- Cell Class methods and members:
[r,c]operator to get a cell from a DAT table.
- automatic casting
- Python list class methods and members:
- Python math module functions:
- Python global functions:
getattr()(In limited cases)
- Project Class methods and members: (Builds 2018.22040 or later)
- SOP Class methods and members (Builds 2018.40000 or later):
- Primitive SOP the
me.inputPrimPrim Class has these members optimized (Builds 2018.40000 or later):
- Point SOP has these special cases optimized (Builds 2018.40000 or later):
- Includes inputPoint2, inputColor2, inputNormal2 and inputTexture2 as well.
- Vector Class (Builds 2018.40000 or later):
x, y and zmembers
- Position Class (Builds 2018.40000 or later):
x, y and zmembers
- Color Class (Builds 2018.40000 or later):
r, g, b and amembers
- Panel Class (Builds 2018.40000 or later):
- Access to Panel Values.
- PanelValue Class
- Automatic casting
- expressionCHOP Class methods and members (Builds 2018.40000 or later):
- waveCHOP Class methods and members (Builds 2018.40000 or later):
Python Math Module constants and functions (Builds 2018.40000 or later):