Grasshopper

algorithmic modeling for Rhino

Hi,

I am new to python. I am trying to read simple bouncing ball example.

Can you explain me what sc.sticky function do here?

Maybe you could give me a link to scriptcontext documentation?

# Add the count variable to the sticky dict
if "count" not in sc.sticky:
sc.sticky["count"] = rs.VectorCreate(StartPt, org)
if "vel" not in sc.sticky:
sc.sticky["vel"] = rs.VectorCreate(dir, StartPt)

def re():
if Reset:
sc.sticky["count"] = rs.VectorCreate(StartPt, org)
s = rs.VectorCreate(dir, StartPt)
sLen = rs.VectorUnitize(s)
sc.sticky["vel"] = rs.VectorScale(sLen, length)
return

def bound():
if sc.sticky["count"][0]> box[0] or sc.sticky["count"][0]< -box[0]:
sc.sticky["vel"][0] = - sc.sticky["vel"][0]
if sc.sticky["count"][1]> box[1] or sc.sticky["count"][1]< -box[1]:
sc.sticky["vel"][1] = - sc.sticky["vel"][1]
if sc.sticky["count"][2]> box[2] or sc.sticky["count"][2]< -box[2]:
sc.sticky["vel"][2] = - sc.sticky["vel"][2]
return

def main():

print(sc.sticky["count"])

if Toggle:
ac = sc.sticky["count"]
bc = sc.sticky["vel"]
add = rs.VectorAdd(ac, bc)
sc.sticky["count"] = add
print(sc.sticky["count"])

bound()

# Reset the count
re()

# Return count to GH
Count = sc.sticky["count"]
pt_x = sc.sticky["count"][0]
pt_y = sc.sticky["count"][1]
pt_z = sc.sticky["count"][2]

a = rs.AddPoint(pt_x, pt_y, pt_z)

return a

a = main()
b = box

Views: 3522

Replies are closed for this discussion.

Replies to This Discussion

.sticky makes a persistent variable that doesn't reset everytime the component recalculates but keeps its value. This is important if, for example, you are using a timer (whose pulse resets the script component) but you want the next frame to build off the previous results. Variables saved in .sticky can also be called from other script components.

(Of course you'll need to include the line

<code>import scriptcontext as sc</code>

at the beginning for it to work right)

https://github.com/mcneel/rhinopython/blob/master/scripts/samples/s...

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service