trigg with keypress

Hi!

Im trying to do something that should be really simple. I want to toggle between on and off (1 and 0) with a keyclick. I know I can use the keyboard in chop to get key inputs. But it only get the value when the key is pressed.

How do I get this simple task to work? I guess I can also do this with a chop execute but I cannot get the script to work.

Would be great with example how to buld this simple toggle both with chops and with script!

Thanks!

Hey Arco,

Hereā€™s a break down of how to solve this problem with a combination of CHOPs and DATs (example1), just CHOPs (example 2), and just DATs (example 3).
base_simple_toggle_example.tox (1.9 KB)

Which approach to use really depends on what youā€™re doing and whatā€™s most comfortable for you to work with. The one thing to be careful about is the CHOP only approach. Because the Count CHOP always cooks it means that everything down stream will cook as well - all the time. You can work around this by using a null and setting itā€™s cook type to selective.

Hope that helps.

2 Likes

Thanks! And thanks for all the effort with your great tutorials!

@raganmd
Hi, sorry for reviving a 4 year old postā€¦ Iā€™m interested in using the CHOPs + DATs example in your fileā€¦ but being a total beginner in Python, I canā€™t seem to change the ā€œCHOP executeā€ so that instead of changing the text that appears in the ā€œTableā€, it changes the Play button in a ā€œmoviefileinā€ā€¦

I tried changing op(ā€˜table1ā€™) to op(ā€˜moviefilein1ā€™).par.play
and then changing ā€œToggle Onā€ and ā€œToggle Offā€ to ā€œ1ā€ and ā€œ0ā€ or ā€œonā€ and ā€œoffā€ā€¦

but nothing seems to workā€¦ if anybody can help it would be greatly appreciated.
Thanks
Damien

Try the following expression.

	if state: op("moviefilein1").par.play = not op("moviefilein1").par.play

First, we are checking if the button gets pressed (state is True when the key goes down and False when going up).
If this is the case, we simply inverse the value of the parameter using the not keyword.
So if the value is True, it will go to false, if it is false, it will go to True.

Hi @alphamoonbase , thanks for trying to help.
Iā€™m not sure where in the ā€œchop executeā€ I should add this codeā€¦

Right now the code looks like this:

def onOffToOn(channel, sampleIndex, val, prev):
	
	target 		= op('moviefilein1').par.play

	if target[0,0].val 		== "True":
		target[0,0] 		= "False"

	else:
		target[0,0] 		= "True"

	return

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

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

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

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

And when I run it, it doesnā€™t work, and the error says

Error: Traceback (most recent call last):
File ā€œ/project1/SCENES/scene1/chopexec1ā€, line 16, in onOffToOn
TypeError: ā€˜td.Parā€™ object is not subscriptable

Hope that sounds comprehensible to youā€¦ :slight_smile:
Thank you
Damien

Can you try to explain to me what exactly you are trying to do with your code?
So target is your parameter that you want to toggle on or off.
What are you aiming to do with the [0,0] in this case?

I see two solutions here, and I think they go in tandem:

  1. Take some time to dive into the basics of python in touchdesigner to understand how to acces and change a parameter, how to use callbacks etc. Check docs.derivative.ca and the introduction to python.
    Introduction to Python Tutorial - Derivative
    Python Tips - Derivative

  2. Use only CHOPs to handle everything.
    Use a keyboardInCHOP. Then, use a selectCHOP to select the channel corrosponding to your key you want to press.
    Then download my ToggleCHOP, place it in your project, feed the output of the selectCHOP into the toggleCHOP and the output of the toggleCHOP into a nullCHOP. You can now export the channel onto your parameter.
    OLIB - Toggle Chop

Also, you can delete all the other functions (whileOn, onOnToOff, etc) so it is easier to read.

Hi alphamoonbase,

I was trying to mess with the code made by raganmd (above) to be able to trigger the ā€œplayā€ button of a ā€œmoviefileinā€ with keyboard (later I will do it with a midi controller)ā€¦ but couldnā€™t figure out the pythonā€¦ Youā€™re right, I should dive into the introduction to Python, I will. I usually prefer trying to figure out specific problems and learning along the way (it worked out well for some html and php back in the day)ā€¦ but maybe the learning curve to python is too steep for that approach.

Anyway, I tried your OLIB - Toggle Chop, and it is working perfectly for what I need! Thank you!
I will take look inside it to try to understand what is going on.

Itā€™s a bit frustrating, because I need to build a whole live system that allows me to switch from one project/tox to another easily (not always in the same order), and then allow me to interact with that tox before going to anotherā€¦ And I need to make sure I can manage to create this system before even starting having fun creating visualsā€¦

Anyway, thanks again for your help, and sorry for the noob questions.
Damien

No worries.
I asked you to explain to me what the code should do so you can better understand what is going on exactly. At least for me it helps alot explaining (and if it is to myself) step by step what every line of code is supposed to do.

Regarding your system: Check out the SceneChanger!

I understand, thanks!

Yes, yes, Iā€™m working with the SceneChanger, but Iā€™m trying to be able to interact with it with my keyboard (and later midi controller), hence all the questions to try to program what each key does!
Iā€™m getting thereā€¦

Have a nice one.
Damien

One very easy solution to obtain a toggle with keyboard is to use a count Chop with:

  • Limit : Loop Min/Max
  • Limit Minimum = 0
  • Limit Maximum = 1
    I hope its whats you need.

The Logic CHOP has a Channel Pre Op menu that you can set to Toggle, which flips the channel off-on-off-on each time it gets a pulse on the inputā€¦ see Snippet

Hi jacqueshoepffner and greg,

Thank you for your input, Iā€™ve found a solution with alphamoonbaseā€™s method, but Iā€™ll look into your suggestions to see if it could also work out (and also help me explore different possibilities, and learn)!

Cheers.
Damien

Hi @jacqueshoepffner and @greg ,

I tried both of your solutions, and they do work just as claimed. However, it seems that when I use a Count Chop or a Logic Chop, the ā€œdotted lineā€ connected to the Top with the toggle (moviefilein) seems to be always cooking.
When I use the Toggle Chop created by @alphamoonbase , that same line is only cooking when I press the key.

Being still at the beginning of this TouchDesigner journey, I havenā€™t experienced yet just how important managing things cooking or not is. But from everything I read on those forums, it seems to be a pretty big deal once a network becomes complex.

So I guess my question is an ā€œabstractā€ one, but which method do you all advise me to use ? Isnā€™t alphamoonbaseā€™s method better, since nothing is continually cooking ?

Thanks again for all your help!
Damien

dont be affraid bby the dotted line, its not this cook who can slow your process! If you are disturbed by it, you can add a null Chop after the count Chop with cook type to ā€œselectiveā€
count

Thanks Jacques.
I imagine one cooking Count wouldnā€™t be a problem, Iā€™m just trying to think ahead of my project which will have about 15 to 20 different .tox/projetcs inside of it, all with different interactions.

So I try to optimize as much as I can (and understand) from the start.

Thanks!