The Interactive & Immersive HQ

TouchDesigner Python Tricks

Why are TouchDesigner Python tricks so useful?

Not to sound obvious, but all tricks are useful. Python tricks can be especially useful because they can keep you from doing going down a weird rabbit hole. Some of these tricks are really standard Python practices that just never make it into the repertoire of TouchDesigner developers. Most TouchDesigner developers learn Python inside of TouchDesigner. This means you learn essentials like data types, conditionals, then maybe you get into functions and some object oriented class stuff on the edge. But you often skim over some of the more powerful things like list comprehensions. So for a lot of the self-taught Python users in TouchDesigner I’ve explained a few really useful TouchDesigner Python tricks that you can use to speed up your workflows.

String Formatting

This one is the simplest of the bunch. Essentially it’s the new standard way or creating strings with variables scattered in the middle of the string. Previously, you may have seen lots of examples of this kind of string concatenation:

# we'll insert this word into our sentence
word_to_insert = "sentence"

# then this value will be inserted
value_to_insert = 5555

# all of our words and values are inserted using string concatenation, including need to convert the integer into a string
output_string = "This" + word_to_insert + "is composed of" + str(value_to_insert) + "pieces."

This is something common that needs to happen in projects. You’re given some kind of stock phrase, and you have to insert some new values into it. The problem with the method above, even though it is the way most beginners learn to do this, is that it’s very error prone and hard to read quickly. If you put a quotation mark in the wrong place or forget one, the Python code will likely error out.

So what’s the proper way to go about this? In Python 3 it’s become the new standard to do this kind of string building using the string.format() method.

It sounds more complicated than it is. Let’s look at the same example done again with string.format().

# set our variables again
word_to_insert = "so much"
value_to_insert = 9000

# make our output string here where you can place {} anywhere and deal with them in the format()
output_string = "This way is {} better then the other. Power level over {} for sure".format(word_to_insert, value_to_insert)

What happens here is that we leave a pair of squiggly brackets {} wherever we want to insert something, and then the brackets get matched in order to whatever I put in the .format() as arguments. So my first set of {} get replaced by word_to_insert, and my second set of {} get replaced by value_to_insert. This can scale as much as you like.

Cleaner? Check. Easier to read quickly? Check. Less prone to errors? Double check. Less extra code and worrying about data types? Check. This one is a no-brainer as far as I’m concerned. It’s simple and will keep your code clean and bug-free.

To learn Python string hacks, check out our YouTube tutorial:

Get Our 7 Core TouchDesigner Templates, FREE

We’re making our 7 core project file templates available – for free.

These templates shed light into the most useful and sometimes obtuse features of TouchDesigner.

They’re designed to be immediately applicable for the complete TouchDesigner beginner, while also providing inspiration for the advanced user.


Oof, setattr. What a feature. Why it isn’t in ever beginners playbook is a mystery. If you’ve ever thought to yourself “Why is it so hard to programmatically update operator parameters?” then the answer was that you didn’t know about setattr. So let’s take a common example. You have a Constant CHOP with 10 parameters. You want to be able to loop through all 10 in a Python script and update them. Sounds easy? Yup. Is it in practice? No, it’s basically an impossible in practice unless you know setattr. Here’s what would happen without setattr:

# lets give this a try...

# start our for loop here
for i in range(10):
# lets grab the operator
update_me = op('constant1')

# now lets grab the par
update_me.par.value0 # oh what, wait how do I update the 0 on the end of that??????

I basically see that exact scenario on the TouchDesigner forums and Discord all too often. The problem is that you can’t easily just slap a new string to the end of your update_me.par. already written…or is there? In comes setattr:

# take 2

# start our for loop here
for i in range(10):

# grab the operator
update_me = op('constant1')

# now lets add use some setattr magic
setattr(update_me.par, 'value{}'.format(i), 9000)

Clean? Double clean! Slick? Super slick. So let’s break down what’s going on here.

Essentially everything else is the same up until the setattr. We’re creating a for loop and getting a reference to our operator. Instead of trying to do some weird manipulation of the update_me.par.value0 like we might be tempted to do, we harness the power of setattr. It allows us to pass it three arguments. The first is the object we want to work with. In this case it’s the par class of the Constant CHOP (fancy name for the update_me.par part of the code). The second argument is a string name of an attribute you want to access (fancy name for the value name in this case). The final argument is the value you want to pass. It’s that easy. It’s easy to remember if you forget, just thing about this:

setattr(object, name, value)

Once you know setattr, you should get a tattoo of it, because it is really that useful when you’re programmatically trying to control complex state machines or data structures you might have built out in TouchDesigner. It’s easily one of the best TouchDesigner Python tricks.

Wrap up: TouchDesigner Python Tricks

These two TouchDesigner Python tricks aren’t particularly complicated but you can see why you might have missed them in your Python journeys. If you did a beginners course online (like I did many years ago) just so you could get going in TouchDesigner, you probably got just far enough to make TouchDesigner work. This means you missed out on all kinds of powerful Python skills that would make your more advanced TouchDesigner life a lot easier. The two we mentioned here — string.format() and setattr — are hugely useful but those aren’t the only ones out there. I mentioned list comprehensions at the beginning of the post, which are another great resource. Don’t be afraid to take a step back from TouchDesigner to beef up some of your Python chops with either more advanced tutorials or by asking around to Python experts you might know.