How to use python and if, case stament in touchdesigner

hello.
I am trying to receive and interpret the value with Touch Designer through UDP communication.
I am not an English speaker so I used a translator. Thank you for your understanding.
A signal was received using ‘udpin’ of ‘dat’, and one ‘index’ was received using ‘select’.
After that, I used ‘convert’ to separate the spaces. There are a total of 4 accepted data signals. If [a,b,c,d], when a is 1, I want to receive the signal from b, and when b is 1, I want to receive the signal from c, d.
Ultimately, it is very difficult to move ‘circle’ or ‘rectangle’ by using ‘dat to’ for c and d using if or case type grammar…

I also looked at python-related materials written by matthewragan. Although if statements and def functions are used there, they are not of much help to me because the final result is output as ‘print’. This is because I want to set a condition in python, change the result to ‘dat to’, and then apply it to the x, y position values ​​of ‘circle’ etc. as the signal of ‘chop’. It doesn’t have to be this way, and it doesn’t have to be Python. I would like to implement what I described. So, I’d like to get some helpful information… I need a lot of help because the language I use (Korean) is quite lacking in resources. thank you

Hey @ADC_BEEMO, I wasn’t exactly sure what you are trying to accomplish, but I made you this simple example - maybe it can get you up and running with parsing of udp data :slight_smile:
quick_udp_example.1.toe (7.0 KB)

Thank you so much for your kindness!
However, I have already done this, but I do not know the next process.

What I want is this.

I would like to receive people’s coordinates and make the circle move according to the people.
(In reality, it is not a circle, but an effect. It is an effect made with circle, so I will explain it as circle.)
Each signal is coming in through UDP, and I want to use this.


Here’s a description of the data I receive.

For example, 0,1,512,512
If the value comes in, the first person is moving and is at (512,512).

I expressed the signal displayed as DAT as ‘CHOP’ using DATTO, and classified the x and y values ​​using ‘select’.

In my code I put ‘chop’ of ‘math1’ into ‘center’ of ‘circle top’.
And because there will be about 10 people coming in.
I was thinking of connecting 10 ‘circle tops’ and grouping them with ‘comp’.


It’s a bit of a stupid method, but…

However, the update of data sent by the sensor is fast and
10 ‘circle’ results in duplicate values.
In other words, different coordinate values ​​do not occur depending on the person.
As you can see, even if I create a lot of ‘circletops’, the circles will be in the same position, so visually it will look like one circle is moving.

0,1,512,512
1,1,432,535
.
.
.

So I think,
Only when the index value is 0, ‘circle1top’ receives the x, y values ​​and applies them to the ‘center’ value,

When the index value is 1, ‘circle2top’ receives the x, y values ​​and applies them to the ‘center’ value…
The only idea I can think of is repeating 10 like this.

Case statements or other functions…

Thank you for reading this rather long and somewhat stupid post…
Just in case, I’m sending here the code I practiced.
You can ignore about the ‘fan’ or ‘expression’ at the end, it is my practice!

cirlcefollowspeople.toe (7.2 KB)

Aha, I see, I was just guessing where the problem was originally, now I think I have a better idea. Here is an example on how to approach this. There are many possible solutions but this one seemed easy and quick to me. Hope this helps.
quick_udp_example.2.toe (7.1 KB)

Thank you so much for reading this long content!
But this was also one of the things I had already tried haha.

What I’m wondering is, is it not possible to solve what I described in ‘circle top’?
Is there a way to do ‘instacing’ on ‘circle top’?

I’m also thinking about ‘instancing’ it using ‘geometry’ like you did.
However, I should not connect the x,y values ​​to ‘circle sop’, but to ‘circle top’.

This is because my effect starts from ‘circle top’.
Here is the overall structure of my code.

Enter the x, y coordinate values ​​of the incoming signal into the ‘center value’ of ‘circletop’.
After that, it goes through ‘opticalFlow’ to give effects such as ‘feedback’,
Next, the final ‘top’ of the effect was added to the ‘colormap’ of ‘constant mat’.
Then, specify ‘constant mat’ as the ‘material’ of ‘geometry’ and add ‘light’ and ‘camera’.
Ultimately, final output is ‘render top’.
circlesfollowpeople_structure.toe (11.7 KB)
Is what I’m trying to do inherently impossible? If instancing doesn’t work, is there another way?
Thank you for your reply…it is very helpful.

Of course, there is no need for ‘geometry’ in the last part if I use ‘circle top’.
However, since ‘instancing’ is in ‘geometry’, I practiced to connect it somehow.

Sure, you can do it that way too, but I think it won’t be as efficient as instancing geometry. Since you can apply post-processing effects on Render TOP the same way you do it with Circle TOP, there shouldn’t be a problem in this matter. (In case you would like to apply different post-processing per circle, geometry instancing setup would be more tricky, but not impossible.) Anyway, in case you might want to use Circle TOPs, here is possible solution. :slight_smile:
quick_udp_example.3.toe (8.3 KB)

1 Like

Thank you for your kind reply. After trying various methods, I realized that I could just instantiate the circle top directly!
When I tried the two methods you suggested, I found that the method of ‘instancing’ the circle top was the most effective.

def onReceive(dat, rowIndex, message, bytes, peer):
	index, x, y = message.split(':')
	if index in [cell.val for cell in op('table1').col('index')]:
		op('table1')[index, 'x'] = x
		op('table1')[index, 'y'] = y
	else:
		op('table1').appendRow([index, x, y])

if index in [cell.val for cell in op(‘table1’).col(‘index’)]:

The conditional statement checks whether the index column of the table contains the value stored in the index variable.
If the index column of the table has a value stored in the index variable, the corresponding row in the table is updated.

else:

The code adds a new row to the table if the table’s index column does not have a value stored in the index variable.

I was studying the Python code you wrote, but I still have a lot to implement, so I’m having a headache. Here is a part that I still don’t understand.

I’m wondering how the names “index”, “x”, and “y” are in the first row of “table”. I understand how the values are updated, but how are the names inserted?

The names in first row of table (“index”, “x”, and “y”) were added manually before I wrote UDP callbacks. I just edited the table - writing this header row. Since this table is saved within the toe file, there is no need to create it again - as soon as you load the file, this table contains values it was saved with (allowing UDP callbacks to work with it). :slight_smile: