Grasshopper

algorithmic modeling for Rhino

This should be a relatively simple question but I cant seem to work it out 

I have a datatree which has two branches, each branch contains multiple floats,

using python how could I sum each data tree branch and update the datatree so that it contains only the sum of each branch,

Thank you!

Views: 1935

Replies to This Discussion

If you set your input parameter to "List Access" instead of "Tree Access" you can just let GH do the rest (see attached). Otherwise you will have to operate on DataTrees which is also an option, but quite a bit more involved.

Attachments:

Here are the data trees options (clearing branches then inserting sums, or creating a new data tree and appending sums to its branches).

Attachments:

Heh, I just did the same. Seemed appropriate :)

Attachments:

Nice!

Hi Djordje 

Thanks Im actually doing it in a component - (Creating a datatree in the component then summing each branch in the component)

Thank you Anders and Dominik too!

Hi Anton,

Nothing changes. You can still clear the branches of that created data tree and insert its sums instead (if you do not need that previous data tree). Or if you need both of them (initial and sum one), create a new data tree and then add the sums to its branches.

Hi Andres

Can I ask you a question, in the code that you've written here:

import Grasshopper as gh


a = gh.DataTree[float]()
for i in range(x.BranchCount):
a.Add(sum(x.Branch(i)),x.Paths.Item[i])

the line - gh.DataTree[float]() within the [] is declaring the type of the objects that the datatree takes right? 

As the Grasshopper SDK DataTree constructor needs a type - DataTree(T) Constructor.

Why do we use [] to denote the type? shouldn't we use () like in the Grasshopper SDK also are these types python types? I tried using string in the type but it didnt work. 

I'm a bit confused about this

Thank you!

Anton

Hi Anton,

The question is not directed to me, I've seen it today, and just in case Anders is preparing for tomorrow's workshop:

That's how you define types in ironpython (which is what grasshopper and Rhino are using). If I remember correctly you code both in C# and python. The C#'s  <someType> syntax in ironpython is: [someType].
Example of usage is DataTree in this case, or System.Collections in case you need them (few RhinoCommon methods for some strange reason do not not accept standard builtin data types:

import System

typedList = System.Collections.Generic.List[str]()

Grasshopper documentation lacks syntax examples for python, that's why there was no "Grasshopper.DataTree[]()" example in there.


str type sometimes does not work. You can use the System.String instead, it's the same:

newTree = Grasshopper.DataTree[System.String]()

Again apologies for intruding, and hope Anders or Giulio could add up more on this.

Cheers Djordje, we're actually already well into it. Dave Reeves made me a nice hat:

lol, 

Many congrats to the model and the creator.

:)

Nice one modelled using GH?

Indeed, all copyrights owned by Mr.Reeves :)

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service