Example - GHPython/Rhinocommon/Treehelper functions

ghpython
rhinocommon
unhandled

(Chris Hanley) #1

I 'm posting this as an example. I don’t have a specific question, just wanted to share and say thanks for implementing the treehelper functions in R6!
I was working on something today that required me to put values into a certain data tree structure using GHPython. I have used tree helpers to deconstruct existing data, but not too much with creating a structure.
Below is a stripped down example. The goal was:

  1. Create domains from input list of numbers
  2. Create Branch for each domain
  3. Check input Values for inclusion in Domain
  4. Sort values into corresponding branches.

import Rhino
from ghpythonlib import treehelpers as th

Doms = []
newTree = [] # blank "container" list to hold other lists

# create intervals/domains from list of numbers in x
intervals = zip(x,x[1:])
for int in intervals:
    Doms.append(Rhino.Geometry.Interval(int[0], int[1]))
    newTree.append([]) # add a new nested list to "container" list for each interval

# from random numbers in y, see if they are inside domains just created
for v in y:
    for i, dom in enumerate(Doms):
        if Rhino.Geometry.Interval.IncludesParameter(dom, v, True) == True: # test to see if value is in domain
            newTree[i].append(v) # append to appropriate nested list
        else:
            pass

T = th.list_to_tree(newTree, True)  # convert nested list to data tree

If you know a better way, (in GHPython), please post!
GHPYExample-TreeHelpers.gh (9.8 KB)