Grasshopper

algorithmic modeling for Rhino

Is there an easy way to cast an Array or Array (or list of list) into a Data Tree? 

I tried using a tree.AddRange() since it was supposed to be able to receive IEnumerable[]() but it throws an exception that such cast is not valid. 

I am operating on lists and my final product might be a multi level nested list such as:

_list = [[x],[x],[[x],[x],[x]]]

Here's my code:

# recursive function to process any input list and output
# matching structure list
def process_list(_func, _list):
return map( lambda x: process_list(_func, x) if type(x)==list else _func(x), _list )

def toRHObject(item):
if type(item) == ms.MSEllipse:
return item.toRHEllipse()

#EllipseOut = DataTree[rc.Geometry.Point3d]()
if _import:
_tree = DataTree[rc.Geometry.Ellipse]()
some_data = process_list(toRHObject, data)
someList = Array[Array[rc.Geometry.Ellipse]](map(tuple, some_data))
outEllipses = _tree.AddRange(someList)

Here's my some_data when I try to cast it:

Array[Array[Ellipse]]((Array[Ellipse]((<Rhino.Geometry.Ellipse object at 0x0000000000000064 [Rhino.Geometry.Ellipse]>)), Array[Ellipse]((<Rhino.Geometry.Ellipse object at 0x0000000000000065 [Rhino.Geometry.Ellipse]>))))

Views: 7593

Replies to This Discussion

Hi Konrad,

It's not really a bug - that is intended. The sample expects a leading {0} as a base, as that is usual GH behavior.

It's just that the "natural form" of the tree as list has an arbitrary base of {0} by itself. For example, there is no "no-path-but-single-item" possibility in Grasshopper -- that would also be a possible combination for a possible data structure that has any type of input mapped to any type of tree.

To make the structure look as similar as possible, but also adjustable, I chose to have a retrieve_base argument that you can see in tree_to_list. Just ask for the whole thing as a base, and you will get all those different bases ... inside an additional list.

Use this code:

tree_to_list(x, retrieve_base=lambda x:x)

I hope this helps,

Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Makes sense now. Thanks again! 

Well... I confess that I can't get the gist of the issue: why bother with ListOfListOfList... and not use DataTrees instead? (NOT a multidimensional List).

Or maybe the real "underlying" issue of yours is to iterate through "variable" DataTrees on a per parent to child basis? (variable == variable amount of items per branch and branches with variable dimensions). If so I could post here the solution (but is in C#).

Anyway here's some simple examples on that matter (but in C#).

Attachments:

Peter,

This workflow is for work that I am doing with importing serialized geometry from other software (Dynamo/Revit). Concept of DataTree is unique to GH thus I wasn't using those in Dynamo instead I used Lists and Nested Lists. When I serialize my input back together in GH I am holding a List/Nested List object. That's why I am asking for conversion from that to DataTree. 

Giulio,

Thank you for that example. It works like a charm. I bet it is a frequent question since Lists are such integral part of Python. Anyways, thank you again. 

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service