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

2 Likes

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:

6 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

Hi, how do I call the tree class in python?
Thereā€™s a method I wish to use. Itā€™s the renumber branches to ascending numbers.
But, I cannot use without calling tree class firstā€¦?