git.lucas.co / hou-control
git clone https://git.lucas.co/hou-control.git

vex/include/myLib.h (5.4K)

  1 // include guards: those two lines and #endif at the end of this file will prevent multiple including of this header
  2 #ifndef _myLib_
  3 #define _myLib_
  4 
  5 
  6 
  7 // you can use macros to define constants and use them in your code
  8 #define MY_INT			123
  9 #define MY_FLOAT		3.1415926
 10 
 11 // you can also create alias to the function
 12 #define RENAMEDPOWER	pow
 13 
 14 // or use macros for defining new functions
 15 #define ADDTEN(val)	 	(val+10)
 16 
 17 
 18 
 19 // void functions do not return anything
 20 // "function" keyword is not required
 21 function void myRemPoints(int ptnum) {
 22 	if (ptnum > 30)
 23     	removepoint(0, ptnum);
 24 }
 25 
 26 // function parameters are passed by reference automatically, without additional syntax
 27 // (function receive the original variable, not its copy)
 28 void scaleByTen(vector P) {
 29 	P *= 10;
 30 }
 31 
 32 // you can prevent changing input variable references
 33 void changeA(int a; const int b; int c) {
 34 	a += 10;
 35 	//b += 10; // uncommenting this line will result in error
 36 	c = a;
 37 	c += 4; // even though arguments are passed by reference, they are not true references, "c" is still independent from "a"
 38 }
 39 
 40 // testing escaping of special characters
 41 // here it requires two backslashes :D I am wondering about a legendary place where it needs only one :)
 42 void printTestEscaping() {
 43 	string a = "from myLib.h: \\n \\t v\@P, %04.2f";
 44 	printf(a + "\n");
 45 }
 46 
 47 // a function returning float value
 48 float superRandom(vector4 seeds) {
 49 	float out = rand(seeds.x * seeds.y * seeds.z * seeds.w);
 50 	return out;
 51 }
 52 
 53 // a function returnig an array
 54 int[] range(int max) {
 55 	int out[];
 56 
 57 	for(int i=0; i<max; i++) push(out, i);
 58 
 59 	return out;
 60 }
 61 
 62 // normalize Normal vector by amount [0..1] with specified seed value
 63 vector randomizeN(vector N; float amount, seed) {
 64 	vector randDir;
 65 	
 66 	// getting different random value for each axis, scaling to [-1..1] range
 67 	randDir.x = rand(seed * 684.49848) * 2 - 1;
 68 	randDir.y = rand(seed * 178.46548) * 2 - 1;
 69 	randDir.z = rand(seed * 489.49856) * 2 - 1;
 70 	
 71 	randDir = normalize(randDir);
 72 
 73 	N = lerp(N, randDir, amount);
 74 	N = normalize(N);
 75 
 76 	return N;
 77 }
 78 
 79 // function has different set of parameters, but the same name
 80 vector randomizeN(vector N; float amount; vector4 seed) {
 81 	vector randDir;
 82 	
 83 	// getting different random value for each axis, scaling to [-1..1] range
 84 	randDir.x = rand(seed.x * 684.49848 * seed.w) * 2 - 1;
 85 	randDir.y = rand(seed.y * 178.46548 * seed.w) * 2 - 1;
 86 	randDir.z = rand(seed.z * 489.49856 * seed.w) * 2 - 1;
 87 	
 88 	randDir = normalize(randDir);
 89 
 90 	N = lerp(N, randDir, amount);
 91 	N = normalize(N);
 92 
 93 	return N;
 94 }
 95 
 96 // this function declaration returns different type
 97 // the function name does not really match its functionality, it is just for the example
 98 float randomizeN(vector N; float amount; int seed) {
 99 	float randDir;
100 	
101 	// getting different random value for each axis, scaling to [-1..1] range
102 	randDir = rand((float)seed * 684.49848) * 2 - 1;
103 
104 	return randDir;
105 }
106 
107 // vex also supoorts structs and methods associated with them
108 struct myCustomMatrix {
109 	// uninitialized variables
110 	vector x, y, z;
111 	
112 	// variables with default values
113 	vector translate = {0,0,0};
114 	string comment = 'default comment';
115 	float myPi = 3.14159265;
116 	float uniformScale = 1.0;
117 	float myArray[] = {1,2,3};
118 }
119 
120 // struct for carrying information about our project file
121 struct hipFile {
122 	string base, ext;
123 	int version = 1;
124 
125 	// you can create methods that operate on structs
126 	// this method increases version by 1 and returns new version number
127 	int incVersion() {
128 		this.version++;
129 		return this.version;
130 	}
131 
132 	// inside of a struct function, you can refer to struct fields by name as if they 
133 	// were variables (for example, base is a shortcut for this.base).
134 	// this method writes to console window / terminal
135 	void printName() {
136 		printf("this file has name: %s_%03d.%s\n", base, version, ext);
137 	}
138 
139 	// returns a string with full file name
140 	string getFullName() {
141 		return sprintf("%s_%03d.%s", this.base, this.version, this.ext);
142 	}
143 }
144 
145 // we can create functions that operate on our structs and use their methods
146 int compareHipFiles(hipFile A, B) {
147 	int match = 0;
148 	if (A->getFullName() == B->getFullName()) match = 1;
149 
150 	return match;
151 }
152 
153 // func returning hipFile type
154 // this function expects comma separated list of filenames and will 
155 // return the first occurance of a hip file
156 hipFile findFirstHipFile(string text) {
157 	string inFiles[] = split(text, ",");
158 	string hipParts[];
159 
160 	foreach(string file; inFiles) {
161 		string parts[] = split(file, ".");
162 		if (parts[-1] == "hip" || parts[-1] == "hipnc") {
163 			hipParts = parts;
164 			break;
165 		}
166 	}
167 
168 	// we can also return error state, warning() function is also available
169 	if (len(hipParts) == 0) error("No houdini project found in this file list: %+s.", text);
170 
171 	string prefix[] = split(hipParts[0], "_");
172 	int ver = atoi( prefix[-1] );
173 	string base = join( prefix[:-1], "_");
174 	string ext = hipParts[1];
175 
176 	hipFile out = hipFile(base, ext, ver);
177 	return out;
178 }
179 
180 // we can as well return an array of structs
181 hipFile[] findAllHipFiles(string text) {
182 	string inFiles[] = split(text, ",");
183 	hipFile hips[];
184 
185 	foreach(string file; inFiles) {
186 		string parts[] = split(file, ".");
187 		if (parts[-1] == "hip" || parts[-1] == "hipnc") {
188 			string prefix[] = split(parts[0], "_");
189 			int ver = atoi( prefix[-1] );
190 			string base = join( prefix[:-1], "_");
191 			string ext = parts[1];
192 
193 			hipFile out = hipFile(base, ext, ver);
194 			push(hips, out);
195 		}
196 	}
197 
198 	// output a warning when no Houdini projects were found
199 	if (len(hips) == 0) warning("No Houdini projects found.");
200 
201 	return hips;
202 }
203 
204 
205 #endif // end of include guards