Houdini VEX - Snippets


Partition points

Here is a vex version of a techique I saw John Kunz use in Vops. Note that the points comming in on the second input will be the “cluster centers”. Theese points can be generated anyway you want, for instance by the scatter node or by fusing “the original points” and with the snap distance control how many centers you want.

Point wrangle

int npt = nearpoint(1, @P);
vector p = point(1, 'P', npt);
vector relbb = relbbox(0, p);
if(relbb.y <= chf('threshold')){
    @my_attrib = 1;
else {
    @my_attrib = 0;

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);


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.


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');




Using capture group and 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);

Slashes to underscrore

string names[] = {'/mat/one', '/mat/two'};

foreach(int i; string name; names){
    int pnt = addpoint(0, 0);
    name = re_replace('/', '_', name);
    setpointattrib(0, 'name', pnt, name);


Zero padding

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

Edit Primitves


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);


Scale UVs around UDIM center

int u = int(floor(v@uv[0]));
int v = int(floor(v@uv[1]));
int udim = 1000 + u+1 + v*10;
i@udim = udim;

vector2 center = set(u+.5, v+.5);
vector disp = (@uv-center)*chf('uv_mult');
@uv += disp;