Stanislav Yakymenko
Berlin-based digital artist

Houdini VEX Snippets

Handy collection of Houdini VEX fragments

HOUDINI VEX SNIPPETS

 
 

Handy collection of Houdini VEX fragments I have found online and snippets created by myself.

rand_removepoint1

// removes random point with certain probability 
// create spare parameters
float prob = chf('probability');
float seed = chf('seed');
float u = rand(set(@elemnum%65535, @elemnum/65535, seed));
@group_pgroup1 = prob > u;
if(@group_pgroup1)
removepoint(geoself(),@ptnum);

 

rand_pscale1

// randomizes points scale
// create spare parameters
@pscale = fit01(rand(@ptnum + chi('pscale_seed')), chf('max_value'), chf('min_value'));

 

rand_protation1

// randomizes points rotation
// create spare parameters
float minmaxangle = chf('minmaxangle');
float angl = fit01(rand(@ptnum), -minmaxangle*10, minmaxangle);
@rot = quaternion(angl, {0, 1, 0});

 

normal_orient1

@N = set(0, -1, 0);

 

median_pt_num1
Source

int npt = npoints(0);
if(npt%2==0)
    i@midpt = (npt/2) -1;
else
    i@midpt = (int(ceil(float(npt)/2))) -1;;

 

points_box1

// creates box with points using vex
// run over numbers
// create spare parameters
addpoint(0, fit01(rand(@elemnum), chv('min'), chv('max')));

 

bend_curve1
Source

// create spare parameters
// input is a line
// resample SOP on the line for more points
// activate curveu attrib in resample SOP
// use transform SOP with Y translate -$YMAX for 0 yposition
@curveu=chramp("ramp",@curveu);
float bendamt = chf("bend_amount");
vector benddir = chv("bend_direction");
@P+= benddir * bendamt * @curveu;

 

Parabola formula to create wires
Extra info | Source


parabolic_tension1

// minimum 3 points is required
// create spare parameters
float ptn = @ptnum;
float npt = @numpt;
float curvu = ptn/(npt-1);
float paraby = 1-(pow((2*curvu-1),2));
@P.y -= paraby*ch("tension");

 

Replace a prim with a point
Source


prim_to_point_replace1

// with point normals
// run over primitives
int newpoint = addpoint(0, @P);
setpointattrib(0, “N”, newpoint, @N);
@up = set(0, 0, 1); 
removeprim(0, @primnum, 1);


prim_to_point_replace2

// run over primitives
addpoint(0, @P);
removepoint(0, @primnum, 1);

 

object_center1
Source

// run over detail
// object whose centroid is to be computed goes into input 2
vector cent = getbbox_center(1);
addpoint(0, cent);

 

procedural_subdivision_curves1
Source

// connect equal amount of points to inputs 0 and 1
// works best along X-axis
// create spare parameters
// add Resample SOP after
float o = chf("Offset");
float rando = fit01(rand(@ptnum + chi("Seed")), -o, o) * chf("Random_Offset_Amp");
o += rando;
float s = chf("Curve_Softness");
vector npos = point(1, "P", @ptnum);
vector pos = v@P;
vector d = npos - pos;
vector p2 = pos + set(clamp((o - s*o), 0, 1) * d.x, 0, 0);
vector p3 = pos + set(o * d.x, 0, d.z * (1.0 - s));
vector p4 = pos + set(clamp((o + s*o), 0, 1) * d.x, d.y, d.z);
int npt2 = addpoint(0, p2);
int npt3 = addpoint(0, p3);
int npt4 = addpoint(0, p4);
int npt5 = addpoint(0, npos);
int newprim = addprim(0, "polyline", @ptnum, npt2, npt3, npt4);
addvertex(0, newprim, npt5);

 

Connects any found points; supports second input detection
Source


rand_nearby_connect1

// create spare parameters
int read=0;
if(npoints(1)>0){
    read=1;
}
int pts[]=pcfind(read,"P",v@P,chf("maxLineLength"),chi("maxFindCount"));
int l=len(pts);
float fl=float(l);
int randomConnect=chi("randomConnect");
int rander=pts[int(random(@ptnum*fl+randomConnect)*fl*fl) % l];
vector curPos=attrib(read,"point", "P", rander);
int to=addpoint(0,curPos);
addprim(0,"polyline",@ptnum,to);


line_generator1

// create spare parameters
int drawLine(vector f; vector t){
    int base=addpoint(0,f);
    int to=addpoint(0,t);
    int line=addprim(0,"polyline",base,to);
    return line;
}
int read=0;
if(npoints(1)>0){
    read=1;
}
int maxLines=chi("maxLineCount");
float minLen=chf("minLineLength");
int pts[]=pcfind(read,"P",v@P,chf("maxLineLength"),chi("maxFindCount"));
int randomConnect=chi("randomConnect");
int keepPointCount=min(1, max(0,chi("keepPointCount")));
int runner=0;
vector curPos;
int pt;
if(randomConnect == 0){
    for(int x=0; x<len(pts);++x){
        pt=pts[x];
        if(runner > maxLines){ break; }
        curPos=attrib(read,"point", "P", pt);
        if(length(curPos-v@P)>minLen && (@ptnum<pt || read)){
            if(keepPointCount){
                int to=pt;
                if(read){
                    to=addpoint(0,curPos);
                }
                addprim(0,"polyline",@ptnum,to);
            }else{
                drawLine(v@P,curPos);
            }
            runner++;
        }
    }
}else{
    int l=len(pts);
    float fl=float(l);
    int rander=pts[int(random(@ptnum*fl+randomConnect)*fl*fl) % l];
    curPos=attrib(read,"point", "P", rander);
    if(keepPointCount){
        int to=rander;
        if(read){
            to=addpoint(0,curPos);
        }
        addprim(0,"polyline",@ptnum,to);
    }else{
        drawLine(v@P,curPos);
    }
}

 

Circle Pattern
Source


circle_generator1

// create spare parameters
int sample = chi('sample');
float radius = ch('radius');
vector center = chv('center');
// circle formula
float theta = 0;
float step = PI*2 / (float)sample;
float x,z;
vector pos;
while(theta < PI*2)
{
    x = center.x + cos(theta)*radius;
    z = center.z + sin(theta)*radius;
    pos = set(x, center.y, z);
    addpoint(0, pos);
    theta += step;
}


line_generator1

// create spare parameters
int line = addprim(0, 'polyline');
int const_offset = chi('offset');
float phase = ch('phase');
// additional offset
float fit_ptnum = fit(@ptnum, 0, npoints(0), 0, PI*phase);
int sin_offset = (int)(sin(fit_ptnum) * ch('sin_offset'));
// find connection neighbour
int neigh = (@ptnum + const_offset + sin_offset)%npoints(0);
// create the connection line
addvertex(0, line, @ptnum);
addvertex(0, line, neigh);


color_ramp1

int pts[] = primpoints(0, @primnum);
float fit; vector color;
for (int i=0; i < len(pts); i++)
{
	fit = fit(i, 0, len(pts), 0, 1);
    fit = chramp('color', fit);
    color = set(fit, fit, fit);
    setpointattrib(0, "Cd", pts[i], color, "set");
}

 

Attribute name as a string from channel
Source


attrib_name1

// create spare parameters
string name = chs("attributeName");
int newatt = addattrib(0, "point", name, "...");

 

range_filter1

// create spare parameters
i@everyother = set((@ptnum+chi('offset'))%chi('delete_every')==0);
if(@everyother)
removepoint(geoself(),@ptnum);

 

procedural_line1
Source

// create spare parameters
// run over detail
float length = ch('length');
int pointcount = chi('points');
vector dir = chv('direction');
vector startpos = chv('start_point_position');
dir = normalize(dir);
int points[];
resize(points, pointcount);
f@stepvalue = length/(float)(pointcount-1);
for (int i = 0; i < pointcount; i++)
{
    vector pointpos = dir * (@stepvalue*i) + startpos;
    int currentID = addpoint(geoself(), pointpos);
    points[i] = currentID;
}
addprim(geoself(), "polyline", points);

 

recursive_subdivision1
Source

// create spare parameters
// run over primitives
float x = chf("x_amount");
float y = chf("y_amount");
int pnts[] = primpoints(0, @primnum);
vector pos_0, pos_1, pos_2, pos_3;
vector pos_a, pos_b, pos_c, pos_d, pos_e, pos_f;
pos_0 = point(0, "P", pnts[0]);
pos_1 = point(0, "P", pnts[1]);
pos_2 = point(0, "P", pnts[2]);
pos_3 = point(0, "P", pnts[3]);
pos_a = lerp(pos_0, pos_1, y);
pos_b = lerp(pos_1, pos_2, x);
pos_c = lerp(pos_3, pos_2, 1-y);
pos_d = lerp(pos_0, pos_3, x);
pos_e = lerp(pos_d, pos_b, y);
pos_f = lerp(pos_d, pos_b, 1-y);
int pnt_a = addpoint(0, pos_a);
int pnt_b = addpoint(0, pos_b);
int pnt_c = addpoint(0, pos_c);
int pnt_d = addpoint(0, pos_d);
int pnt_e = addpoint(0, pos_e);
int pnt_f = addpoint(0, pos_f);
addprim(0, "poly", pnts[0], pnt_a, pnt_e, pnt_d);
addprim(0, "poly", pnt_a, pnts[1], pnt_b, pnt_e);
addprim(0, "poly", pnt_b, pnts[2], pnt_c, pnt_f);
addprim(0, "poly", pnt_d, pnt_f, pnt_c, pnts[3]);
removeprim(0, @primnum, 0);