Code Snippets: VEX

Updated: Feb 9

attributes vs variables

f@name // attribute, assigned to geo
float name // variable, within wrangle only

declaring data types

// float
f@name  or  float name
// integer
i@name  or  int name
// string
s@name  or  string name
// vector
v@name  or  vector name
// list/array
string name[]  or  float name[]  etc.

some built-in attributes

@ptnum // the point id
@numpt // total number of points
@Time // current time, in seconds
@Frame // current frame
@primnum // the primitive id
@numprim // the total number of primitives

some built-in attributes with sub-components

@Cd (@Cd.r, @Cd.g, @Cg.b) // color
@P (@P.x, @P.y, @P.z) // position
@N (@N.x, @N.y, @N.z) // normal

sub-components can use rgb, xyz, or [0][1][2]


working with strings

// convert from float to string
string name = sprintf('%f', float)

// combine strings
string name = concat("string", "string")
// result: stringstring

// split
string list[] = split("string_01", "_") 
// result: ['string', '01']


channel references on wrangles

ch('channel_name') // float channel
chi('channel_name') // integer channel
chv ('channel_name') // vector channel
chs ('channel_name') // string channel
chramp ('channel_name', attr) // ramp channel (in 0-1 range)

click the plug button to the right to automatically create non-existing channels


point attribute reference

point("/path/to/node", point_num, "attribute", attribute_index)

// for example:
point("../transform", 1, "P", 0) // reads @P.x for point 1

primitive attribute reference

prim("/path/to/node", prim_num, "attribute", attribute_index)

some functions

fit(attr, oldmin, oldmax, newmin, newmax) // take a number between 2 values and fit it between 2 other values (like 0-1)

rand() // generate a random number between 0 and 1

sin(), cos() // sine and cosine, in radians
radians(degrees) // convert a number from degrees to radians

length(vector) // measures the length of a vector
distance(pt1, pt2) // measures the distance between two points

len(list) // counts the number of items in a list

normalize point number


invert (must be in 0-1 range)

f@result = 1 - @attribute;

measure the distance between a point and the origin

float dist = length(@P);

general syntax

// each line must end with ;

// if statements
if (test == value) {
    code to execute;
    code to execute;
else {
    code to execute;