Hi,
This script produces a very simple mover/agent/particle that randomly travels within two-dimensional bounds.
I think that it used to work great back in Rhino 5, but seems very laggy in version 6. The mover seems to slow down when approaching the scene centre (?) and the other GH components start to recompute like crazy. There is nothing particular evaluated in this region though.
Here’s the GHPython script:
import Rhino.Geometry as rg
import Grasshopper as gh
import random
import math
class Mover:
def __init__(self, pos, vel):
"""Inits this mover."""
self.pos = pos
self.vel = self.contruct_random_vector3d()
def update(self):
"""Updates this mover."""
self.check_bounds()
self.pos += self.vel
def contruct_random_vector3d(self):
"""Returns a random three-dimensional unit vector."""
vec = rg.Vector3d.YAxis
angles = [math.radians(a) for a in range(15, 345+1, 30)]
angle = random.choice(angles)
vec.Rotate(angle, rg.Vector3d.ZAxis)
return vec
def check_bounds(self):
next_pos = self.pos + self.vel
if next_pos.X > 100 or next_pos.X < -100:
self.vel *= -1
if next_pos.Y > 100 or next_pos.Y < -100:
self.vel *= -1
###############################################################################
def update_component():
"""Updates this component, similar to using a Grasshopper timer."""
def call_back(e):
ghenv.Component.ExpireSolution(False)
# Get this Grasshopper document
gh_doc = ghenv.Component.OnPingDocument()
# Schedule this component to expire
gh_doc.ScheduleSolution(1, gh.Kernel.GH_Document.GH_ScheduleDelegate(call_back))
###############################################################################
if Run:
mv.update()
a = mv.pos
b = mv.vel
update_component()
else:
mv = Mover(Position, Velocity)
Is there any issue with the update_component
function in Grasshopper 1.x?
Or am I simply overlooking something?
simple_mover.gh (8.0 KB)
Edit:
Changed the category to Scripting. Might be more appropirate for this question.