Houdini VEX Snippets

I have always been borrowing code snippets from others, so I thought it was about time to give some back. I will update this list whenever something new comes to my attention.
This page is work-in-progress so it will be very messy for a while.

//
//
//
// VEX types

float                   f@name
vector2 (2 floats)      u@name
vector (3 floats)       v@name
vector4 (4 floats)      p@name
int                     i@name
matrix2 (2×2 floats)    2@name
matrix3 (3×3 floats)    3@name
matrix (4×4 floats)     4@name
string                  s@name
dict                    d@name

Array
float bounds[] = primintrinsic(0, "bounds", 0);
f[]@bounds = bounds; //adds array indices to attributes
//
//
//
//Build-in Attributes

//Point
int pointNumber     = @ptnum;
int numberOfPoints  = @numpt;

vector pos0 = point(0, "P", @ptnum); //get attribute

setpointattrib(0, "P", @ptnum, set(0,0,0), "set"); // set attribute

//
//Vertex
int vertexNumber        = @vtxnum;
int numberOfVerticies   = @numvtx;

vector N = vertex(0, "P", @ptnum); //get attribute

//
//Primitive
int primitiveNumber     = @primnum;
int numberOfPrimitives  = @numprim;

int foo = prim(0, "someAttrib", @primnum); //get attribute

//
//Detail
setdetailattrib(0, "Attrib", 666, "set");

int numPts = detailintrinsic(1, "pointcount"); //get number of points from input 2
//
//
//
//Get Number of Points pr. primitive (run over =  primitives)

i@numPoints = len(primpoints(0, @primnum));
//
//
//
//Assign random colors points or primitives

float r = random(@ptnum+1);
float g = random(@ptnum+2);
float b = random(@ptnum+3);
@Cd = set(r, g, b);
//
//
//
//Rotate Points in XZ-Plane

vector rotAxis = set(0,1,0); // Set rotation axis to Y (we're working on '2D' here)
vector N = set(1,0,0); //set Normal to x axis.
 
// create a 3x3 orientation matrix using N and rotAxis as the principal axes.
matrix3 m = maketransform(N, rotAxis);

// now rotate this matrix around the N axis over time.
float someAngle = 13;
float angle = radians(someAngle);
rotate(m, angle, rotAxis);

p@orient = quaternion(m);
//
//
//
//Get attribute from another point

vector p = point(0, "P", @ptnum-1); // get pos from prev point
//
//
//
//CREATE QUADS

//create points (shared among prims)
addpoint(0, {-150,0,0});
addpoint(0, {150,0,0});
addpoint(0, {-100,0,100});
addpoint(0, {100,0,100});
addpoint(0, {-150,0,200});
addpoint(0, {150,0,200});
int points[] = primpoints(0, 0);

//create prims without verticies
int verts[] = primvertices(0, 0);
addprim(0, 0, "poly", points, verts);
addprim(0, 1, "poly", points, verts);

//add verticies to prims
addvertex(0, 0, 0); //prim 0
addvertex(0, 0, 1);
addvertex(0, 0, 3);
addvertex(0, 0, 2);
addvertex(0, 1, 2); //prim 1
addvertex(0, 1, 3);
addvertex(0, 1, 5);
addvertex(0, 1, 4);
//
//
//
//Delete Points (Run Over = 'Detail')

int pts = @numpt;
for(int i = pts-1; i >= 0; i--) {
    removepoint(0,i); 
}
//
//
//
//Delete all verticies (Run Over = 'Detail')

int numPrims = @numprim;
int numVerts = 0;
for(int j = 0; j < numPrims; j++) {
    numVerts += len(primvertices(0,j));    
}
for(int i = 0; i < numVerts; i++) {
    removevertex(0, i);
}
//Count verticies for each prim and store in detail with dynamic attribute name
for(int i = 0; i < @numprim; i++) {
    int newatt = adddetailattrib(0, "prim"+itoa(i)+"_verts", len(primvertices(0,i)));
}
//Find the 2 closest points for each point

i@closestPoint = -1;
i@secondClosestPoint = -1;

float dist1 = 1000000;
float dist2 = 1000000;

for(int i = 0; i < @numpt; i++){
    if(i != @ptnum) {      
        float newDist = distance(@P, point(0, "P", i));
        if(newDist < dist1) {
            dist1 = newDist;
            i@secondClosestPoint = i@closestPoint;
            i@closestPoint = i;
        } else {
            if(newDist < dist2) {
                dist2 = newDist;
                i@secondClosestPoint = i;
            }
        }
    }
}

int newPrimID = addprim(0, "polyline");
addvertex(0, newPrimID, i@closestPoint);
addvertex(0, newPrimID, @ptnum);
addvertex(0, newPrimID, i@secondClosestPoint);
//
//
//
//Worley Noise

float dist1,dist2;
float freq = 8;
vector offset = set(0,0,0);
int seed = 666;

wnoise(@P * freq+offset, seed, dist1, dist2);
@Cd = dist1;
//
//
//
//DICTIONARY

int arr[] = {1,2,4};

dict elements;
elements['lort'] = arr;

if(isvalidindex(elements, 'lort') == 1) {
    int arra[] = elements['lort'];
    i@lort = arra[2];
}