Tree operations in Python

Hello everyone,

I would like to learn how to do tree operations using Python.

Could anyone address me to some tutorials?


I don’t know of any specific tutorials, but in R6, (I think that’s when it was introduced), you can use treehelper functions., (after importing ghpythonlib)…also assuming you are referring to the ghpython component.

here is a link to one example that I know of, (I’m sure a search of the forum would turn up more/better references).


thanks Chris!

There’s also this page, but we are working on it:



Giulio Piacentino
for Robert McNeel & Associates

1 Like

There’s also the documentation of the DataTree class itself:

And some minimal examples for reading and writing two dimensional trees:


""" Read the Grasshopper Datatree "FooTree" and translates it to 
a nested Python list """

pyList = [list(b) for b in FooTree.Branches]


import Grasshopper as gh

def listToTree(nestedList):
    """ Convert a nested python iterable to a datatree """
    dt = gh.DataTree[object]()
    for i,l in enumerate(nestedList):
    return dt

And Giulios more advanced helper functions that reads/writes arbitrary nestings:


So I don’t know if I am going crazy or not. First of I have successfully manipulated trees and list before pre and post tree helpers. I tried using tree_to_list but got:

Runtime error (MissingMemberException): ‘str’ object has no attribute ‘BranchCount’

thought that was odd so I tried the pre treehelper (R5) workflow as described in:

Still having problems so then I downloaded the sample file there. Now this is where it gets weird. The python components from the example file i downloaded worked, however I copy and pasted the same code to a fresh Py component and it was giving me the same sort of errors:
‘str’ object has no attribute ‘BranchCount’ (or ‘Branch’)

My original code that I wrote was copy and pasted into this “downloaded” component worked. I checked the Lib imports from the SDK mode as well and both are the same. Anyone else having this sort of problem? I feel like I’m taking crazy pills!

fresh pythong component of the left, downloaded component on the right:

Have you changed the input to tree access?

O__O Thanks for proving I’m crazy… haha

1 Like