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,

If you know the depth of your list will be 2, then here is an example:

Attachments:

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

Attachments:

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

Attachments:

Very nice Giulio. Thank you for sharing it.

This is amazing! Thank you for sharing. 

I assume it would be handy to have this functionality available directly on the tree class itself?

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! 

Giulio, 

I noticed one small bug. When data tree is simplified to single digit paths like {0}, {1} etc then output is only the first branch:

It works great with anything that has a trailing zero in a path like {0,0}, {0,1}

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service