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?

Thanks

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).

Great,

thanks Chris!

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

Thanks,

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

1 Like

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

https://developer.rhino3d.com/wip/api/grasshopper/html/T_Grasshopper_DataTree_1.htm

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

Read:

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

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

Write:

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):
        dt.AddRange(l,gh.Kernel.Data.GH_Path(i))
        
    return dt

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

2 Likes

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