Implementing a State Machine - Help me think like a TD Developer

Dear Touchdesigner community

I am fairly new to programming with TD, normally I code with openframeworks or Unity, so now things are different and I need your help.

Right now I am working on an interactive installation using the kinect sensor.
Some functionality that I have done before using normal code, now becomes a challenge for me, because I don’t know how to do it and most tutorial are about visual things.
My problem is more a logical task.

Here is what I am trying to do:

The application should recognize when a new user is detected by the kinect sensor. But it should only consider the user an active user if he is inside a certain area.
Only then the movements of the user’s joints are supposed to have an effect.

On top of that I would like the application to react differently depending on certain other things, like the duration of interaction, certain gestures ans so on.

Normally I would use a classical state machine and define conditions for the state transitions.
I would create a custom “User” object that has a state attribute, with every state leading to a distinct behaviour of the Application.

Can anyone please tell me if that is possible, and if so could point me into the right direction?

Thanks in advance - I am looking forward to learning from you :slight_smile:

Arne

Hi Arne - I’m also a TD beginner, so someone with more experience may have ideas for making a more Op-based TD state machine. However, I’ve done some similar state-machine work recently, and I think that just writing your state machine in Python is probably the quickest way to get going.

What you’d basically be doing is generating DATs or CHOPs from the Kinect sensor, then running a script op (DAT or CHOP, depending) with that information. That script will maintain your state machine and then write out whatever information you need to a CHOP/DAT, and then TD can use that to visualize the data. So, besides the language difference, your code would probably look similar to what you’ve done in OpenFrameworks.

So, my basic approach would be:
– Get all of your sensor data & decision-making data into a CHOP or two
– Feed that data into custom Python state machine in a script CHOP
– Your state machine code will output all data needed for visualization to a CHOP
– TD uses the generated data to visualize as desired

(You might also check out this state machine post by Matthew Ragan. His system doesn’t work quite as I’ve proposed, but he does a really great job, here as everywhere, of explaining exactly why he does things a certain way/)

Good luck, and please show us how it’s going when you get some things put together!

Hi Evan
Thank you very much for taking the time to answer my question in such detail. I had already tried several times to bring my code somehow meaningfully into the game, but I haven’t done it yet. I will tackle it right away and hope for the best.
You helped me a lot!

Hi tricksal,

you can use the node CHOP Kinect to analyse the datas and create your logic in few steps.

To define a “certain area” you can isolate a channel from Kinect by using CHOP select, use the CHOP Expression and with a simple python script create your IFexpression with single or multiple channels.
me.inputVal if me.inputVal => -0.5 and me.inputVal <= 0.5 else 0
In the same way, in order to have an incremental value based on the active presence of a user, isolate the channel by using CHOP select and then use a CHOP count, in its parameters set Increase Count While on and Resent Count to zero from OntoOff. The CHOP Count will produce an incremental number at the speed of 60 frames per second. You can remap the value by using CHOP Math, in the section Range.

You might also want to have a look to the CHOP Hold which is an easy way to keep on, hold values and release them.
You can also create your logic and hierarchy of users by using CHOP Reoder.

Basically you need to seprate channels and reordering them based on logcis, i suggest CHOP Select, Expression, Hold, Math, Merge and if you really need it Reoder.

Have fun! and if you need help there’s plaenty of ppl ready to help :slight_smile:
cheers,
Nima

Hi Nima,
thanks very much for your reply.
That is very useful information for me!

Right now I relied heavily on a Script CHOP and Table DATs to keep the data.
It is a longer process to adapt to the CHOP way of data handling.
Anyway thanks for your thoughts!
Arne

If you’re more familiar with python, and have worked with TouchDesigner python extensions before, the most powerful state machine solution I’ve found is integrating the python library Transitions. It takes a while to wrap your head around just the library, and even longer to figure out how to get it to play nicely inside of an extension, but to me the features are worth the effort of integration.

1 Like