This tutorial covers essential Python expressions in TouchDesigner, teaching you how to use dynamic expressions for parameter control and animation.

Getting Started with Python in TouchDesigner

If you’re already familiar with Python, Derivative recommends checking out the User Guide’s Python category, specifically the Quick Start Tutorial and Introduction to Python in TouchDesigner. For beginners, this tutorial covers the most common expressions you’ll use.

Time-Based Expressions

absTime.frame

The absTime.frame expression returns the absolute amount of time that has elapsed since TouchDesigner started running, expressed in frames.

Example: Add a Geometry component and use absTime.frame in a rotation parameter to create continuous animation.

Mathematical operations: You can perform math directly in parameter fields:

  • absTime.frame * 0.25 - Slow down the animation
  • absTime.frame * 2 - Speed up the animation

absTime.seconds

Similar to absTime.frame, but returns time in seconds instead of frames.

Example: Add a Constant CHOP with a Trail CHOP connected to visualize the value over time.

Using Modulus for Looping

The modulus operator (%) creates repeating cycles:

absTime.seconds % 2

This expression counts from 0 to 2, then resets back to 0, creating a simple ramp that loops every 2 seconds. This is incredibly useful for building cyclical animations and effects.

Path and Parent Expressions

parent.name

Returns the name of the parent operator.

Example: Inside a Container, add a Text component and use parent.name in the text parameter. If you rename the parent container, the text automatically updates.

parent (Full Path)

Using parent without .name returns the complete path to the parent operator.

Example: parent might return /project1/container1/myGreatComp3

This matches the address shown in the pane bar, giving you the full path to the operator’s location.

parent(n) - Accessing Ancestors

You can access grandparents and further ancestors by passing a number:

  • parent or parent(1) - Returns the immediate parent
  • parent(2) - Returns the grandparent
  • parent(2).name - Returns just the grandparent’s name

parent.digits

Returns only the numeric portion of the parent’s name as an integer.

Example: If the parent is named myGreatComp6, parent.digits returns 6.

The TDU Module

The tdu module provides utility functions for common operations in TouchDesigner.

tdu.digits()

Extracts numeric values from any string:

tdu.digits("helloWorld10")  # Returns 10
tdu.digits("myComp19")      # Returns 19

This is extremely useful when extracting numeric values from strings or paths, a common operation when building dynamic systems.

Summary

These essential Python expressions form the foundation for dynamic parameter control in TouchDesigner:

ExpressionReturns
absTime.frameElapsed time in frames
absTime.secondsElapsed time in seconds
value % nValue cycling from 0 to n
parent.nameParent operator’s name
parentParent operator’s full path
parent(n)Ancestor n levels up
parent.digitsNumeric portion of parent’s name
tdu.digits(string)Numeric portion of any string

These expressions enable you to create dynamic, responsive projects where parameters automatically update based on time, hierarchy, and other factors.

本教程介绍TouchDesigner中的基本Python表达式,教你如何使用动态表达式进行参数控制和动画制作。

在TouchDesigner中开始使用Python

如果你已经熟悉Python,Derivative建议查看用户指南中的Python类别,特别是快速入门教程和TouchDesigner中的Python介绍。对于初学者,本教程涵盖了最常用的表达式。

基于时间的表达式

absTime.frame

absTime.frame表达式返回自TouchDesigner开始运行以来经过的绝对时间,以帧为单位表示。

示例: 添加一个Geometry组件,在旋转参数中使用absTime.frame来创建连续动画。

数学运算: 你可以直接在参数字段中进行数学运算:

  • absTime.frame * 0.25 - 减慢动画速度
  • absTime.frame * 2 - 加快动画速度

absTime.seconds

absTime.frame类似,但返回以秒为单位的时间而不是帧。

示例: 添加一个Constant CHOP并连接Trail CHOP来可视化随时间变化的值。

使用取模运算创建循环

取模运算符(%)创建重复循环:

absTime.seconds % 2

这个表达式从0计数到2,然后重置回0,创建一个每2秒循环一次的简单斜坡。这对于构建循环动画和效果非常有用。

路径和父级表达式

parent.name

返回父操作符的名称。

示例: 在Container内部,添加一个Text组件并在文本参数中使用parent.name。如果你重命名父容器,文本会自动更新。

parent(完整路径)

不带.name使用parent会返回父操作符的完整路径。

示例: parent可能返回/project1/container1/myGreatComp3

这与面板栏中显示的地址匹配,给你操作符位置的完整路径。

parent(n) - 访问祖先

你可以通过传递数字来访问祖父级及更远的祖先:

  • parentparent(1) - 返回直接父级
  • parent(2) - 返回祖父级
  • parent(2).name - 仅返回祖父级的名称

parent.digits

仅返回父级名称中的数字部分作为整数。

示例: 如果父级名为myGreatComp6parent.digits返回6

TDU模块

tdu模块提供TouchDesigner中常用操作的实用函数。

tdu.digits()

从任何字符串中提取数字值:

tdu.digits("helloWorld10")  # 返回 10
tdu.digits("myComp19")      # 返回 19

这在从字符串或路径中提取数字值时非常有用,这是构建动态系统时的常见操作。

总结

这些基本的Python表达式构成了TouchDesigner中动态参数控制的基础:

表达式返回值
absTime.frame以帧为单位的经过时间
absTime.seconds以秒为单位的经过时间
value % n从0到n循环的值
parent.name父操作符的名称
parent父操作符的完整路径
parent(n)向上n级的祖先
parent.digits父级名称的数字部分
tdu.digits(string)任何字符串的数字部分

这些表达式使你能够创建动态、响应式的项目,其中参数会根据时间、层次结构和其他因素自动更新。