Website powered by

Houdini: Using Python to Use Cumulative Sum

Making Of / 09 September 2019

Hip file is here;
https://gum.co/euIJg

In this article I explain what I've done to make Cumulative Sum using Python in Houdini.

What is a cumulative sum?

According to Wolfram Mathworld,

cumulative sum is A cumulative sum is a sequence of partial sums of a given sequence. 

Let's say you have a list like {1.2, 3.2, 0.6, 1.5, 7.5}, Then you want to get a list of its cumulative sum {1.2, 4.4, 5.0, 6.5, 14.0}.

So, how do I recreate in Houdini?
As usual I tried in VEX first. But unfortunately the consequence isn't what I expected.
And this led me to trying in Python SOP. 

What I was trying to do is that placing randomly selected shapes(see images below) next to each other.

And the result is below;

Randomly Selected Shapes

First of all, I created 3 basic shapes and connected them to switch node so one of them are randomly selected.

In switch SOP node, I put an expression below;

fit01(rand(detail("../foreach_begin3_metadata1/","iteration",0)+ch("../seed/seed")),0,2)

ch("../seed/seed") is a null SOP node I created.

Copy to Point

Next, I added a point by using add SOP node. The position the point is set to {0,0,0} which is the center of the world position.
Then copy the randomly selected shapes to the point inside the foreach SOP node.

At this point I had  several shapes but with uniform scale.

Random scale and get each length

I created another foreach SOP to make each shapes randomized scale, and get each length.

To get the length I did a math in VEX;

And the result is below;

Get cumulative sum in Python

At attribute wrangle4, I added a new attribute that slide "length" attribute one step.
I called them "lastLength".

From now it's python time.
I wrote comments for each function to understand what I've done in each of them. 

And the result is below;
As you see, a new attribute called "Accum" was added.
This is the one doing cumulative sum.

Then there is another foreach loop.
Inside it, I transfer Accum attribute from primitive attribute to point attribute.
And in VEX, each shapes are moved their position so each of them are next each other.  

  

What can it be used for?

I did this to make procedural frame for paintings;
But this system can be used for various things which I want to find out from now on.

You can get the hip file here;
https://gum.co/euIJg