Grasshopper

algorithmic modeling for Rhino

Hello,

So I have this problem that I am not sure how to handle inside of a Python component.

Giulio has graciously provided a method for converting DataTrees to List which has helped me immensely, but it also created this problem for me. It only processes correctly datatrees that have a leading zero value in their path. Example below:



What that forces me to do is either use the Path Mapper to add a leading zero or simply flatten everything to a single branch:

Is there a way to perform the Path Mapper {A} > {0;A} operation on incoming datatree if it has more than a single branch? (if all data is on a single branch it is not necessary).

Thank you,

Giulio's definition for reference:

# written by Giulio Piacentino, giulio@mcneel.com
def TreeToList(input, RetrieveBase = 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] = []
extend_at(path, index+1, simple_input, rest_list[target])
all = []
for i in range(input.BranchCount):
path = input.Path(i)
extend_at(path, 0, input.Branch(path), all)
return RetrieveBase(all)

Views: 1055

Replies to This Discussion

Hi Konrad,

Use the dataTree.RenumberPath method.

Check the attached file.

P.S.

For future questions, please post your code inside of block quotes (") as it will retain the original code's indentation structure.

Attachments:

Awesome! Thank you, and i will make sure to post code inside of block quotes. My apologies.

Actually after further investigation, the method that you provide is not a very flexible/adaptable to other cases. I need something that "prepends" a zero value to any path. That means that if the paths are simple like this: {0}, {1}, {2} or more complex like this {0;1;1}, {1;1;1} etc it just adds a zero to the beginning of the path.
I think that GH_Path.PrependElement() method might be just what i need. Does anyone have an example of proper use of it on an entire data tree of any size?

dataTree.RenumberPaths("0;{i}") method exactly replicates the behavior your described (Path Mapper's {A} > {0;A}). There was no mention of other cases.
For GH_Path.PrependElement(), simply add 0 as an argument.

I would create a new blank data tree and then append all old branches with new prepended paths.

Attachments:

Thank you, this makes all sense now. Thank you gain.

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