Need Help! GH_Python : Using global List

python

#1

Hi,

I’m writing a code to generate simple branch structure in grasshopper.
I use couple of ‘global list variable’ to store lines and points.
The list for points is fine, but the list for curves only stores ‘null’.
Please see the screenshot and code below. Thanks for your help in advance.

randomBranching_WIP.gh (9.0 KB)


import rhinoscriptsyntax as rs
import Rhino.Geometry as rg
import random as rnd

if go :

wanderpt (attr, walkingDist)
checkdistandaggr(attr, boundary, min_dist)

else:
aggr = []
lines = []

wander_pt = rg.Curve.PointAt(boundary, rnd.uniform(0, 1))
aggr.append(attr)

a = wander_pt
b = lines
c = aggr

def wanderpt (attr, scaleDist) :
global wander_pt

vec = rs.VectorCreate(attr, wander_pt)
vec = rs.VectorUnitize(vec)
vec = rs.VectorScale(vec, scaleDist)
ang = rnd.uniform(-75, 75)
vec = rs.VectorRotate(vec, ang, rs.VectorCreate( [0,0,1], [0,0,0]))

wander_pt = rs.VectorAdd(vec, wander_pt)

def checkdistandaggr(attr, boundary, m_dist) :

global wander_pt
global aggr
global lines


for i in range(len(aggr)):
    dist = rs.Distance(wander_pt, aggr[i])
    
    if dist < m_dist:
        
        lines.append(rs.AddLine(wander_pt, aggr[i]))
        aggr.append(wander_pt)
        wander_pt = rg.Curve.PointAt(boundary, rnd.uniform(0, 1))
        break


(Giulio Piacentino) #2

Could you please attach your definition? That will make it faster to see what you are doing. @lepli


#3

Thank you for your reply.
Here is my definition.
Thanks,

randomBranching_WIP.gh (9.0 KB)


(Giulio Piacentino) #4

Hi @lepli

I added two comments.

  • If you want to make geometry live forever, you need to transform it into RhinoCommon data, because the “Grasshopper rhinoscript target” document is cleared after every solution is done. Otherwise, leaving anything behind would create serious memory leaks.

  • There was a minor flaw in the logic, and a good guess would have also been a zero-length curve

As a suggestion, you could maybe get nicer results if you exclude points that have a distance from the circle that is see than the one to the next “aggr” point.

lepli_randomBranching_fixed.gh (9.4 KB)


#5

Hi @piac

Thank you for your help. I have been confused with the issue related to RhinoCommon and RhinoScriptSyntax, and went wrong many times. Your comment is very helpful for me to understand it.

Many thanks