Understanding Iteration/Looping

I’m struggling a little bit with how to “think in TouchDesigner” after coming from a OOP/Processing background. In the file linked below (apparently new accounts can’t attach files), I have 10 icospheres which I use as the underlying geometry/instancing for a bunch of spheres that get placed on each of the points of the icosphere.

I calculate the positions of the spheres by using a Timeline chop to drive a chopExecute DAT every frame. This script loops through all 10 icospheres’ positions and generates a table of xyz coordinates for the spheres based on the positional data from the original icosphere point data.

While this works, I’m not sure whether this would actually be the “right” or most efficient way to do this in TD? So I have a couple questions:

  1. Is there a way to “for loop” a set of chop data?
  2. Is there a more efficient and/or “TD” way of accomplishing this same task?

Thanks for your time.

iterating_instances.toe

Hi @s4frn - welcome to the forum. You should be able to attach files after you’ve been on the forum for a stretch. This discus forum tool is a change from the previous one and you “earn” trust on here by reading posts and participating in discussion. Long story short - there was some spam that kept appearing and I think this is a measure to help address some of that.

When it comes to thinking in TouchDesigner, it is a little different than other OPP environments, though it should start to make sense in OPP terms as you work with it over time.

Some considerations in working with Touch. When possible avoid making changes to any SOPs. SOPS are currently calculated on the CPU and will bottleneck at some point. There isn’t a hard and fast rule for this, as there are often pieces of your model that you want to be animated - but when possible it’s better to perform any transformation operations on the geometry COMP. The geometry COMP object is already copied to the GPU, and operations there are significantly faster (especially at scale) than those on a SOP.

Operating on DATs is another typically slow operation - all table contents are technically strings to TouchDesigner. While the typecasting is pretty intelligent, when it comes to speed this will also bottleneck you at some point.

Your example is interesting since because of the more complex shape of your instances. I’m gonna see if I can work out an elegant way to tackle this one. In the meantime, for a shape that doesn’t have an orientation this style of approach should work - this might also help you get started:
base_instancing_instances.tox (4.1 KB)

1 Like

Hey @raganmd,

Thanks so much for taking the time to respond. My gut instinct was telling me that this probably wasn’t the right way to tackle the problem given what I know thus far about TD, so thank you for confirming my thoughts and giving me some good feedback to think about.

I’ve taken a look at your example and both options make sense to me, so I’m going to do some playing to see if I can find a good solution to what I’m trying to do.

Hey @s4frn,

Figured out a small order of operations error that was making me CRAZY. Woah. The change to consider is to re-order your samples after shuffling them to ensure that they’re actually in order for the Math CHOP to work correctly.

Flattening your location data by shuffling (splitting each sample) will mean that your math CHOP will perform the calculations you want almost like if it were a for loop. Some quick examples here. Consider that have some number of points on a line that you’d like to transform. We might start with a line SOP that we’ve converted to a CHOP, and with a constant CHOP. Set to combine CHOPs, add - the math chop will add the first input to all samples of the second input:

Killer. Let’s say, however we want to use that single line, but end up with copies that have been transformed in two separate directions. The kind of instances of instances idea in your toe file. Here we can fake that with two different constant CHOPs, joined (as if it were a piece of geometry). If we flatten that with a shuffle CHOP, ensure that it’s order is correctly output as tx ty tz, then feed it into our math CHOP, something interesting happens. What we end up with is a set of channels that matches the structure of our first input - but applying the math to our second input accordingly. Almost the for loop you’re looking for:

The trick is that we then need to shuffle them again to ensure that they’re correctly ordered for instancing. The same idea should get you pretty close with your example… the only wobble is rotation. That’s some more math CHOPs to correctly calculate the new position of our instances - very doable, but might end up looking like a bit of wires here and there. I tried to put together some more clear examples here to help:

base_instances_of_instances.tox (8.5 KB)

I’ll try to find some time to work out rotation - I love a clean example of these ideas, it always makes returning to them easier. Hope this helps.

Hey @raganmd,

Thank you so much for taking the time to put this example together for me. I wanted to take some time to really integrate/understand what was happening here before I responded and it definitely took me a while to wrap my head around what was going on here. You also happened to answer another question I had on my mind about tracking/following objects in your icosphere example, so double thanks :pray:

For anyone stumbling on this in the future: I was struggling to understand Matthew’s explanation until I added a chopToDat for most of the Chop’s in the “base_math_chop_principles” COMP (in his provided example) so that I could see the actual values in tabular form and track how they were shifting/changing across the network. Highly recommended.

It’s a very different way of thinking coming from a loops background, but it’s starting to make sense :metal:

1 Like