algorithmic modeling for Rhino
Hi, new to this community - I have a good grasp of Grasshopper and now trying to get into python scripting.
I'm working on a simulation involving collision detection. Right now I have set up the system so that when two objects collide they change color. However, as soon as they stop colliding they return to normal. See video below for example:
https://www.youtube.com/watch?v=JbH0YHEDVGk
I am looking for a way for the change to be persistent. What I would like to do is compare the list to itself, and once collision is detected to change the value "permanently" (or at least until a reset).
It seems to me there is no way to do this natively in grasshopper, and that I need to create a custom component to do so.
Tags:
Figured it out. Needed to use scriptcontext to maintain a "sticky" list. Learned a lot of python along the way. Got the idea from this thread:
http://www.grasshopper3d.com/forum/topics/storing-data-in-gh-python
You can see the animation here:
And this is my code:
import scriptcontext as sc
if "save5List" not in sc.sticky:
sc.sticky["save5List"] = []
for i in List:
sc.sticky["save5List"].append(False)
print "still falsing"
if Toggle == True:
for i in range(0,len(List)):
if List[i] == True:
sc.sticky["save5List"][i] = True
control = sc.sticky["save5List"]
else:
for i in range(0,len(List)):
if List[i] == False:
sc.sticky["save5List"][i] = False
control = sc.sticky["save5List"]
if Reset == True:
for i in range(0,len(List)):
sc.sticky["save5List"][i] = False
Thanks for the help!
It's pretty straightforward to make a persistent variable without using sticky (see attached). One upside here is that the variable scope will be contained to its GHPython component (i.e. not available to the entire Rhino Python session, and thus not in danger of being overwritten or otherwise messed with).
Okay, that is much more elegant. I'm not entirely sure how it's working, though.
Is any variable I create outside of a function global and persistent? Is the statement if in globals() just checking to make sure other python scripts haven't used that specific name? Just trying to understand whats going on here in the background.
Either way that cleaned up a lot of clutter, thanks :)
New Code:
#check global variables, create new global for list, fill it with falses. Same if reset button pressed.
if "saveList" not in globals() or Reset:
saveList = []
for i in List:
saveList.append(False)
#toggle controls weather we maintain true (color) or false (no color) for the cull pattern
if Toggle == True:
#check the list and every true (collision) gets saved in the global list
for i in range(0,len(List)):
if List[i] == True:
saveList[i] = True
else:
#check the list and every false (collision) gets saved in the global list
for i in range(0,len(List)):
if List[i] == False:
saveList[i] = False
#output global variable
colorCullList = saveList
It's basically a clever way (courtesy of Giulio) of exploiting how the GHPython interpreter works. See this discussion for more information (in which I initially considered it as being a bug as it can lead to "phantom" variables).
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