GHPython-counter from 0 to a value

Hi!

I would appreciate any help. I am looking for a script in GhPython that counts the values in the list every second. The list starts always with zero and ends with “y”. I want the script to count values every second, as soon as I push the start button. For example in the first second the output is 0, at the second = 1 and so on. As soon as the value equals the “y” the GhPython script should stop counting and stay by the “y”. With the new button press the new sequence should go.

I hope, I described my goal properly.

Thanks!

Best regards,
Dale

You need to use “global variables”, which will not automatically reset at each re-computation of the component.

See @piac 's comment here C#Timer to Python Timer - Grasshopper

Changing it to something like this should do what you want:

if x or 'counter' not in globals():
counter = 0
else:
if counter < y:
    counter += 1

a = counter
2 Likes

You can also script the timer/trigger if you want, here’s a quick example:


200330_GHPython_CountTo_00.gh (3.6 KB)

7 Likes

Thanks a lot! it´s very helpful!

Dear Anders!

Thank you one more time for your help! I wanted to ask you, if it is possible to rewrite your script, so you can start the process with a button and it will reset itselfs at the end automatically. I also need a boolean output “true of false” during the script´s work, so when you press the button the counter goes and the boolean output is true.

Thanks one more time! I am sorry, if I seem to demanding and I will understand, if you won´t have any time for it :).

I am not so versed with python, so I would apricate every help :slight_smile:

Best regards and thanks 1 more time!
Dale.

I’m not sure why one would use a Button for this (which has two states i.e. press down and release). Both features would be solved by still using a Boolean Toggle and a slight modification to the script:


200512_GHPython_CountTo_00.gh (4.2 KB)

1 Like

Dear Anders,

Thanks for your answer.

I am looking for a starter for a sequence: I have n models which I want to perform FE-Analysis on. I gather the needed information of each model with a “Data recorded” to evaluate all of them later and find the “best” one. I can´t do FE-Analysis at all the models at once, because there thousands of them.

So I tried to use your script and rewrite it so that sequence starts with a button press and goes from zero to target-value, resets itselfs and then stops. I only need one cycle. But I failed to rewrite.

Your scrict from the prior post would be perfect if it was only one cycle, which could be started by the boolean or a button.

Thanks so much for your help in advance! I don´t want to be too persuasive and will understand if you won´t have any time for it :slight_smile:

Best regards,
Dalel

If I understood this correctly, moving the updateComponent call to the scope where we increment the count variable should do it:


200513_GHPython_CountTo_00.gh (3.5 KB)

6 Likes

Yes! That´s perfect! Thanks!

1 Like

Oldie but goldie! :100: :joy:

3 Likes

Nice script. It is possible to add an option to reset the counter back to Zero when the target is reached and run the timer again?

I think this version does that:

1 Like

Thank you. Much better.

1 Like

How to add a pause button that can control whether to continue updating the output or not?

Here’s a quick edit that adds a Pause toggle:


200512_GHPython_CountTo_00_WithPause.gh (6.9 KB)

4 Likes

what do i have to change in the script so that it is possible to run 2 counters without the speed of both being exactly the same?

That’s a good question, I never came across this requirement before. I’d guess it has to do with the ScheduleSolution method implementation:

Isn’t it enough to rewrite the script?

Well yes. One of the two methods used to trigger the GHPython component update is ScheduleSolution. So one would have to study why two components would get “entangled” the way you’re describing.

It is possible to add also a reset button?