Grasshopper

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.

Views: 846

Attachments:

Replies to This Discussion

Thanks for the quick reply. I'm using Kangaroo for the animation, there arent any frames, its just solving the physics in real time.

I would love to see an example and would be happy to try solving both with code and without.

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:

https://youtu.be/H9Ip4fDaaX8

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!

Attachments:

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

Attachments:

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

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service