Simulate a button click with python

python
(Mesrop Andriasyan) #1

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.

0 Likes

(IVELIN PEYCHEV) #2

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

0 Likes

(Michael Pryor) #3

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

(IVELIN PEYCHEV) #4

@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.

0 Likes

(Michael Pryor) #5

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

(IVELIN PEYCHEV) #6

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

import time

0 Likes

(Michael Pryor) #7

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

(IVELIN PEYCHEV) #8

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

0 Likes

(Michael Pryor) #9

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

(IVELIN PEYCHEV) #10

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

0 Likes

(Michael Pryor) #11

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.

0 Likes

(IVELIN PEYCHEV) #12

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

(IVELIN PEYCHEV) #13

Here’s a dirty solution:
toggle_for_a_second.gh (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.

0 Likes

(IVELIN PEYCHEV) #14

@Michael_Pryor,

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

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

1 Like

(Michael Pryor) #15

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.

0 Likes

(Mesrop Andriasyan) #16

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.

0 Likes

(IVELIN PEYCHEV) #17

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

(IVELIN PEYCHEV) #18

@DavidRutten, what does this mean?

0 Likes

(Mesrop Andriasyan) #19

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.

0 Likes

(Seghier khaled) #20

Just a try
timer.gh (4.8 KB)

Script to detect when value changed:

2 Likes