Ghpythonlib tree helpers

I’m trying to figure out how to use ghpythonlib’s tree helpers to move between trees in grasshopper and nested lists in Python. However, I can’t seem to convert a tree to a list and back within Python (of course what I’m trying to ultimately do is more complex, but I need to get this to work first). Can anyone tell what I’m doing wrong here? (5.8 KB)


Have you set the TreeIn input to Tree Access? At first glance, I don’t see what else could be wrong with your code.

Yes, I have

Hm, something seems to be askew with the path numbering of your input data. I tried flattening and re-grafting the data and it worked fine!

Maybe @piac could shine a light on this. If I remember correctly from a while back, it was he, who came up with the amazing treehelper functions for GHPython!?


test = th.tree_to_list(TreeIn, lambda x:x[3])

There may be a better way, but the documentation states the following:

| tree_to_list(input, retrieve_base=<function <lambda$33> at 0x0000000000000089>) | Returns a list representation of a Grasshopper DataTree.
| Inputs:
| input: A tree to be transformed.
| retrieve_base= lambda x: x[0]:
| Most trees start with a [0] path that is never changed.
| That is rendered as a list with a single item. However, for simplicity, most users
| will want to start using the first branch in that path. Therefore, retrieve_base
| defaults to a function that will just return the first item in the output.
| If a tree is created manually or edited,
| then it can also have paths starting with [1] etc. In this case, retrieve_base can
| be set to None. In this case, the entire result is returned, inside a single list.
| This is the equivalent of the “source” input for list_to_tree.
| Returns:
| A list object

it’s a result of your data tree structure starting with 3.

1 Like

Thanks for the replies. To be honest I’m having a hard time understanding your quote from the documentation, but it also works to use Path Mapper to just add a 0 in the front of all my paths, then simplify after things come out of python.

try this:

test = th.tree_to_list(TreeIn, None)
1 Like

Thank you all for the replies. It was indeed the weird paths that were causing the issue, I ended up fixing that upstream but it’s great to know these fixes for future reference.