Outputting a nested python list as tree?

I am trying to set up something in GH which is fairly easy to do in a normal python environment, the result being a 2 level nested list where the both the outer and inner list lengths can be variable… The problem is when outputting my nested list, I would like it to be a GH tree with branches being the items in the outer list, and each branch containing the items in the inner list. However, the python component only gives me the following when I output my nested list:


Something special I need to do here?

Thanks --Mitch

1 Like

You can instantiate a DataTree and append Python lists to it like so (you can specify a datatype instead of [object]):

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

Giulio wrote some handy functions for reading/writing arbitrarily nested Python lists to DataTrees. I believe these come included with the Rhino WIP GHPython component.



Hi Mitch @Helvetosaur

yes, as Anders mentions, the Grasshopper SDK just does not see IEnumerables as nested lists. It has its own paradigm of nested lists, which is called DataTrees. Other nested lists are ignored (as you can see).

In WIP, there is the import ghpythonlib.treehelpers as th group of functions that should make your life easier. Please note, that the “base” of the datatree is very annoying in nested lists, so one usually wants to remove it and deal with it separately.


Giulio Piacentino
for Robert McNeel & Associates

1 Like

Hi guys,

Thanks! I remember it being something like that. I really only need GH because I would like to have a real-time preview of the output when the user varies a single input parameter. As the logic of getting items into the correct nested lists is a bit complicated to deal with using just GH components (for my tiny brain anyway), I am just going to write the whole thing as a Python script until it’s running correctly, then I will try adapting it to the Python GH component using your info.

Cheers, --Mitch