Python combine data of branches

I try to python branches to another kind of list, what am I doing wrong?
Thanks in advance for your response.

20190106 problem python combine branch (5.9 KB)

Wrong image?

Thank you, I now changed it in the first post.

Try this file!

20190118 problem python combine branch (6.4 KB)

1 Like

one thing to be careful of is how the ghpython component “outputs” list structures. If you pass a nested list, (as an actual list, not just a string), the component will do it’s best to translate that into a tree structure.

if you need to pass your nested list within the code of the component, you could use this to structure your list.
Note: the print statement will yield the list, structured how you want. But, if you assign that list directly to an output, it will not be what you want.

from ghpythonlib import treehelpers as th

newlist = []
newlist.append(th.tree_to_list(x, 0))
print newlist[0]
a = newlist[0]

set the input of the ghpython component to Tree access.

1 Like

I was trying this a couple of days ago, but could not figure it out.
How to transfer the list in a list to another python component?

20190118 problem python offset (7.0 KB)

I was trying things like

> newlist=[]
> newlist.append(th.list_to_tree(x,0)

But as it does not make any sense doing it like that, it does not work either. I still have to learn it.

try this in your second component:

import ghpythonlib.treehelpers as th

#print oneandtwo[0][0] # which will be 4

newList = th.tree_to_list(oneandtwo)
print newList[0][0]
print newList[1][0]
1 Like

Input is set to Tree. I tried that previously, but it gave me characters instead of numbers. Hmmm… this was the thing that I could not solve previously.

It are the characters of ‘IronPyton.Runtime.List’

don’t pass the list through the text panel.

1 Like


Now I am trying to output curves like a branch. But, I think I need to do something else like ‘for’ or something. However, I do not know it.

newlist.append(th.tree_to_list(x,0)) as you show earlier, but then for something else, I might must do something similar to that

I cannot retrieve te curves and export it as a tree with curves from Python into Grasshopper. Do you know what am I doing wrong?

In the image, left beneath (last rule) are the input crvs, right the part which I cannot let work to output it as crvs.

20190118 problem python combine branch (5.2 KB)

in your component’s print statement, it looks like your list is already structured, (nested list). if you want to output that to a data tree, you need to use list_to_tree.

1 Like

1. Data conversion failed from Goo to Curve
Hmmm, do you know what a Goo is?

a discussion on gh data types/classes here:

as always, posting a file with an example helps. If geometry is from the rhino file, please be sure to internalize the geometry.

1 Like

Odd enough, it works now, I do not know why. I just refreshed again and again, and open Python and closed it and opened it and refreshed it.

It works. Thank you. :smiley:

How to go from Goo to Crvs?
I want to get of every rectangle the areas which they share with others, grouped per rectangle (so, member A shares two intersecting areas, member B shares four intersecting areas, etc.). (10.2 KB)

Hi @ForestOwl,

The problem is that you append lists to the inters list output. This produces a nested list as output. Nested lists are lists with an x-amount of sublists and levels.

Here is, how it works:

import rhinoscriptsyntax as rs

inters = []
for reg in regions:
    # Don't call your varibale 'int'!!!
    # It's reserved for an internal method to convert floats to integers!
    rint = rs.CurveBooleanIntersection(r, reg) 
    if len(rint) > 0: # check for empty intersection lists
        inters.append(rint[0]) # unpack the intersection curve ids before appending
        #inters.extend(rint) # OR use extend instead of append
1 Like

And if you want to get rid of the duplicate intersection curves that your script produces and see how everything can be done in GHPython with rhinocommon, check this out:

import Rhino.Geometry as rg

def list_to_tree(input, none_and_holes=True, source=[0]):
    """Transforms nestings of lists or tuples to a Grasshopper DataTree"""
    # written by Giulio Piacentino,
    from Grasshopper import DataTree as Tree
    from Grasshopper.Kernel.Data import GH_Path as Path
    from System import Array
    def proc(input,tree,track):
        path = Path(Array[int](track))
        if len(input) == 0 and none_and_holes: tree.EnsurePath(path); return
        for i,item in enumerate(input):
            if hasattr(item, '__iter__'): #if list or tuple
                track.append(i); proc(item,tree,track); track.pop()
                if none_and_holes: tree.Insert(item,path,i)
                elif item is not None: tree.Add(item,path)
    if input is not None: t=Tree[object]();proc(input,t,source[:]);return t

crvs_inters = [] # nested list of intersection curves per curve

for i in range(len(Regions)):
    crv_inters = [] # list of intersection curves for the curve Regions[i]
    for j in range(i+1, len(Regions)): # prevent double checking and duplicate intersections
        crvs_bint = rg.Curve.CreateBooleanIntersection(Regions[i], Regions[j])
        if len(crvs_bint) > 0: # if successful intersection
    if len(crv_inters) > 0: # curve Regions[i] has intersections with other curves

# Outputs
Intersections = list_to_tree(crvs_inters) (11.6 KB)

1 Like

@diff-arch,If you have access to Rhino 6, the list_to_tree and tree_to_list functions are builtin, so you don’t have to re-write the function. You can import them via:

import ghpythonlib.treehelpers as th

edit: this function is specifically in regards to tree/list functions. nothing to do with casting data types.

1 Like

@diff-arch Thank you!

@chanley so with this I can export goos, I will try that