How to Sum a List in Grasshopper (Mass Addition Explained)

How to Sum Elements in a List in Grasshopper - Full Tutorial
Contents

To sum a list of numbers in Grasshopper, use the Mass Addition component: it returns the total in its Result (R) output, and every running total along the way in Partial Results (Pr).

Adding the Mass Addition Component

You can find it in the ‘Maths‘ component tab, in the ‘Operators‘ group. Select it and click anywhere on the canvas to add it to your script.

Grasshopper interface showing where the Mass Addition component is located.

If you prefer to use the component search bar, simply double-click on the Grasshopper canvas where you want the component to be placed, and type “mass addition” into the search bar. Then click on the first result to add the component.

Image showing how to add the mass addition component using the quick search bar.

Mass Addition Outputs: Result and Partial Results Explained

Let’s take a look at the component inputs and outputs. The only Input (I) accepts any list of numbers. These will be the numbers the component adds up.

On the right we get two outputs: The Result (R) contains the sum of all the values we provided. Keep in mind that data structures still apply, more on that in a later section. If we provided a single list of numbers, the output will be a single number.

Example of how to sum elements of a list in Grasshopper with the Mass Addition component

The second result is called ‘Partial Results’ (Pr). It contains a list of all the intermediate steps leading up to the total sum.

Imagine you have a list of ten numbers from 1 to 10. If you were to add them all together, you would start by adding 1 and 2, which results in 3. Then you would add 3 to make it 6. Next you’d add 4 to make it 10. The partial results output contains all the intermediate addition steps that lead up to the final sum. This can be quite useful in some situations.

Understanding the result and partial result of the mass addition component

Running totals are how you place objects of different widths side by side, or stack floors of different heights. You can also use it to create a Non-Uniform Curve Subdivision.

How Data Structure Changes the Sum.

When using the Mass Addition component, it’s important to understand that this component sums up the elements of a list. In Grasshopper, a list is not a generic term, it describes a particular type of data structure.

Let me show how different data structures lead to different results:

Example 1: Sum of Elements in a List

In the example below I’m using the Panel component to both display the contents and the structure of the data. Here the data is stored in single list or branch, which means the Mass Addition component processes the information as expected.

Image showing the Mass Addition component with a list of numbers in a panel as input and the sum of those numbers in a panel in the R output.

Example 2: Sum of Elements in Separate Data Branches

In this second example we have the same numbers, but they are grouped in three different data branches, making up three separate lists. Data branches exist to keep buckets of data separate. This also means that the Mass Addition component won’t ‘cross’ the line of one branch to the next, which is why it returns three separate sums, one for each list.

Image showing the Mass Addition component with three lists of numbers in a panel as input and the sum of each of the lists' numbers in a panel in the R output.

Example 3: Getting the Total Sum of Elements Across All Data Branches

Now sometimes we have numbers stored in several data branches, but we still want to get the total sum of all the numbers. To do that, we need to effectively ‘delete’ the data structure information and bring all the numbers back to the same list. To do that in Grasshopper, we have to use the ‘Flatten’ component. Flattening a data tree means that all branch information will be erased, like felling a tree.

To flatten the input data, right-click on the Input List (I) input of the Mass Addition component, and select ‘Flatten’.

Image showing how to flatten the input of the Mass Addition component to get the total sum of the input regardless of input data structure.

Once we flatten the data, the Mass Addition component will return a single number, representing the sum of all elements in the list.

The small downward pointing arrow on the input give us a visual cue that we flattened the data.

And as expected, we get the total sum of all the numbers across the branches in the Result (R) output.

Image showing a Panel with three lists containing numbers as input for the Mass addition component with the input being flattened and a panel in the R output showing the total sum of all those numbers.

Step-by-Step Tutorial: Mass Addition of Floor Areas

Let’s say we have a script where we create floor slabs from an irregular massing and we want to know the GFA (Gross Floor Area) of the resulting design. This is a typical mass addition task. Here’s how we can approach it:

Basic Floor Generation Script

Before we add the floor areas, let’s generate the floors. The following is a basic setup to generate floors with the help of the Contour component:

  1. We have a referenced Brep (polysurface) from Rhino that we contour with the Contour component. We define a floor height for the contour distance using a Number Slider and stick with the default contour starting location of 0,0,0, and the default contour direction of z (vertical contours).
  2. We turn these closed, planar curves into surfaces by plugging them into a Surface data container component.
  3. We use the Area component to calculate the area of the resulting surfaces.

The Panel below displays the output of the Area component. Each floor and floor area is stored in a separate data branch (marked by {0;0}, {0;1} etc. ).

Example use of the Mass addition component: adding the floor area of a building generated by contours.

Adjusting the Data Structure for the Mass Addition

To obtain the cumulative sum of all floor areas, we must modify the data structure before inputting it into the Mass Addition component. To erase all data branch information and collect all items in a single branch, we can flatten the input.

To flatten the input, right-click on the Mass Addition input and click on ‘Flatten’. All data is collected in a single branch.

Example of Flattening the I input of the Mass Addition component to get the total of the input.

Note: Flattening the data structure worked in this particular case due to the single massing. Depending on the data structure of the numbers you want to get the sum of, you will need to adjust the data structure differently. This may involve trimming the data structure for example. Always check and modify the data structure of the Mass Addition component input to get the sum you want.

Summing values like this is also how you build a fitness value for optimization with Galapagos.

Happy designing!

Grasshopper Pro

Followed this tutorial? Learn to build the next one yourself.

Grasshopper Pro is a four-module course for architects, built on one method: Design → Logic → Grasshopper. Start with the design intent, turn it into logic, then build it. You'll work on massing studies, façades and patterns, from a blank canvas.

70% of students who finish rate their Grasshopper skills as intermediate or advanced · 22 Trustpilot reviews, every one 5-star

See the curriculum →

$197 one-time · lifetime access · 30-day refund

Article by

Picture of Thomas Jeremy Tait
Thomas Jeremy Tait
Founder of Hopific. Expert in parametric design for architecture. Previously worked as a Senior Architect at Snøhetta for 10 years.

Quickstart Guide to Grasshopper

Download your free guide and transform your designs today.

More Great Reads