Python in TouchDesigner

Learning TouchDesigner can feel a bit intimidating if you happen to be new to programming. For artists who are new to the idea of working in a programming environment it’s easy to feel overwhelmed. That’s okay, in fact it’s great. There’s lots to learn here in the TouchDesigner universe, and one of the primary ingredients for you to start with is getting a solid handle on Python 3. It’s easy to feel like maybe GLSL or C++ should come first, but if you’re looking to get a handle on building full projects, then you need a solid scripting language. Python, as a high level language, is fairly human readable, and once you get the hang of it is fun to write. We can use Python for a number of different operations in Touch, and it is hands down the best way to improve your touch programming quickly.

Python according to Wikipedia
Python according to Python.org

These series of examples will look at basic python concepts through the lens of the TouchDesigner programmer. Cross domain transfer, the act of moving knowledge from one conceptual frame to another, is often very challenging. For that very reason here we’ll look at how to start with python in the context of TouchDesigner, what we might do with it, and why it’s so very important.



Learning Resources

Learning how to Print

Core Concepts

  • Using the text port
  • Running scripts
  • print()
  • strings, integers, floats, and booleans
  • printing and joining
  • simple substitution in strings

What are Variables

Core Concepts

  • Understanding variables in Python (different form what we mean with TouchDesigner variables)
  • Referencing by using variables in a script
  • simple scripting
  • simple preset recall
  • Writing Python References

Writing References in Python

Core Concepts

  • Python expressions as references
  • Syntax
  • Accessing CHOPs
  • Accessing DATs
  • How to read the Python help on the Derivative Wiki

Logic

Core Concepts

  • Simple Logic constructs in Python and in TouchDesigner
  • If, And, Or
  • >= , <= , == , !=
  • if, else, elif
  • Scripting Logic and references
  • Comparing values
  • The Channel Class

Data Structures | Lists

Core Concepts

  • Lists – a structure and a concept
  • Creating lists – syntax and structure
  • Retrieving items from a list – Syntax
  • Adding items to a list .append() and .extend()
  • Lists of Lists
  • Why Lists matter in TouchDesigner
  • The Channel Class – seeing CHOPs as lists
  • The Point Class – thinking of geometry as lists
  • The COMP Class and .findChildren() – pulling apart returned lists
  • More about how to read the TouchDesigner wiki

Data Structures | Dictionaries

Core Concepts

  • Dictionaries – a structure and a concept
  • Looking at Dictionaries and Lists side by side
  • What are key value pairs
  • Retrieving values from dictionaries
  • Retrieving .keys() and .values()
  • Adding items to dictionaries
  • Nested data structures
  • A better text formatting approach with .format() (a big thank you to Willy Nolan for setting me on the right path with text formatting in Python 3)
  • A practical look at dictionaries in TouchDesigner
  • Nested data structures – dictionaries in dictionaries
  • Using Dictionaries as a preset structure
  • Using Python to set parameters
  • Using Python variables in scripts to generalize our code

An Intro to Functions

Core Concepts

  • Functions as a concept
  • Anatomy of a function
  • Writing functions
  • Calling functions
  • Returning values
  • Passing arguments

Executes

Core Concepts

  • Using the methods in the Execute DATs
  • Optimization
  • Writing simple functions
  • Programatic thinking for operation execution

OP Class

Core Concepts

  • Understanding classes in Python
  • A closer look at the Op Class as an example
  • Some Examples of how to use the op class

For Loops

Core Concepts

  • Understanding loops
  • Faster programming through abstraction
  • List Comprehensions
  • Using .pars()

For Loops in Dictionaries

Core Concepts

  • Using loops with dictionaries
  • Dictionaries to hold presets
  • Loops and .pars()
  • Using the Op Class to set up machines based on a config file
  • Using JSON

Modules

Core Concepts

  • Understanding modules on demand
  • Writing functions as modules on demand
  • Using Doc Strings in Python
  • Code documentation
  • Calling Doc Strings
  • For loops and Doc Strings
  • Practical example – building a logger for your project
  • Using Local > Modules

Extensions

Core Concepts

  • Another brief look at extensions
  • Comparison between modules on demand and extensions
  • Modular approach in Class building
  • Planning for the future when coding

 


Examples Hosted on GitHub