Execute Button Comp with Chops

Hi Guys,
I’m playing since four with TD around and learnd a lot so far.
But now I ran into a problem I can’t solve by my own or with friends.

I want to toggle a Button via Chop on value change.
To be more exact: I want to execute the rotate Buttons (left or right) of the Kantan Mapper with the Chop thats matched a calculated rpm of a stepper motor.

So far a friend send me an answer from Chat GPT:


In TouchDesigner, triggering a button COMP using a CHOP is definitely possible, and there are a few ways to accomplish it depending on the exact behavior you want to trigger and how your network is set up. Here is a general approach you can take:
Select CHOP: Use a Select CHOP to isolate the channel from your CHOP network that you want to use to trigger the button. The channel should ideally be a toggle or a pulse that goes from 0 to 1 when you want the button to be “pressed”.

CHOP Execute DAT: You’ll need to use a CHOP Execute DAT that monitors the channel coming from your Select CHOP. Inside the CHOP Execute DAT, you can write a script in Python that will be executed whenever the channel value changes.

Python Scripting: Within the CHOP Execute DAT, you can write a Python script that triggers the button COMP. For example, if you want to trigger the button when the channel value goes from 0 to 1, you could use something like this:

def valueChange(channel, sampleIndex, val, prev):
    # val is the new value of the channel
    # prev is the previous value of the channel
    if val == 1 and prev == 0:
        # Fetch the Button COMP
        button = op('path_to_button_comp')
        # Simulate button press
        button.click()
    return

Implementing the Script: You would replace ‘path_to_button_comp’ with the actual path to your button COMP. The .click() method simulates a button click.

DAT to CHOP: If the button COMP is linked to a value in a DAT, such as a table DAT or script DAT, and that value is what actually triggers the rotation, you might need to write to this DAT instead of triggering the button COMP directly. This is usually done via the .run() method on the DAT or by modifying its .text or .par.value properties, depending on how the Kantan Mapper listens to button presses.

Monitoring Frequencies: If you want the button to trigger at the frequency of a CHOP, you would need to ensure the CHOP you’re using to trigger the button changes value at the frequency you desire.


Sounds like it could work, but I dont get it to work.

Can somebody help with that?

Hi @HannesRofall,

First off - you are on the right track - simulating button clicks on a CHOP channel’s value change can be done utilizing a CHOP Execute DAT. It actually already comes with the correct callback:

def onValueChange(channel, sampleIndex, val, prev):
	return

in here you could then follow the suggestion and click the button.

Now with KantanMapper, you could just go and find the Rotate Button and click that in the script. To find the button hover with your mouse over the button and hit F10 on your keyboard. This will take you straight to where the button is defined via a floating window.


So you could just specify the button path, perhaps relative to the CHOP Execute DAT:

def onValueChange(channel, sampleIndex, val, prev):
	rotRBtn = op('kantanMapper/ui/main/gadgets/rect/rotate/right')`
	rotRBtn.click()
	return

What you will encounter very quickly though is that the Shape has to be selected for this to work and the network location of the button might change depending on the type of shape.

So now we get into funny territory as we have to dig a bit through the code and building blocks of kantanMapper. Looking at where the shapes are stored, brings us to a DAT branch in kantanMapper/project with 2 DATs of interest: One is called “sorted” which can help us to identify the shape we want to manipulate and the other one is “fullPath” which can be seen as a mapping between shape id and the path it is stored in.

In my example here, I can see a rectangle shape being stored at /project1/kantanMapper/project/allShapes/item1 - with a bit of inside knowledge I can also say that the shape has a Extension that comes with a Rotate() function.
To rotate the specific shape you can:

shape = op('`kantanMapper/project/allShapes/item1')
shape.Rotate(rotate={'z':45}, delta=True)

This will rotate the shape in question by 45 degrees. By setting delta to False, you can specify the shapes final rotation.

Hope this helps
Markus