Houdini VEX - Snippets

General

Array of Unique (detail)

int nprim = nprimitives(0);
s[]@mat_names = {};

int count = nuniqueval(0, "prim", "shop_materialpath");
for (int i = 0; i < count; i++) {
    string val = uniqueval(0, "prim", "shop_materialpath", i);
    push(s[]@mat_names, val);
}

Transform

We have control over the distribution of various sizes (bias), the size over life (note that we use the internal @nage i.e. normalized age an attr that comes by default when we do a popsim), min and max scale as well as an overall multiplier.

pscale

float ps_life = chramp('ps_life', @nage);
float ps_bias = fit01(chramp('ps_bias', rand(@id)), chf('ps_min'), chf('ps_max'));
@pscale = ps_life*ps_bias*chf('ps_mult');

String

Regex

Replace

string names[] = {'monkeys_12', 'X_AE_A-12', 'my_name', 'Forty_Two____42'};
// remove trailing digits including underscore/dash...
// ...matching as few as possible in our capturing group
// note that we use the first capturing group in our replacement string
string regex = r'^([a-zA-Z_]*?)[-_0-9]*$';

int pnt;
foreach(int i; string name; names){
    pnt = addpoint(0, 0);
    name = re_replace(regex, r"\1", name);
    setpointattrib(0, 'name', pnt, name);
}

Formatting

Zero padding

int i = int(rand(@ptnum+chi('seed'))*(chi('max_num')+1));
s@z_padded = sprintf('hello_%03i', i);

Edit Primitves

adjustPrimLength

Here we can use a measure sop measure the perimeter of the prim. This is needed for the function to work. Note that if we does not clamp the length it will extend the prims to match the length of the longest prim.

#include <groom.h>

float length = clamp(chf('length'), 0, @perimeter);
adjustPrimLength(0, @primnum, @perimeter, length);

Functions

frac()