I’m looking for a general method to animate loops with python in grasshopper. Attached is a basic example of what I mean. In this example I have 3 points chasing each other. I am animating by using and internal timer that increases the range of the for loop incrementally and continuously. The problem is this method is that each “frame” it has to run through the entire loop which gets longer and longer as time passes.
Would there be some method that could bypass running though the entire loop? For example would there be someway to overwrite the intial variable with an operation store that value and then update the compnenent again using the new values without creating an ever increasing range? Does this make sense?
import rhinoscriptsyntax as rs
import time
import Grasshopper as gh
def updateComponent():
""" Updates this component, similar to using a grasshopper timer """
# Define callback action
def callBack(e):
ghenv.Component.ExpireSolution(False)
# Get grasshopper document
ghDoc = ghenv.Component.OnPingDocument()
# Schedule this component to expire
ghDoc.ScheduleSolution(1,gh.Kernel.GH_Document.GH_ScheduleDelegate(callBack))
# Instantiate/reset persisent starting time variable
if "startTime" not in globals() or Reset or not Run:
startTime = time.time()
# Calculate the elapsed time (in seconds)
ElapsedTime = round((time.time() - startTime), 2) * 100
steps = int(ElapsedTime)
for h in range(steps):
for i in range(len(pts)):
index_repel = (i - 1) % len(pts)
index_attract = (i + 1) % len(pts)
vec_repel = pts[i] - pts[index_repel]
vec_attract = pts[index_attract] - pts[i]
sum_vec = (vec_repel + vec_attract) * 0.5
sum_vec = rs.VectorUnitize(sum_vec) * vec_multiplier
pts[i] = pts[i] + sum_vec
updateComponent()
3body.gh (7.9 KB)