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]>))))
Tags:
that's the main issue here. My process_list() function operates on/outputs any depth of list. I was hoping for something more flexible: any depth list to datatree. Suggestions?
Hi Konrad,
because this functionality is requested quite often, I wrote this sample that I think does what you are asking.
As a side comment, for large lists it would still be more efficient to create the tree directly rather than with this converting function; but this function is arguably more convenient for the programmer.
Thanks, I hope this helps,
Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com
Giulio,
While we are at it. I am wondering if you have a method for doing reverse (a DataTree to nested lists?)
I do not right now. :)
Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com
Hi Konrad,
this Python script should do the work you are requesting. Lists of lists indeed make working with "trees" easier in Python.
I hope this helps,
Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com
Very nice Giulio. Thank you for sharing it.
This is amazing! Thank you for sharing.
Yes definitely, or in a Python module we ship with GhPython otherwise.
Yes, something like DataTree().FromList(inputList) or someDataTree.ToList(). Thank you in advance!
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by