@PeterFotiadis Thanks for that jagged puzzle thing
Took a bit of detective work but I fixed the bug in my code above, it should work on your tree now.
Only caveat is that it “renormalizes” floating branch counts to start from zero, but their relative order and hierarchy should remain the same. i.e. a structure like
{3} "A"
{3,10} "B"
{8} "C"
will be transformed into the nested list
[["A", ["B"]], ["C"]]
which deserializes on the other end to a regular
{0} "A"
{0,0} "B"
{1} "C"
tree2nest2tree.gh (7.0 KB)
Updated code:
from itertools import groupby
def nest(tree, paths=None, level=0):
paths = list(tree.Paths) if paths is None else list(paths)
result = []
if paths[0].Length == level:
result += tree.Branch(paths.pop(0))
for _, subpaths in groupby(paths, lambda p: p[level]):
result += [nest(tree, subpaths, level + 1)]
return result
nested_list = nest(tree)
