Simulate a button click with python

I’m trying to create a python script with one integer input and one boolean output. The idea is to change the output boolean state for 1 second if the input integer is changed. Like to simulate a button click.

Why don’t you simply add a timer to the component you wish to update every second?

Because he doesnt want to update every second. He wants to change a bool for a second each time the integer changes, then change back, like a button click.

1 Like

@Michael_Pryor, I’m sorry but I do not understand your comment. This IS the easiest/simpler way to make that. If you don’t want to code the timer yourself.

It is absolutely not a timer in any way. For instance, let’s say I have a script running over a bunch of panels and I say I want this thing to trigger false, then back to true, to do something like reset the loop or run something else, if my panel size is 5m2 (5 being the trigger in this case). How is that a timer problem? It’s a button condition.

1 Like

being it button is irrelevant since he just need the bool, but I’ll add one more simpler solution :slight_smile:

import time

Again. How is timer doing the following.

-Detecting that the input changed.
-Outputting true
-Then switching back to Outputting false

And doing this ONLY when the input changes. Timers are constant. What I would do is the following.

-Test if the input changed
-if true output true
-pause the script for 1 second (not a timer, a pause)
-switch the output to false
-end the script

The behavior is exactly that of a button but with input change rather than button click.

1 Like

how would you do that without a timer? :slight_smile:
or import time

You would never do it with a timer, we are not measuring any constant interval across time for this, we are measuring an input change unrelated to time. You can use the import time with time.sleep(1). A pause is not a “timer”. It is a pause, and we only need it one time when an input is changed, we do not need intervals of it. Please show me how you would do that with a timer component and not code.

Also to track that change to output I believe some output may need to be out of the solve instance.

1 Like

Well I didn’t say there will be no code involved :slight_smile:

Btw, wouldn’t time.sleep(1) lock the python engine for 1 sec? You may need this 1 second for the other calculations happening meanwhile.

Now, I think there’s one more way to do that trick, using datadam

Btw, wouldn’t time.sleep(1) lock the python engine for 1 sec? You may need this 1 second for the other calculations happening meanwhile.

That’s why the output needs to be out of the solve instance so you can still output the change while it’s paused and so you can output before the script is finished. When the pause is finished the output will switch back.

Man, Grasshopper really doesn’t like code generated timers. It locks everything for the duration of the timer. It doesn’t even pass the outputs properly. It doesn’t matter if it is Time.Sleep() or counting with Time.Time().

1 Like

Here’s a dirty solution: (6.4 KB)

btw @Michael_Pryor, my original idea with the timer was: if you modify the toggler function such that it will count up. Remove the time_trigger and hook up the timer-component.

Set it to 1 millisecond and while the value of the toggler is less than 1000 it will change the value of a

I believe (not tested it) that at least this way you will actually get the toggled value of a during the counting. Unfortunately you can’t be sure that 1 grasshopper second is equal to 1 real life second. As a matter of fact most certainly it will not be true.


And this is how it’s done with timer component: (4.7 KB)

No lockup. In my opinion this is the better solution even though not real time seconds.

1 Like

It’s not a really direct solution, it should be like how anemones trigger works, keeping time seems a bit excessive for this problem. I’ll script something when I get a chance but it won’t be in python.

Thank you @Michael_Pryor and @ivelin.peychev for the discussion. Michael perfectly described the issue that I have. image This script doesn’t really do the trick. In the recorded data each time I change the input x there should be both True and False values consecutively coming out from ‘a’ with a slight lag. As you can see we are getting only one value now.

The trick works but GH has a problem when launching other apps (in this case the python interpreter). GH locks, waiting for the interpreter to finish all its tasks (in this case the waiting). This is a known issue (at least I consider it an issue). I think GH and Rhino and the Python Engine should all run on separate processes transmitting data between them. Currently everything runs in a single process. So in order for plugins to transfer data between each other, they have to lock themselves.

check the second script I have added. adjust the script for 20 milliseconds or so. Then you will see it True and false.

There’s one more solution but then you will need additional component. It cannot happen with just one component. Unless that component is compiled and written in c# I guess. To run natively inside grasshopper. And one more solution is if you implement the updating (expiring) mechanism from inside the python component by running it in SDK mode.

1 Like

@DavidRutten, what does this mean?

Thank you Ivelin. This one seems to be the one I need. Could you please upload the script? I want to test it inside the algorithm.

Just a try (4.8 KB)

Script to detect when value changed: