Let’s say I have a tree with multiple strings per branch. When I organise a list as below, the first branch of every depth is offset by the number of items in the parent branch (For example, my branch {0}'s first child branch is {0;3} because {0} has 3 items). Is this an error in my implementation? Or is it a treehelper bug? How can I fix it so that I end up with {0} and {0;0}?
I don’t think that you’re dealing with a bug, because when you look at your example, you have an outer list which gets “converted” to a branch at path {0}, and a nested list at index 3 of the outer list which results in the branch {0;3}.
I’m not sure whether there is anything to fix here. {0;0} would simply mean that the sublist would be at index 0 of the outer list (i.e. [[green], black, black, black]
)!
Here’s the full method behind this, written by @piac :
def list_to_tree(input, none_and_holes=True, source=[0]):
"""Transforms nestings of lists or tuples to a Grasshopper DataTree"""
from Grasshopper import DataTree as Tree
from Grasshopper.Kernel.Data import GH_Path as Path
from System import Array
def proc(input,tree,track):
path = Path(Array[int](track))
if len(input) == 0 and none_and_holes: tree.EnsurePath(path); return
for i,item in enumerate(input):
if hasattr(item, '__iter__'): #if list or tuple
track.append(i); proc(item,tree,track); track.pop()
else:
if none_and_holes: tree.Insert(item,path,i)
elif item is not None: tree.Add(item,path)
if input is not None: t=Tree[object]();proc(input,t,source[:]);return t
If you merge together items and paths, you get a representation that cannot be rendered as nested lists, unless you start adding more elaborate types, which is beyon the scope of the tree-to-list functions.
Just make sure that items do not get placed in locations used by paths, and you are good to go. You can use a “Graft” on the lower-lever tree to achieve this.
Thanks,
Giulio
–
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com
Thanks piac and PirateBoy. I changed the order of the items in the list (which generated some NoneTypes in the branches after conversion) but at least the hierarchy was correct. After that I just removed all NoneTypes from all branches.