# Difference between revisions of "HoudiniVex"

So you've pushed past hscript, ignored python for now, and seen the awesomeness of vops. You'll also probably start to get curious as to why there's at least 4 different ways to define point attributes, all with various idosyncrasies. Time to sweep all that away with Vex, specifically with the Wrangle Sop.

Wrangle Sops let you write little bits of Vex code, and come with a few UI niceties to make them quick and efficient to use. They come in various flavours (point, prim, detail, attribute), they're all the same thing, just with the 'what do i operate on' menu switched to point/prim/detail.

Best way to get into vex and wrangle nodes is to put down a 40x40 grid, append a point wrangle, and follow along:

### Create a new attribute

You want to define a new point float attribute 'foo'? Just type

@foo;

Hit ctrl-enter, look in the geometry spreadsheet, there you go, float attribute. The @ tells houdini that you want this to be an attribute, the default type is float.

You want it initialised?

@foo=1;

You want a vector attribute? Prepend 'v' before the '@'. To initialise, use curly braces if its just numbers, or use set() if you're doing something with functions or other attributes:

v@myvector={1,0,3};
v@other=set(0, @P.x, 1);

You can set the attribute type using the appropriate prefix:

// common ones
f@foo = 12.234 // float
i@foo = 5 // int
v@myvector={1,0,3}; // vector

// less common ones
w@george;  // quaternion, a 4 value vector
m@transform; // matrix

// there's more, see the docs. :)

### Get existing attribute values

You want myvector to get its values from the point position?

v@myvector=@P;

The @ symbol is used to get attributes as well as set them. All the attributes you see in the geometry spreadsheet, you can access them by sticking @ in front of their name.

You want it 1.5 times N?

v@myvector=@N*1.5;

To access an individual attribute of a vector or colour, use @attribute.channel. Eg to get just the y value of each point and divide in half:

@foo=@P.y / 2;

To set it is similar. Eg to set the red channel of each point to the sine of double the x position:

@Cd.r = sin( @P.x * 2 );

Vector attributes like Cd or P let you refer to components in several ways, use whatever is convenient. The component can be r/g/b, x/y/z, [0]/[1]/[2].

// These all do the same thing, set the first component of the vector:
@Cd.r = 1;
@Cd.x = 1;
@Cd[0] = 1;

// As are these, all setting the third component:

@P.z = 6;
@P.b = 6;
@P[2] = 6;

### Implicit vs explicit attribute type

Note that wrangles implicitly know certain common attribute types (@P, @Cd, @N, @v, @orient), but if you have your own attributes, Houdini will assume its a float unless told otherwise, easy mistake to make.

To ensure Houdini does the right thing, prepend the type. Eg, you've set @mycolour as a vector, and try to use it in a wrangle:

@Cd = @mycolour; // This will treat it as a float and only read the first value.

// GOOD
@Cd = v@mycolour; // Explicitly tells the wrangle that its a vector.

I always forget to do this, shout at my wrangles for a bit, then finally remember to stick 'v' in front of my attribute names. No doubt you'll do the same. :)

### Create UI controls

You want a slider automatically created to scale the sine wave for the previous example?

@Cd.r = sin( @P.x *  chf('scale')   );

Hit the little plug icon to the right of the text editor, Houdini scans the vex code, realises you've referred to a channel that doesn't have a UI yet, and makes a channel at the bottom of the wrangle UI named 'scale'. Start sliding it around, you'll see the colours update.

There's several channel types you can use. ch() and chf() both create a float channel. For the others:

// An int channel
i@myint = chi('myint');

// A vector channel
v@myvector = chv('awesome');

// A ramp channel
f@foo = chramp('myramp',@P.x);

The last one is really handy, in one line you get to read one value (@P.x), remap it via the UI ramp widget, and feed that to @foo. I end up using loads of these all through my setups. It assumes the incoming values are in the 0-1 range, you often have to fit the values before feeding to the ramp. The fit function goes fit( value, oldmin, oldmax, newmin, newmax). Eg, remap X between -0.5 and 6, then use a ramp to feed those values into the red channel:

float tmp = fit(@P.x, -0.5,6,0,1);
@Cd.r = chramp('myramp',tmp);

But why stop there? Define the start and end points with UI sliders!

float min = ch('min');
float max = ch('max');
float tmp = fit(@P.x, min,max,0,1);
@Cd = 0;  // lazy way to reset the colour to black before the next step
@Cd.r = chramp('myramp',tmp);

### Customise the UI elements

The plug button is a convenience function, it just scans for any channel references, and creates the default type, with a default value. Once the channel is made, you can right.click on the wrangle node and choose 'edit parameter interface' (or use the gear menu from the parameter panel), and change float ranges, the default value, the labels, whatever you want.

A handy thing I often do is to convert the default vector channel to a colour channel. I'll make as many vector channels as I need in vex, eg:

v@colour1 = chv('col1');
v@colour2 = chv('col2');
v@colour3 = chv('col3');
v@colour4 = chv('col4');

Then I'll hit the plug button, edit the parameter interface, shift-select all the channels I made, and change the type field to 'color', and to be extra saucy, change 'show color as' to 'hsv sliders'. Most handy.

### Common functions

These appear in 99% of my vex wrangles:

• fit() - take a number between 2 values, fit it between 2 other values, usually 0-1. Eg, remap @u from range -5,20 to 0-1: foo = fit(@u, -5, 20, 0, 1);
• rand() - generate a random number between 0 and 1. Usually feed it the point id, so each point gets a random number: foo = rand(@ptnum);
• sin(), cos() - as you'd expect, but in radians.
• length() - measure the length of a vector. Eg, measure the distance of a point from the origin: dist = length(@P);

### Built-in attributes

There's a few built in variables you can use, easiest way to see them is to put down a point vop, and look at the global params. Here's the more common ones:

• @ptnum - the point id
• @numpt - total number of points
• @Time - current time, in seconds
• @Frame - current frame
• @primnum - the primitive id
• @numprim - the total number of primitives

### Example: Random delete points by threshold

After Matt Ebb showed me this, I use it a million times a day. Scatter some points, put down a wrangle with this:

if ( rand(@ptnum) > ch('threshold') ) {
removepoint(0,@ptnum);
}

Use the plug button to create the threshold slider, now slide it up between 0 and 1, you'll see points get randomly deleted. What's going on:

• rand(@ptnum) -- each point gets a random number between 0 and 1 based on its id
• > ch('threshold') -- compare that random number to the threshold slider. If it's greater than the threshold...
• removepoint(0,@ptnum) -- ...delete the point. 0 means the first input, ie whatever you've connected into this wrangle, and @ptnum is the reference to the point.

### Example: Wave deformer

We'll make concentric sine waves that radiate from the center. That means we'll need to measure the distance of each point to the origin:

@d = length(@P);

We'll feed that to a sin(), and use that to directly set the y-position of each point:

@P.y = sin(@d);

Good, but the waves are too big for our default grid. Lets multiply @d by some factor, that'll give us more waves. Change the previous line to read:

@P.y = sin(@d*4);

Ok, but lets make that driven by a slider instead:

@P.y = sin(@d*ch('scale'));

Hit the plug button, play with the slider (you can push the slider past 1), see the waves move around.

To make the waves animate, add time to the inner term.

@P.y = sin(@d*ch('scale')+@Time);

wait, they're moving towards the center. Lets make it negative time:

@P.y = sin(@d*ch('scale')-@Time);

Or better, control it by a slider so you can drive the speed:

@P.y = sin(@d*ch('scale')+(ch('speed')*@Time));

That's messy, lets tidy up the whole thing. Nice thing about wrangles is you can split across multiple lines for readability:

@d = length(@P);
@d *= ch('scale');
@speed = @Time*ch('speed');
@P.y = sin(@d+@speed);

Lets add 2 more things, a control for the maximum height, and a distance based falloff.

Height is easy, whatever the final result is, we'll multiply it by a channel reference; if its 1 it'll be unchanged, 0 will cancel out, other numbers will scale appropriately:

@P.y *= ch('height');

And finally a ramp to control the falloff. A ramp widget expects a variable in the 0-1 range, so the first thing we'll do is take the distance variable, and use fit() to map it between 1 and 0 (note we go 1 0, not 0 1; we want the center to be of maximum height). The start and end points will be controlled by channels:

@falloff = fit(@d, ch('start') , ch('end') , 1,0);

A ramp channel expects a name and a variable, the variable will be remapped through the ramp curve. We'll take the result and directly multiply it against P.y:

@P.y *= chramp('falloff', @falloff);

A minor annoyance here is that I expected the start and end to be in worldspace units, but the 'end' channel was a large number. I realised that's because we'd already multiplied d by the scaling channel to control the number of waves. A quick reshuffle of the code fixed that, here's the end result:

@d = length(@P);
@speed = @Time * ch('speed');
@falloff = fit(@d, ch('start'), ch('end'),1,0);

@P.y = sin(@d*ch('scale')+@speed);
@P.y *= ch('height');
@P.y *= chramp('falloff', @falloff);

### Attributes vs variables

The examples given above all use the @ syntax to get and set attributes on points. If you have more than a few wrangles, or if you don't need those attributes outside the wrangle, this is a needless amount of data you're throwing around.

Instead, you can create variables that only exist within the wrangle. The syntax is similar to other c-style languages; define the type with a full word, and don't use a prefix on the variable:

float foo;
vector bar = {0,0,0};
matrix m;

foo = 8;
bar.x += foo;

Point attributes and vex variables live in seperate worlds, so you can have one of each with the same name, and they happily co-exist. I tend to avoid this in practice, easy to add an @ where you didn't mean, or remove one where you need it, and break things:

float dist = length(@P);  // a local vex variable that only exists within this wrangle

@dist = dist;  // create a new point attribute, @dist, and assign the local variable 'dist' to it. Worlds collide!

Rewriting the previous example to use variables, and do the formal thing of declaring variables first so its clear to see whats going on:

float d;
float speed;
float falloff;

d = length(@P);
speed = @Time * ch('speed');
falloff = fit(d, ch('start'), ch('end'),1,0);

@P.y = sin(d*ch('scale')+speed);
@P.y *= ch('height');
@P.y *= chramp('falloff', falloff);

### Wrangles vs hscript vs vops vs everything else

Vex is as a general rule always going to be faster than using hscript, and will scale much better.

Vops are great, but often its the simple things that you can do in 2 lines in vex, that would take 7 or 8 nodes in vops. That said, certain operations are easier in vops, like using noise or loading other patterns, or building stuff when you don't exactly know what you're aiming for yet. After you're done, you can always re-write your setup in a wrangle if you think its better.

Re other sops, a wrangle can do the work of several other sops, often more efficiently, with the ability to make UI elements easily. Some examples:

• point sop - a lot of older tuts use point sops. Don't. Just say no.
• attribcreate sop - faster and easier in a wrangle. The only reason you might wanna use an attribcreate is to get local variables, but you'd only use them with a point sop, which we already agreed suck.
• vop bind and vop bind export - getting and setting point attributes is way easer with @ shortcuts than in vops.
• promote parameters in vops - again, ch('foo') vs r.click, bind, fiddle with names, realise its wrong, get parameters in a semi hung state, ugh
• channel referencing in hscript - the auto binding with ch() is the bees knees. That little plug button is the best thing ever.

That's being a little trite, all sops and vops are useful at some point, but the speed and elegance of wrangles is hard to beat.

### Mixing vex and vops with inline code

Often I'll start something in vops, then realise part of my network would be much neater as a few lines of vex. When that happens, I'll drop in an 'inline code' vop, and I get something that looks very similar to a wrangle node. I'll then code whatever I need to code, and it happily co-exists with the vops around it.

The syntax is a little different, took a few goes to get used to it. First is that you don't need the @'s, so @P.y is just P.y.

The second is how to define the output of the node. By default there are no outputs, at the bottom of the parameter interface you go to the first disabled output, set its type (eg vector), and give it a name (eg, P). You then see a 'P' appear on the right side of the node, ready for you to connect to other things. Cos I'm an idiot, I tend to name these outputs explicitly, eg 'outP' rather than 'P'.

To assign values to that output, use a \$ prefix. Eg, here I want to drive P.y by Cd.r. I've defined the output as outP on the UI.

P.y=Cd.r;
\$outP=P;

Note that setting P.y like that in a wrangle (with the @ prefix of course) would immediately update the geo, it doesn't in an inline code vop. P here just exists within the node; we then have to be able to export it via outP, then plug it to the global output P variable to see any effect.

### Get values from other points, other geo

Groundwork for the next step (and probably mentioned above in passing).

If you want the colour of the current point, you use @Cd. But what if you want the colour of point 5?

float otherCd = point(0, "Cd", 5);

Ie, the point() function lets you query attributes of other points (the first 0 means 'the first input to this point wrangle'). If you had another mesh connected to the 2nd input of the point wrangle, and you wanted to know the colour of point 5 in that mesh, change the geo number:

float otherCd = point(1, "Cd", 5);

Often this is used for transferring or blending attributes between similar meshes. Eg, you've got 2 meshes, and want to set the colour to be the addition of both. The point numbering will be identical, therefore you can use the same ptnum to get the 2nd mesh, and do what you need:

vector otherCd = point(1, "Cd", @ptnum);
@Cd += otherCd;

### Blurring attributes with vex and point clouds

Another thing that I learned but didn't really understand, forgot, learned again, forgot, then feared for a while, and now finally have an understanding of (I think).

You have Cd on a grid, you want to blur it. One way is to iterate through each point, look at its neighbours, add up the result, and divide by the number of neighbours. Because it's a grid, and the point numbering is consistent, you could do something like

int width = 50; // say the grid is 50 points wide
vector left = point(0,'Cd', @ptnum-1);
vector right = point(0,'Cd', @ptnum+1);
vector top = point(0,'Cd', @ptnum+50);
vector bottom = point(0,'Cd', @ptnum-50);
@Cd = left + right + top + bottom;
@Cd /= 4.0;

Works, but clunky, and it only works with a grid. Using the neighbour() and neighbourcount() functions is more generic (borrowed from odforce post):

int neighbours = neighbourcount(0, @ptnum);
vector totalCd = 0;
for (int i = 0; i < neighbours; i++)
{
int neighPtnum = neighbour(0, @ptnum, i);
totalCd += point(0, "Cd", neighPtnum);
}
@Cd = totalCd/neighbours;

Groovy, nice and generic now.

However, there's an even shorter cleaner way, using point clouds:

int mypc = pcopen(0, 'P', @P, 1, 8);
@Cd = pcfilter(mypc, 'Cd');

pcopen() is designed to open point cloud (.pc) files on disk, but by using the 0 reference, the wrangle will treat the incoming geo as a point cloud. You need to tell it how it will find values in the cloud, 99.999% of the time you'll look up positions in the cloud ('P') using the location of the current point (@P). It searches to a maximum distance (1), and returns a maximum number of points (8). Pcopen() returns a handle so you can refer to the point cloud later, here I store that in variable 'mypc'.

pcfilter() purpose is to look at the results from a point cloud, and return the filtered (blurred, averaged, call it what you want) result of the attribute you specify. Here, we tell it to use the cloud we just opened, and look up the Cd attribute. Because pcopen returned the closest 8 Cd results near the current point, pcfilter will return the average of those 8 points. In other words, we've just blurred Cd by its surrounding 8 points, essentially a 1 pixel blur.

Adding some channels, we can control the blur with a slider:

float maxdist = ch('maxdist');
int blur = chi('blur');
int pc = pcopen(0, 'P', @P, maxdist, blur);
@Cd = pcfilter(pc, 'Cd');

maxdist doesn't really affect the result, its more for speed; if you know the search never has to be greater than a certain distance, these point cloud functions can run a lot faster. Also note that pcfilter() does a more clever job than the neighbour based method earlier, it knows to give far away points less importance than closer points.

Another use for this is vex based smoothing function, try this on a box in polygon mesh mode with lots of axis divisions . The only difference here is rather than blurring Cd, we're blurring P:

float maxdist = ch('maxdist');
int blur = chi('blur');
int pc = pcopen(0, 'P', @P, maxdist, blur);
@P = pcfilter(pc, 'P');

There's a variety of functions to manipulate point clouds, most are prefixed with 'pc', so are handily grouped together in the vex docs: http://www.sidefx.com/docs/houdini15.0/vex/functions/ (skip down to the 'point clouds and 3d images' section)

Curious aside 1: The grid based way of doing this is basically what convolution filters do in image processing. A specific version of this that looks like an edge detect filter is called a laplacian filter, a word which I've seen come up now and then, but didn't really understand. Now I sorta do. More info: http://homepages.inf.ed.ac.uk/rbf/HIPR2/log.htm

Curious aside 2: If you've not used point clouds before, you might wonder why they exist, how are they better than doing point() or neighbour() lookups etc. They're just another way of storing 3d data, but by stripping out edges and polygons, some shortcuts can be taken to make certain operations be much faster. Getting the filtered results of sub-sections of geometry is one such operation, others are quickly iterating through near points, or approximating chunks of far away points with a simple average. The past 10 years of rendering has relied heavily on point clouds for effects like ambient occlusion and subsurface scattering, but have fallen out favour as brute force path tracing has become viable. Still, can be a handy trick for geometry processing in Houdini.

### Get attributes from other inputs

As mentioned earlier, you have 2 similar meshes feeding into the one wrangle, and you want to access the matching P of the 2nd mesh:

vector otherP = point(1,"P", @ptnum);
// do something with it

An alternative method is to prefix the attribute you want with @opinput1_ . This implicitly uses the same ptnum, saving you a call to point() :

vector otherP = @opinoput1_P;

The same rules apply as for normal @ syntax, it will guess the type of known attributes (P, N, v etc), anything else will default to float unless you tell it otherwise:

float foo   = @opinput1_myfloat;   // fine, default is float
float bar  = f@opinput1_myfloat;   // explicit type
float myvector  = @opinput1_thevector;   // bug, as thevector will be returned as float
float workingvector  = v@opinoput1_thevector;   // better

Of course to access any other geo connected connected to the 3rd or 4th slot, increase the number (you might've guessed by now that 0 is the first input):

float two   = @opinput1_myfloat;
float three   = @opinput2_myfloat;
float four   = @opinput3_myfloat;

I wish they'd picked a shorter string that opinputN_, as in practice somtimes it almost feels like point() is faster, but hey, nice to have options. :)

### Arrays

Standard vex arrays are like most c-style languages:

int myarray[] = {1,2,3,4};

int foo = myarray[2];

You can create array attributes too by inserting '[]' between the type declaration and the @:

i[]@things = {1,2,3,4};

int foo = i[]@things[3];

You can use the opintputN syntax from before too, looks messy but it works. Eg, reading a float array from the 2nd input and storing it in a local vex variable array:

float otherthings[]  = f[]@opinput1_thearray;

int foo = otherthings[4];

You can append the array index you want and get even more hard to read:

int foo = f[]@opinput1_thearray[4];

### Further learning

The 'bees and bombs' thread on odforce is a good place to start; lots of self contained looping animations created (mostly) in vex:

The wrangle workshop isn't bad, but if I'm gonna critique moves a little slow:

Shadertoy is a great place for inspiration. The code isn't vex, and a lot of the files use techniques that don't directly map onto Houdini, but even then its handy to see a visual example of a technique you might want to implement, and the core algorithm can often be remapped to vex easily enough:

Processing, various javascript graphics libraries, older actionscript, all great to steal ideas from.