Vex Snippets - gigel3d/houdini GitHub Wiki

Groups

Set groups on first and last point

if (@ptnum == 0)
    setpointgroup(0, "frontpt", @ptnum, 1, "set");
if (@ptnum == (@numpt-1))
    setpointgroup(0, "backpt", @ptnum, 1, "set");

Set prim group for all inverted normals

if(v@N != {0,1,0})
{
    setprimgroup(0, "reverse", @primnum, 1, "set");
}

Set edge group based on direction and tolerance

vector n = normalize(chv("direction"));
float tolerance = ch("tolerance");
string group = chs("group_name");

int pts[] = neighbours(0, @ptnum);
foreach (int pt; pts)
{
    if (@ptnum < pt) {
        vector p = point(0, "P", pt);
        vector dir = normalize(@P - p);
            if (1 - abs(dot (dir,n)) <= tolerance) {
                setedgegroup ( 0, group, @ptnum, pt, 1 );
                s@edgeselect = group;
            }
    }
}

Set the group for a highest prim

vector max = getbbox_max(0);

if(@P.y == max.y) { i@group_top = 1; }

Attributes

Check the maximum value of an attribute

string var_attrib = chs("attribute");

int max_num = nuniqueval(0, "point", var_attrib);
i@max_num = max_num - 1;

Remove primitives based on comparison of two attributes

string check_name = chs("check_name");

if ((s@model_name == check_name)-1){ // compare with existing model name attribute
    removeprim(0, @primnum, 1);
}

Set prim attributes based on range

int segment = chi("segments");
string segname = chs("segment_name");
int select = chi("vertical_selection");

if(select == 1)
{
    s@segment = (segname + itoa(@primnum % segment)); // vertical selection
}
else
{
    s@segment = (segname + itoa(@primnum / segment)); // horizontal selection
}

Set random id to points

float seed = chf("seed");
int objNum = chi("number_of_objects");

i@objId = fit01(rand(@ptnum * seed), 0, objNum);

Check if attribute is equal to some value

int div = chi("divisions");

if (div == 4){
    i@isquare = 1;
}
else {
   i@isquare == 0;
}

Add two strings

string a = chs("SM_");
string b = chs("WindowTall");

string c = a + b;
s@c = c;

Count number of unique string attributes

int maxVar = nuniqueval(0, "prim", "name");
i@maxVar = maxVar;

Count and pick random objects based on percentage

float val = rand(@P + chf("seed"));

if(val < 0.3){
    v@Cd = set(1,0,0);
    setdetailattrib(0, "objSelected", 1, "add");
}

Get attribute value

point("../attribwrangle1",0, "sizeX", 0)

Get bound size

vector size = getbbox_size(0);
f@sizeX = size[0];
f@sizeY = size[1];
f@sizeZ = size[2];

Set letter variant id

int var_id = chi("../variant"); //path to 'switch' node where variants are selected
i@var_id = var_id;
string char = chr(65); // changes 0 value to char 'a'
int num = ord(char);

if(i@var_id == 0){
    s@var_prefix = tolower(char);
}
else {
    s@var_prefix = tolower(chr(num + var_id));
}

Set random point variant id

float seed = chf("seed");

int maxNum = detail(-1, "max_num", 0); // max variant number from a spare input

string var_name = chs("attribute"); // attribute name
string prefix = chs("prefix"); / attribute prefix

float set_id = rint(fit01(rand(@ptnum + seed), 0, maxNum));

setpointattrib(0, var_name, @ptnum, prefix + itoa(set_id), "set");

Removes points based on string attribute

string name = tolower(s@name);

if(name ~= "*plank*")
    removepoint(0,i@ptnum, 1);

Find string attribute and add value

if(match("*desk*", @name)){

s@value = "small";
}

Count length of each prim

int pts[] = primpoints(0, @primnum);
@length = length(vector(point(0, "P", pts[0])) - vector(point(0, "P", pts[1])));

Count offset lines in x and y axis

vector bboxSize = getbbox_size(0);
@offsety = bboxSize.y * chf("offset_top") /2;
@offsetx = bboxSize.x * chf("offset_side") /2;

Curves

Bend curve

float ptnum = @ptnum;
float ptmax = @numpt;

@gradient = ptnum/ (ptmax-1);

float scale = chf("scale");
vector dir = chv("dir");
float ramp = chramp("ramp", @gradient);

@P += ramp * dir * scale;

Curve from profile

float profile = chramp("profile", 1);
int pointCount = chi("./profile");

int pnts[];

for(int i=1; i<=pointCount; i++)
{
    float pointPos = ch("./profile" + itoa(i) + "pos");
    float pointVal = ch("./profile" + itoa(i) + "value");
    int newPt = addpoint(0, set(pointPos, pointVal, 0));
    append(pnts, newPt);
}

addprim(0, "polyline", pnts);

Points

Orient each point

@N = normalize(point(1,"P",0)-@P); //require a center point between two points in input1

Offset based on normal direction

@P += @N * chf("dist");

Ray to min value

v@P = minpos(1, v@P);

Removes points from center

float radius = chf("radius");
if(length(@P) < radius){
    removepoint(0, @ptnum);
}

Create points and randomize scale with for loop

// Run over Detail
for(int i=0; i<chi("num"); i++){
    int pt = addpoint(0, set(i,0,0));
    float pscale = rand(i + chf("seed"));
    setpointattrib(0, "pscale", pt, pscale);
}

Random value based on position

float val = rand(@P + chf("seed"));
val = chramp("ramp", val);
float threshold = fit(val, 0.0, 1.0, -1.0, 1.0);

if(@P.y < threshold){
    v@Cd = set(1,0,0);
}

Geometry manipulation

Circle from line

float gradient = (float)@ptnum/(float)(@numpt-1);

@P.y = cos(gradient * (2 * $PI));
@P.z = sin(gradient * (2 * $PI));

Box to sphere

vector centroid = getbbox_center(0);
vector size = getbbox_size(0);
size = min(size);
@P -= centroid;
@P *= (1.0/size);
@P = lerp(@P, normalize(@P), chf('blend'));
@P *= size;
@P += centroid;

Scale first and last point

if ((@ptnum == 0) || (@ptnum == (@numpt-1))) f@pscale = 2; 
else f@pscale = 1;

Scale on selected axis

float gradient = @ptnum/(@numpt-1.0);
float ramp = chramp("ramp",gradient);

vector dir = chv("dir");
v@scale = 1;

if(dir.x > 0){
    [email protected] = fit01(ramp, 0, 1);
}
if (dir.y > 0){
    [email protected] = fit01(ramp, 0,1);
}
if(dir.z > 0){
    [email protected] = fit01(ramp, 0,1);
}

Random scale per piece

int num_pieces = nuniqueval(0,"prim", "pieces");
i@num_pieces = num_pieces;

float seed = chf("seed");

float minScale = chf("min_scale");
float maxScale = chf ("max_scale");

for (int i=0; i<num_pieces; i++){
    float scale = fit01(rand(@primnum + seed * i), minScale, maxScale);
    f@scale = scale;
}

Random scale

float seed = chf("seed");

float minScale = chf("min_scale");
float maxScale = chf ("max_scale");
float scale = fit01(rand(@ptnum + seed * 23), minScale, maxScale);

@pscale = scale;

Rotate with quaterion XYZ

// Add angle in XYZ axis
float angleX = radians(chf('angle_x'));
float angleY = radians(chf('angle_y'));
float angleZ = radians(chf('angle_z'));

// Apply rotation
vector rot = set(angleX,angleY,angleZ);
@P = qrotate(quaternion(rot), @P);

Random rotation with multiply

float seed = chf("seed");

float minAngle = chf("min_angle");
float maxAngle = chf("max_angle");

float rotx = fit01(rand(@ptnum + seed * 15), minAngle,maxAngle);
float roty = fit01(rand(@ptnum + seed * 12), minAngle,maxAngle);
float rotz = fit01(rand(@ptnum + seed * 25), minAngle,maxAngle);

vector multiply = chv("multiply");

f@rot_x = rotx * multiply.x;
f@rot_y = roty * multiply.y;
f@rot_z = rotz * multiply.z;

Random rotation with direction

float seed = chf("seed");

float min_angle = radians(chf("min_angle"));
float max_angle = radians(chf("max_angle"));
float rot = fit01(rand(@ptnum + seed * 15), min_angle,max_angle);
vector dir = chv("direction");

@rot = quaternion(rot, dir);

Random offset

float minoff = chf("min_offset");
float maxoff = chf("max_offset");

float seed = chf("seed");

float offset = fit01(rand(@ptnum + seed), minoff, maxoff);
vector dir = chv("dir");

@P += offset * dir;

Simplex noise

float scale = chf("scale");
float offset = chf("offset");
float amp = chf("amplitude");

vector pos = @P * scale;
vector4 dir = set(pos.x, pos.y, pos.z, offset);

vector noise = xnoise((@P * scale)+ dir) * amp;
noise = fit(noise, 0, amp, -1, 1 );
noise = noise * amp;

@P += @N * noise;

Ripple noise

vector pos = @P * chf("scale");
vector4 v = set(pos.x, pos.y, pos.z, chf("offset"));
float val = noise(v);

val = sin(val * $PI * 2.0 * chi("waveNum"));

@P += @N * val * chf("waveHeight");

Perlin noise

float scale = chf("scale");
float offset = chf("offset");
float amp = chf("amplitude");

vector pos = @P * scale;
vector4 dir = set(pos.x, pos.y, pos.z, offset);

vector noise = noise((@P * scale)+ dir) * amp;
noise = fit(noise, 0, amp, -1, 1 );
noise = noise * amp;

@P += @N * noise;

Scale on local position

float scale = chf("scale");
vector geoPos = point(1, "P", 0); // point where geometry is located

@P -= geoPos;
@P *= scale;
@P += geoPos;

Vector with cross product

// Improves object orientation
vector crossv = cross(set(0.0, 1.0, 0.0), @N);

v@up = crossv;