Grasshopper

algorithmic modeling for Rhino

Hello,

Is there a python script that works exactly like the mass addition component in the grasshopper interface (result of mass addition and partial results)?

Thanks in advance,

Views: 2231

Replies to This Discussion

It just a for loop to add all the numbers.

I don't know python but the math / loop is the same anywhere. This is how it can be in c#:

private void RunScript(List<double> x, ref object A, ref object B)
{


double massAdd = x[0];
List<double> partialResults = new List<double>();

for(int i = 1; i < x.Count; i++)
{
massAdd = x[i] + massAdd;
double partialResult = massAdd - x[i];
partialResults.Add(partialResult);
}
partialResults.Add(massAdd);

A = massAdd;
B = partialResults;

}

Hi,

you can do this in c# with one line of code:

var massAdd;

massAdd = partialResults.Sum();

Yea I was thinking like you at first but then decided I need to loop anyway for the partial so might as well loop both :D

for the partial results you will need the loop. So take it for both as Michael Pryor said.

Here's a Python solution:

Yess this is exactly what I was looking for. But is it also possible with multiple list (number of lists are variable)? So that the results are also placed in different lists(see attachment). Maybe a loop that runs for each list? Or is it possible to input multiple lists into the Mass addition component?

Attachments:

Afraid, I don't quite follow. Do you mean writing a Grasshopper DataTree that holds multiple lists using GHPython? If so, have a look at this gist (there are also several threads on the forum related to reading/writing DataTrees using GHPython).

Yeah it's possible like Anders said. You just need to loop through your initial nested list and then use GH DataTree to get the result.

Attachments:

Nice example, careful with overriding the standard Python methods/classes/types though, can lead to some gnarly debugging down the road (in this case list):

Just because its funny...

private void RunScript(List<double> x, ref object A, ref object B, ref object C, ref object D)
{
double runningTotal = 0;
A = x.Select(i => new { i, totSum = runningTotal += i });

double runTot = 0;
B = x.Select(i => runTot += i);

C = x.Sum();

D = x.Count;
}

The For Loop will be probably faster, but this shows the use of LINQ.

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