So when I first started learning Houdini I would jump in and try to recreate simple scenes that I would often do in Cinema 4D. One of them was to randomize uniform scale on a bunch of geometry. Simple enough. I would start to create the scene and then realize I had no clue how to do it. WTF! It can’t be that hard to randomize scale in Houdini.
Well, it isn’t. It’s actually really easy. I just didn’t know about one important attribute known as
@pscale. I bet if you’re just starting out, you have fallen victim to it as well. So this
#QuickTip is for all those just starting out. I’m going to show you a few different ways to randomize scale in Houdini. Keep in mind this is just a starting point. It’s up to you to take it from here adding extra functionality and making it all nice and random.
Attributes are the secret sauce to a lot of things in Houdini. You need to understand them because most things in Houdini revolve around attributes. I wrote a post that explains them a bit. The use of attributes is a simple concept to grasp but typing in arbitrary keywords could be a bit intimidating when first starting out. Especially when you don’t know what keywords to type. Most of us are just accustomed to moving sliders around or checking off a checkbox. First, a scene setup.
Drop down a Geometry SOP. Dive in and do the following:
- Create a Grid SOP, a box SOP, and a Copy to Points SOP.
- Connect the Box to the first input of the Copy to Points SOP.
- Connect the Grid to the second input of the Copy to Points SOP.
The @pscale Attribute
You should now see a bunch of boxes copied to each point on the grid. Basically a Cinema 4D cloner. What we want to do next is randomly scale each box. This requires a
@pscale attribute. This attribute is the uniform point scale of the geometry.
So you may be wondering where does this
@pscale attribute go? On the box side or grid side or after the Copy to Points node? Who bears this responsibility? Well, what you want is to vary the scale of each box.
Since the boxes are being copied to the grid’s points, each box will read the attributes available on each point on the grid. The boxes take on whatever value the scale of the point is if the
@pscale attribute is available.
So if you want to vary the scale of each box, you have to change the scale of the points on on the grid. If you randomize the point scale, when each box is copied to each point on the grid, each box will have a different scale value. The box will read the
@pscale attribute in order to scale itself. I think I’m confused myself. It’s easier to understand in practice.
Random @pscale with a Point Expression SOP
The first method of creating a random
@pscale is by using a Point Expression SOP. Hit
tab and type out point. It’s the node with the fashionable red cowboy hat. Insert it after the grid and change the attribute parameter to Scale(pscale) and under the VEXpression parameter dropdown, select Constant Value. Now you can scale your boxes up and down using the slider.
If you middle-mouse click the Copy to Points SOP, you should now have a
@pscale attribute. Open the Spreadsheet to see the attribute with its corresponding values. However, we need to randomize each value because right now all the point scales are the same. Boooooring!
Go back to the VEXpression dropdown and you should see a few presets for Random values already setup for you. Select any of them and you should see randomly scaled boxes. The parameter field is pre-filled with an expression and you can dabble with it to your heart’s content. Also, check your Spreadsheet and see how the values for the points have changed. Get into the habit of using your Spreadsheet!
Random @pscale with an Attribute Create SOP
Another way of creating the
@pscale attribute and randomizing it is by using the Attribute Create SOP. This one takes some typing but very little. Disable or remove the Point Expression sop and drop down an Attribute Create SOP right after the Grid node. First thing you want to do is change the name parameter from attribute1 to pscale. It’s just pscale. Do not include the
Once you do that, your boxes will disappear. Check your Spreadsheet and you should see the
@pscale attribute along with 0.0 for all the point values. All the default values are fine so the only other thing you need to do change is the first field of the last parameter, which is Value. This will set the value for the attribute you just created, which is
@pscale. The default is 0 so this is why the boxes scaled down. Enter 1 in the first field box. Remember this is a one dimensional float value (uniform scale) so you only worry about the first value.
This is great but all our boxes have a value of 1.o. In order to randomize the value we need to use an expression in the parameter field similar to what the Point Expression SOP automatically did for us.
For this we reach for the
rand(value) function. This handy function takes a seed value and returns a pseudo-random number from 0 to 1. A technique and one you will see often used is passing in the
@ptnum attribute as the seed value. This is the current point’s number for the geometry. This works quite well so we will use it but feel free to get fancy with it. Just enter
rand(@ptnum) and you should now have randomly scaled boxes.
Random @pscale with a Point VOP
The third method of creating random
@pscale is by using a Point VOP. VOPs is the visual version of writing out manual VEX. This one gives you a bit more flexibility since you can really customize your output by doing various math operations on your values. After all, you are programming but in a visual manner.
Add a Point VOP node after the grid node and dive inside. Think of this node as a container for all your operations that will affect the points on the geometry. Once inside the Point VOP you first want to add a Bind Export node since this is how you will create the
@pscale attribute. Make sure you create a Bind Export as opposed to a regular Bind. Enter pscale (without the @ symbol) into the Name parameter field. The rest of the default parameters are fine. Notice you can change the data type of the attribute depending on what type of data you are creating.
Now we’re going to randomize. Search for random. There are several nodes that generate random numbers but we will stick to the simple Random node. Plug the output of the Random node into the input of the Bind node. Take the position (pos) input from Random and plug it into ptnum of the geometryvopglobal1 node. The ptnum attribute is the geometry point number. In this case, it’s the grid’s geometry. If you jump back up a level and check your Spreadsheet or Info panel, you should see the
@pscale attribute attached to the geometry. We did the same thing as typing
rand(@ptnum) but with nodes.
Random @pscale with an Attribute Wrangle
The last method we are going to look at and the one I find the easiest, fastest and most flexible is with the Attribute Wrangle. At the end of the day, all these methods are essentially doing the same thing just in slightly different ways. Again, this is just a starting point. Everyone has their own way of doing things and as Paul Ambrosiussen said in my Few Words with him:
There really is no “wrong” way of doing things in Houdini as long as you reach your desired end result.
So add an Attribute Wrangle right after the grid and before the Copy to Points SOP. Type in wrangle to quickly get to it. Make sure the Run Over parameter is set to Points in the Attribute Wrangle. We are going to use the same expression we used in the Attribute Create SOP. There are just some stricter rules you need to follow as far as syntax goes. Type in the following code in the Wrangle:
@pscale = rand(@ptnum);
That’s it! We start off by typing in the attribute name. Notice here we need to use the @ symbol. Next, we use the same function we used in the Attribute Create method along with the same seed value. Make sure you end the line with a semi-colon. It goes without saying you can get really fancy here with all sorts of operations and even create parameter sliders to control your values. Not that you can’t do that with the other methods but I personally find the Wrangles the cleanest way to do it.
There you have it! Four methods to randomizing scale in Houdini using the
@pscale attribute. It’s easy once you know and knowing is half the battle. Also, by no means are these the only methods. There are other methods but they are not used as much anymore since SideFX has provided better and more efficient tools. An example is the the old Point SOP. SideFX recommends you start using the Point Expresion SOP instead. Did anyone say
By the way, if you know of other methods, I would love to hear about them. Tweet me!
So in good fashion or just plain scatterbrained, I forgot of one of the easiest methods of all. Just goes to show you that sometimes we get caught up in the shiny toys that we miss things right under our noses. Thanks to Nnois for pointing it out to me. I’m including this because it was on the list of methods but completely missed it.
Random @pscale with an Attribute Randomize
If you don’t know about the
@pscale attribute, this one is easy to miss mainly because you won’t know what attribute to look for. However, once you know, the SOP does all the work for you without having to type out any code or even change a setting. Just point it to the attribute and you are done.
If you do a search for randomize, you are going to come up with the Attribute Randomize SOP. Throw it into your network and by default it uses the point color attribute
@Cd. Just replace Cd in the Attribute Name field with pscale and check out those random cubes. The nice thing about this node is that you have the ability to change the random distribution function. Don’t like Uniform distribution? Try out Exponential or LogNormal. It also gives you the ability to set mix and max values. All without you having to write a line of code.