Working with OPs in Python
The main class type describing any Operator is the base OP Class. You will need a reference to one of these to do anything. There are two global operator objects are always available (except for in the Textport):
merefers to the operator that is currently being evaluated or executed. For example, when executing a script,
merefers to the containing DAT. When evaluating an expression,
merefers to the containing operator.
rootrefers to the top level component
To get references to other OPs (for example, a node named
'wave1' sitting next to the node
'constant1') the most common functions to use are:
ops(), for example
op() returns a single OP object, while
ops returns a (possibly empty) list of OPs.
They are described in td Module.
These functions search for operators from the current component, so both relative and absolute paths are supported.
The current component is defined as: The OP that
me is inside.
Note that the OP Class itself, also contains an
ops() method. In this case, nodes are searched from the OP.
me.op('..') will always return its own parent, while
op('..') will return the parent of the current component.
If you are typing a python expression in a parameter of a node
'constant1', and you wish to get a reference to
'wave1', you would type
If you are in a script you can assign this reference to a variable for easier repeated access.
n = op('wave1')
In this case
op() will search relative to the DAT that is executing the script.
An OP also has a
parent() method that can be use to get the parent COMP of it.
If you are putting a Python statement in a parameter of a COMP and want to refer to a child of that COMP, you can use the
op() method for the OP, which is available as
me in the parameters.
TIP: To find out quickly what members and methods you have access to for any node, select that node and on its parameter dialog, click the Python Help icon. You will go the wiki for the python classes for that node. There you can find out what info you can get about a node, and what methods are available for it. The documentation can also be arrived at by right clicking on the node and selecting "Python Help..." from the menu.
Place down a new Text DAT, ensure its parameter flag is set to Python, and enter the following script:
n = op('/project1') m = n.ops('text*') for a in m: print(a.name)
After running the script,
n is assigned the results of the global function, while
m is assigned results relative to
Some useful members and methods of an OP object, are:
These are described in OP Class.
Notice the last attribute,
parent() is a function. It takes an optional argument specifying how far up the parent chain to climb.
To see how they are used in practice, put down a new Text DAT, ensure its Parameter Language parameter is set to Python and enter the following code:
print('i am ', me) print('child of ', parent()) print('grandchild of ', parent(2)) print('root children:') k = root.children for r in k: print(r)
The resulting details will be found in the textport.
Common Python Tasksedit
|Creating an OP (Sphere SOP)|
|Creating a named OP|
|Copying OPs (Nodes)|
|Deleting an OP|
|Renaming an OP|
|Changing an OP's type|
|Changing multiple OPs' types|
|Setting an OP's comment|
|Changing an OP's parameter|
|Changing an OP's parameter
with more than 1 value
|Pulsing a parameter value|
|Cooking an OP|
|Saving an OP's data to a file|
|Changing an OP's Render and Display Flags on|
|Loading a .tox file into a COMP|
|Wiring operators together|
|Clicking gadgets (panel components)|
|Querying another OP's parameter|
|Querying a parameter in the same OP|
|Getting Info CHOP channels from an OP
without cooking it
|Getting an OP's parent|
|Getting an OP's grand-parent|
|Getting an OP's name|
|Getting an OP's parent's name|
|Getting digits of an OP's name in its parameters|
|Getting digits of an OP's parent's
name in its parameters
|Getting digits of another OP's name|
|Getting an OP's type|
|getting a unique random number each frame|
|getting a unique random number per numbered operator|
|Checking for an OP's existence|
|Getting the number of children of a COMP|
|Getting the number of inputs of a multi-input OP|
|Getting Info CHOP channels from an OP, width is a member|
|Conditional "if" in one line of a parameter|
|Conditional "if" alternative|
|Convert space separated string to a list|
|Conditional list comprehension|
|Test operator type|
|Test operator family|
"Absolute Time" is the time since you started your TouchDesigner process, not counting when your power button was off (top bar).
|Retrieving a node's local frame number|
|Retrieving a node's local time in seconds|
|Retrieving absolute time in frames|
|Retrieving absolute time in seconds|
Storage in Pythonedit
Storage is the preferred way to work with persistent global data in Python, since it can store anything data type.
|Setting a value in storage of a component
|Getting a value from storage|
|Directly access the storage dictionary|
|Directly access a key in the storage dictionary|
|Test if a key exists in the storage dictionary|
Variables are always text strings.
|Setting a value|
|Setting a Root Variable|
|Setting a Component Variable
at the current component
|Setting a Component Variable
at another component
|Setting a Path Variable||Set the Path Variable parameter of any parent component and use |