Grasshopper

algorithmic modeling for Rhino

ghpython:performs adds up of second dimension data in a tree

Hi everyone,

I have a question about tree manipulation in ghpython to ask.

I have a tree-structured data, how can I do it in python that i can get the individual adds up result in the second level of the branch?

i.e.

{0;0} {1;0} {2;0} {2;1} {3;0} {3;1} {3;2}

   a        a        a        c        a        c        e

   b        b        b        d        b        d       f

in order to get

{0}    {1}      {2}           {3}

  a        a      a + c      a + c + e

  b        b      b + d      b + d + f

I have tried using GH_Path, treedata.Paths etc.

but have no idea how to perform a looping in the second dimension of a tree.

Any ideas?

Many Thanks!!

Views: 484

Attachments:

Replies to This Discussion

Hi Horus,

please, if possible, next time post a sample of the data you are working with, or make up one definition that would work for you.

In this case, I would suggest using a Path Mapper with the following rule:

{A;B} => {A}

That's all you need. I would advise against writing a script for something that can more easily achieved in a component; also because a tree that enumerates over the first path is usually a creation of some mapping or simplifying already; with other GH components you get {0} as a departing path.

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

Attachments:

Hi Giulio,

Really thanks for your reply!!

At last I just go without using multiple dimension tree. And I totally agree we should never complicate problems.

Here is another question,

In python or other programming language, you can loop the last level of the list using indexes,something like:

list = [[[0,0]]]

for i in range(len(list)):

    for j in range(len(list[i])):

        for k in range(len(list[i][j])):

            do sth

while in ghpython, we can only loop the items through the path, something like:

for path in x.Paths:

    for item in x.Branch(path):

        do sth

This is actually a flatterned list, am I right?

Is there any functions for tree data in ghpython which performs like that in python?(just out of curious and in the purpose of learning :P)

x.Branch(path)

returns a list. That's not a flattened one, it's just the list at the branch.

x.Paths

returns a list with the name of all paths.

Does it help?

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

For example, if we have a tree of data structured with multiple dimensions, say {i;j;k;l;m;n}, using the x.Paths we can only achieve to the last dimension(element) of the tree. while in python, we can achieve any dimension of the list by the i,j,k,l,m,n index, say we only need to achive to the level of a list instead of the element in the list.

I mean of course there are some workarounds, but still not the direct way of achieving a certain dimension of the tree.

sorry if i don't make any sense or did not make myself understandable.

What you say is not entirely true. A path is the representation of {i;j;k;l;m;n}, all together. If you want to see a list-in-list ("nested list") representation of your tree, try this:

https://gist.github.com/piac/ef91ac83cb5ee92a1294

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

Thanks for giving this great reference, and this is the exact method I always use to deal with the tree data in ghpython.

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service