fx notes

Search

Search IconIcon to open search

VEX 101

Last updated Jul 4, 2023 Edit Source

# The Better Cheat Sheets / Snippet Libraries / 101s

# Datatypes

// point wrangle

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Integers
int IntVariable = 1;
i@IntAttribute = 1;

// Floats
float FloatVariable = 0.123;
f@FloatAttribute = 0.123; // attributes will be float by default if you don't specify it otherwise

// Vectors
vector VectorVariable = {0,1,0}; // 3 floats > postion, color, v
vector VectorVariable = set(0.1, 0.2, 0.3); // use the set() function to define a vector with floats 
vector2 Vector2Variable = {0,1} // 2 float > e.g. uvs
vector4 Vector4Vairable =  quaternion(angle, axis); // 4 floats > usually used to store quaternions

v@Vector3Attribute = {0,1,0}; 
u@Vector2Attribute = {0,1};
p@Vector4Attribute = quaternion(angle, axis);

// Matricies
matrix2 Matrix2Variable = {1,2,3,4}; // 2x2
matrix3 Matrix3Variable = {1,2,3,4,5,6,7,8,9}; // 3x3
matrix4 Matrix3Variable ={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; // 4x4

2@Matrix2Attribute = {1,2,3,4};
3@Matrix3Attribute = {1,2,3,4,5,6,7,8,9};
4@Matrix4Attribute = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};

// Arrays
string StringArray[] = {'A','B','C'};
string StringArray[] = array(variable_A, variable_B, variable_C); // use the array() function to define an array with variables 
s[]@StringArrayAttribute = {'A','B','C'};

// Strings
string StringVariable = 'Interesting Text';
s@StringAttribute = 'Interesting Text';

// Dictionaries
dict Dictionary;
Dictionary['key'] = 'value'; // Store 3 in the index key
d@DictAttribute = {}; // dict, can only instantiate as empty type
d@DictAttribute['key'] = 'value'; // can set values once instantiated

# Datatypes Overview

// point wrangle

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
int      => i@name
float    => f@name
vector2  => u@name
vector3  => v@name
vector4  => p@name
matrix2  => 2@name
matrix3  => 3@name
matrix   => 4@name
string   => s@name
dict     => d@name

# Comments

// point wrangle

1
2
3
4
5
6
// normal comment

/*
multi-line 
comment
*/

# Conversion

// point wrangle

1
2
3
4
5
6
7
// int to string
int i = 123456;
string s = itoa(i);

// string to int
string s = '123456';
int i = atoi(i);

# String manipulation

// point wrangle

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
// Slicing >>> string[start:end]
string s = 'abcde';
s[:];     // abcde
s[:-1];   // abcd
s[1:];    // bcde
s[1:-1];  // bcd
s[-1];    // e
s[0];     // a

// Concatenation
string text = 'TEXT';
string number = '1234';
string s = sprintf('%s%s%s', text, ' = ', number);
printf('%s', s);
// TEXT = 1234

// Reversing
string s = '1234'
reverse(s);
// 4321

# Arrays

// point wrangle

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Defining
int nums[] = { 0, 1, 2, 3, 4, 5 };
i[]@nums = { 0, 1, 2, 3, 4, 5 };

int a = 5; 
i[]@nums = array(0, 1, 2, 3, 4, a); // just like set() for vectors

// Retrieving 
int num = nums[3] // 2
i@num = i[]@nums[3] // 2

// Accessing, Slicing (VEX uses Python style slicing notation)
int start[] = nums[0:2]; // { 0, 1 }
int end[] = nums[-2:]; // { 4, 5 }
int rev[] = nums[::-1]; // { 5, 4, 3, 2, 1, 0 }
int odd[] = nums[1::2]; // { 1, 3, 5 }

// Sorting, Reversing
int nums[] = {5, 1, 4, 3, 8};
int sorted[] = sort(nums)); // {1, 4, 4, 5, 8}
int reversed[] = reverse(sort(nums))); // {8, 3, 4, 1, 5}

// Adding Elements
int numbers[] = {1, 2, 3, 4, 5, 6};
numbers[1] = 7; // {1, 7, 3, 4, 5, 6}
numbers[6] = 7; // {1, 2, 3, 4, 5, 6, 7}
append(numbers, 7); // {1, 2, 3, 4, 5, 6, 7}

// Finding Index of Value
int numbers[] = {5, 4, 3, 2, 1};
int index_of_4 = find(numbers, 4);
printf('%d', index_of_4 ); // 1

// Split string with a space to array of strings
string numbres = '1 2 3 4 5 6';
string array[] = split(numbres, ' ');
printf('%s', array); // {1, 2, 3, 4, 5, 6}

// Split integer into array of integers via strings
int int_number = 312654;
string string_number = itoa(int_number);
int int_numbers[];

for(int n=0; n<len(string_number); n++){  
        int_numbers[n] = atoi(string_number[n]);
        }

int nums[] = int_numbers; // {3, 1, 2, 6, 5, 4}
int sorted[] = sort(int_numbers); // {1, 2, 3, 4, 5, 6}
int reversesorted[] = reverse(sort(int_numbers))); // {6, 5, 4, 3, 2, 1}

// Copying between arrays and vectors/matrices

float x[];
// Cf and P are vectors
x = set(P);  // Assigns the components of P to the corresponding members of the array x
Cf = set(x); // Assigns the first 3 members of x as the components of the vector Cf
float x[] = {1, 2} // Not long enough to fill a vector
Cf = set(x);  // Cf == {1, 2, 2}

# Channel Syntax

// point wrangle

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
ch('float'); // Float
chf('float2'); // Float
chi('integer'); // Integer
chv('vector'); // Vector 3
chp('quaternion'); // Vector 4 / Quaternion
ch3('matrix3'); // 3x3 Matrix
ch4('matrix4'); // 4x4 Matrix
chs('string'); // String
chramp('ramp', x); // Spline Ramp
vector(chramp('rgbramp', x)); // RGB Ramp

# Debug with printf

// point wrangle

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// print strings
printf('Hello Houdini');

// print datatypes
printf('string: %s', 'Text'); // string: Text
printf('interger: %d', 123); // integer: 123
printf('float: %f', 0.123456); // float: 0.123456
printf('float (rounded): %.2f', 0.123456); // float (rounded): 0.12

// get all primitives
int primitives[] = expandprimgroup(0, "*");

foreach (int currentPrim; primitives){   
        // print primnum
        printf('Prim %s \n', currentPrim);
        }

# Global Attributes

// wrangle

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Available in all SOP wrangles
i@Frame     // current frame number => $F
f@Time      // current time in seconds => $T
f@TimeInc   // timestep currently being used for simulation or playback

// only in DOPs
i@SimFrame  // integer simulation timestep number => $SF
f@SimTime   // simulation time in seconds => $ST

// Available in Attribute Wrangle (point, vertex, primitive and detail)
v@P         // position of the current element

i@ptnum     // point number attached to the currently processed element
i@numpt     // total number of points in the geometry
i@vtxnum    // linear number of the currently processed vertex
i@numvtx    // number of vertices in the primitive of the currently processed element
i@primnum   // primitive number attached to the currently processed element
i@numprim   // total number of primitives in the geometry

// useful: elemnum and numelem morph into whatever wrangle you are in
i@elemnum   // index number of the currently processed element
i@numelem   // total number of elements being processed

// Available in Volume Wrangle
v@P // position of the current voxel
f@density // density value at the current voxel location
i@resx, i@resy, i@resz // resolution
v@center // center of the volume bbox
v@dPdx, v@dPdy, v@dPdz // change in position that occurs in the x, y, and z voxel indices
i@ix, i@iy, i@iz // voxel indices (only dense volumes / non-VDBs ) range from 0 to resolution-1

// Available in Heightfield (Volume) Wrangle
f@height // height at xy coordinate
f@mask // mask at xy coordinate

more specific / context related attributes can be found here.


sources / further reading:


Interactive Graph