The Interactive & Immersive HQ

Python Tags in TouchDesigner

There are a ton of ways that Python is integrated with operators in TouchDesigner. We all have likely referenced channel values, DAT tables, or controlled our operators with Python. As you work on larger projects, the organizational features of Python start to become incredibly useful. Today we’ll look at how you can tag operators in Python with specific keywords, then search your whole project for those tagged operators.

Setting up a sample

Before diving into tags, let us quickly setup an example project. You can download the project at the bottom of the post for reference as well! What I’m going to do is make a handful of Container COMPs nested within each other to simulate a complex project structure with lots of networks in it. Then I’m going to create a bunch of Timer CHOPs and scatter them around the structure. We can imagine that these Timer CHOPs might be in charge of our state machines, sequencing our show, running animations, or anything really. This will look something like this:

Now remember, you don’t need to worry too much about the specifics of this network. It’s a random bunch of timers and containers that simulate what a project might look like. In our demo here, what we’re going to do is imagine that a few of these timers are really important timers that control our whole application. Because we want to access them regularly, we need an easy way to do that in Python.

How you might do this without tags

There’s a few ways you might do this without tags. Those could be:

  1. Making a Table DAT full of paths that you can easily lookup
  2. Keeping all the paths of the operators in Python storage (not a bad idea!)
  3. Writing a bunch of lines at the beginning of every script that says op(‘path_here’) for every single operator
  4. Use a combination of Global OP shortcuts and then a secondary OP search (also not a bad idea!)

Ideas 1 and 3 are generally considered suboptimal methods of doing this. They’ll be slow, hard to maintain, and not great to look at over and over again. Ideas 2 and 4 aren’t bad ideas either, but they have specific places they will work and specifically places where they probably won’t work as well. For example, although I usually recommend idea 4 as a general best practice for project architecture, if you’re building a system where you need to track down a ton of individual operators (maybe to do something like bypass them or reset them or etc), even the global op shortcut code starts to become incredibly tedious. So how do we do this with tags?

Adding tags to operators

The first step to using tags is actually tagging operators. In our example, what I’m going to do is find a few of my Timer CHOPs and then I’ll give them a tag named timers. Below I have one of my Timer CHOPs selected and I can see it’s parameter window. To add a tag:

  1. I’ll start by clicking on the pencil icon in the top right corner
  2. Then I’ll type my tag word into the string field that appears under the pencil icon
  3. Finally I’ll hit Add Tag to the right of it.

This process looks like this:

Once we add a tag, you’ll also notice the pencil icon has changed to indicate that the operator has an active tag. You’ll see a white rectangle of box underneath the pencil:

Great, so we’ve successfully assigned our first tag! I’ll go through my example project and randomly assign only a few of these timers tags to my Timer CHOPs. That way we can use some nice Python to only fetch us the Timer CHOPs that have the tag.

Finding tags without Python

If you’d like to find all the operators that you’ve added our timers tag to without creating a Python script, there are useful filters on the OP Find DAT. What I can do is:

  1. Create an OP Find DAT on the top level of my project
  2. Turn off the Limit max depth parameter,
  3. Set the Tags parameter on the Filters page to timers
  4. Turn on the Path toggle on the Columns page of parameters

This will give me a quick table that will search my whole project for the operators with the tags and put their paths in an easily accessible place for me. This looks like this:

Great! So even if you’re just starting out with Python or wanting to create a reference table without Python, you have an easy to turn to option. But what if you want to do similar inside of a Python script or extension?

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.

Accessing tags with Python

If I wanted to do the same thing as my OP Find DAT from within a Python script, it isn’t too difficult using the findChildren() method. The findChildren() method belongs to all component operators (COMPs) and comes equipped to already include tags in the search. If we make a Text DAT inside of /project1/ in our project, what we can do is have our findChildren() method work on the project1 container, essentially acting as a project wide search since all of the operators we added are inside of project1. This looks like this:

Now inside of this Text DAT we’ll add our few lines of code as below:

op_list = parent().findChildren(tags=['timers'])

for i in op_list:
	print(i)

What this does is:

  1. Create a variable named op_list
  2. For the value of op_list, we first go to the parent() object of the Text DAT, which is our project1 container.
  3. Then we use the findChildren() function on project1 and tell it to find any child operator which has the tag timers. A list gets returned to our op_list variable of all of the operators found.
  4. With a list in hand, we can do a quick for loop on the list and print out every item inside of it, which will return to us the same results as our OP Find DAT, as we can see below

That’s it! We just needed one line to do all the searching and return us a list of results. From here you could do perform any critical functionality within the for loop, or store the list for later usage.

Deleting old tags

Deleting tags is just as easy as adding them.

  1. Start by clicking the pencil icon to reveal all the tags
  2. Hover over the tag you want to delete
  3. You’ll see an x icon appear on the right of the tag and clicking on it will delete the tag

This looks like this:

Wrap up

Whether you plan on using tags inside of scripts or with OP Find DATs, they’re a great way to keep track of specific operators or groups of operators inside of projects. As projects get larger and larger, it’s common that we’ll often lose track of which operator in which part of the project is doing a specific thing. With the help of tags, finding and keeping track of these critical operators can become much easier. Enjoy!