22. September 2023

Working with Arrays – Part 1

Arrays are the key data structure when it comes to accessing and manipulating large amounts of data. While high level modeling functions work directly on geometry in every situation where a pre made modeling node is not available we need to handle the components of Geometry on our own.

Two types of arrays are heavily used in this context, arrays of vectors (Points) and arrays of indexes (Selections). Therefore these two will be used mainly to demonstrate the use of arrays.

Fundamentally arrays can be in two different ways, as a whole, or element by element.

Calculating a single result from an Array

A good example for an operation that works on a whole array but delivers a single value is the Average operation.

In this example the average of four vectors is calculated.

For comparison let’s have a look how this would work as an element by element operation

Both setups do the same thing, but working directly on the array makes things a lot simpler.

If you already have an iteration there is a third alternative, there is also an Average node that works on iterated values (Stream of Data)

All three variations can be found in this example file.

Example-Array-Average.c4d

Obviously you would not use the iteration/LCV setup if you have a ready made solution at hand, but if it is not an Average you need but a non standard operation it might be necessary to go down this route.

Modifying all elements of an Array

If you want to add a certain value to each element of a Vector you can use the Scalar Arithmetic node. It offers a couple of basic arithmetic functions that you can apply.

Like with the Average node there is an alternative based on an Iteration.

Iterate Collection iterates over all elements of the Array and Assemble Collection creates a new array with the new values. This would be the solution if you need both, the original array and a new one, but if only one is needed the setup can look like this.

What happens here is that the original array is reused by first reading the element with the Iterate Collection node, then modifying the value and finally writing the new value back at the correct place in the array. This approach saves memory and speed compared to the creation of a new array. Still, using Scalar Arithmetic is the better solution, provided it offers the function you require.

All variations are included in the example file

Example-Array-Scalar-Arithmetic.c4d

Take aways

  • If possible use build in functions to manipulate array values, they are usually more efficient
  • If possible modify the existing array instead of creating a new one
  • If you use an iteration make sure the Scope (blue wire) determines the correct end of the loop
  • Nodes Spline groups are very handy for prototyping and showing information in the viewport