Hello Everyone.
I Have A Problem With Converting Data In ghpythonlib.TreeHelpers as you see bellow:
TreeToList.gh (8.5 KB)
I have a Data Tree of Curves in Grasshopper and I Want To Convert it to a Nested List that in the Inner List Contains a List of 2 Curves. But It Seems that treehelpers only give One TreeBranch of my Data.
I Don’t Want to Export the Nested List to the Grasshopper Canvas again, I Export the Data to show my problem, If the Nested List Remains in the ghpython component, it’s Ok. it’s just a simple part of my code.
thanks
piac
(Giulio Piacentino)
June 6, 2022, 3:31pm
2
Hi, that is a simplified tree that represents a forest; see here:
(The comment shows when you click here, discourse shows a useless preview, though)
list_to_tree.py
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
This file has been truncated. show original
tree_to_list.py
# written by Giulio Piacentino, giulio@mcneel.com
def tree_to_list(input, retrieve_base = lambda x: x[0]):
"""Returns a list representation of a Grasshopper DataTree"""
def extend_at(path, index, simple_input, rest_list):
target = path[index]
if len(rest_list) <= target: rest_list.extend([None]*(target-len(rest_list)+1))
if index == path.Length - 1:
rest_list[target] = list(simple_input)
else:
if rest_list[target] is None: rest_list[target] = []
This file has been truncated. show original
You can also fix this by selecting items normally and using “graft” or directly writing paths are {0;0}, {0;1}, {0;2}…
1 Like