algorithmic modeling for Rhino
Hello,
I am aware that GH is not designed as an animation software; however, I consistently find myself in conditions where I need to script time-based sequential conditions. For example, one action will occur only after a certain amount of time has passed or a condition has been met.
When using firefly, I constantly need to wait for a data set to be filled before I send a boolean to another component.
I am trying to use python's 'time' module to mitigate this; however, it does not work like a time delay in processing or arduino would. Instead, it waits and sends all the data at once in a condition like this:
import time
a = []
for i in range(20):
if i % 2 == 0:
time.sleep(.5)
a.append(i+2)
else:
print 'better luck next time'
Another sample code that freezes:
import time
while True:
if x == 1:
time.sleep(.5)
print 'now waiting...'
a = 0
else:
a = 1
print 'finished'
break
Thank you in advance
Tags:
The attached GHPython script does three things which may help with your needs:
1) Uses the Python time module to get a start time and subsequently calculate elapsed time(s).
2) Uses the globals() method for instantiating a persistent variable for storing the starting time.
2) Uses a custom function for updating the component similar to the normal Grasshopper timer.
Vaya con dios..
Thank you both of you for your replies.
It seems that both approaches employ a timer/counter. This is very useful in many applications.
@ Anders, I think the usage of a persistent variable is quite useful.
Nevertheless, I am attempting a variation on what you have employed where I print out a list of numbers, but with a time interval in between like this:
a = []
for i in range(5):
a.append(i)
time.sleep(3)
I would think that this would wait 3 seconds between every output; however, this is not the case. I am thinking that I need to measure the elapsed time since the start of the code by using python's built in counter.
Once more, I appreciate your help.
I am referencing this link: http://pythoncentral.io/pythons-time-sleep-pause-wait-sleep-stop-yo...
Thank you.
You probably shouldn't be using time.sleep() as this locks up the interpreter and thus the component and thus the GH canvas. I'm having a bit of a hard time understanding precisely what you're after, but from what I can gather it sounds like you want to output some data whenever a certain time interval has passed (continuously). You can do that by altering my example above quite easily (add a conditional statement to check whether the elapsed time is divisible to zero by a desired time interval). So in the attached file, Foo will be True every 5 seconds, else False.
Note that the first parameter of the ghDoc.ScheduleSolution() call determines how often (in milliseconds) the component updates itself. So, you could also use this to only have the component check/shoot out stuff every N milliseconds that way.
Thank you Anders, you are quite right. Your code is very helpful to my understanding. I can see that the time.sleep was not the right route now. This is a very adaptable solution... much appreciated!
@ Mateusz. Thank you for sharing this, I did not know about it. It very useful for your already useful component. Thanks for the tip and for writing such a great library.
Thanks again,
Anemone has a time buffer component which will wait with ending a loop for a prescribed period, compensating the time it took to solve the upstream components. For instance if your definition takes 100 ms to compute, and you want to run it every 500 ms, the time buffer will store the data for 400 ms and then pass it to the loop end component.
EDIT: You can also just use it with any definition, not only the loop.
EDITEDIT: It's multithreaded by default (the background thread invokes the expire solution) so it won't freeze the UI. You can turn the threading off and then it works exactly as your script.
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by