Globals functionality in python component

Hi,
I want to use this code to generate some circles along a sin curve and when I complete the code and click on ‘Test’ or ‘Ok’ in ghpython component, everything works just fine, but when I want to refresh the algorithm by pressing ‘F5’ button or change the number sliders, the first circle’s radius starts to change abnormally and I guess it’s having a conflict with globals but I don’t know how to fix it.

from math import sin , pi 
import Rhino.Geometry as rg
import ghpythonlib.components as gh
a = []
for i in range(x):
     j = t * sin(y*i*pi/x)
     a.append(rg.Point3d( i , j , 0))
     
b = cylinders = []
c = rads =[]

for i in range(len(a)):
    if 'radius' not in globals() or reset:
        radius = 0.65
    else :
        radius = gh.Distance(a[i], a[i-1]) / 2
    circle = rg.Circle(a[i] , radius)
    cylinders.append(rg.Cylinder(circle , radius*h))
    rads.append(radius)

how can I stop this from happening? I want every circle’s radius to calculate from the distance of origin point with that of the previous circle and exit the loop when it gets to the last point, but in my algorithm, the last radius calculation gets wrong and I guess it calculates the distance between the last and first point somehow!


Hello,

You could set radius=None before the loop and check for if not radius

I think GrassHopper doesn’t reset globals between runs…

thanks for your response but I do that and it’s not working. it’s still there!

What data does the t symbol contain?

it’s amplifying the height of the curve in Y axis.

Can you share your file? That would speed things up.

of course!
Snake.gh (7.7 KB)

Something like this should work:

import Rhino.Geometry as rg
from math import sin , pi 


a = [rg.Point3d(i, t * sin(y * i * pi / x), 0) for i in xrange(x)]

circles = []

for i in xrange(x):
    if i < x - 1:
        radius = a[i].DistanceTo(a[i+1]) / 2
    else:
        radius = a[i].DistanceTo(a[i-1]) / 2
    circle = rg.Circle(a[i] , radius)
    circles.append(circle)

c = circles

b = [rg.Cylinder(cir, h) for cir in circles]

However, in a scenario like this, where you have predefined origin points, there is no guarantee that the generated circles intersect each other at a single point. Most will overlap or not intersect at all.

2 Likes

thanks
yes, maybe I should use itertools for this practice. because in this one it only checks the distance with the previous point and because of that it has the issue you mentioned below.

Even if you check both distances, to the previous and next neighbours and for instance chose the smallest or biggest value, you’ll get intersections or no intersections. The problem is that the points aren’t equally spaced. You’d have to slightly adjust those, like in a circle relaxation simulation.

1 Like

that’s wright, maybe I give it a try in the future. :grinning_face_with_smiling_eyes:

Pressing the Test button or F5 does clear global variables, while just running the component “from outside” does indeed not. It was a clever design by @piac that enabled both persistent variables between runs, and an easy way to debug/clear “phantoms” variables:

1 Like