On this page |
You can create custom functions you can use in HScript expressions. Before you write your own function, double check the built-in library of expression functions. The function you need may already exist.
Note
If you are writing such advanced expressions that you need custom functions, you should consider switching to Python for scripting.
How to
To... | Do this |
---|---|
Open the custom function editor |
|
Create a new custom function |
|
Load definitions from an external file |
Click the plus icon in the bottom right corner of the custom function editor and choose the file to load. |
Edit the function source code in an external editor |
|
Format
Custom function definitions have the following syntax:
[return_type] functionName([[arg_type1] arg_name1 [, [arg_type2] arg_name2] ...]) { ... }
The return type and argument types can be float
, string
, vector
, or matrix
.
Note
If you don’t explicitly list a return type or an argument’s type, Houdini will assume it’s a float, and will silently cast any other data type to a float. If you forget to specify that an argument is a string, Houdini will cast it to float, potentially causing hard to find bugs.
The body of the function definition can use extra syntax such as assignment (=
, +=
, -=
), if
, for
, while
, etc. Use return
to return a value.
Lines beginning with #
are comments and are ignored by Houdini.
Examples
# Function to find the minimum value of two # floating point numbers min(v1, v2) { if (v1 < v2) { return v1; } else { return v2; } } # Function to reverse the order of a string string strreverse(string in) { float len = strlen(in); string result = ""; for (src = len-1; src >= 0; src--) { result += in[src]; return result; } } # Example to find the minimum element in a vector float vecmin(vector vec) { min = vec[0]; for (i = 1; i < vsize(vec); i++) { if (vec[i] < min) min = vec[i]; } return min; } # Example to transform a vector into the space # of an object passed in. vector opxform(string oname, vector v) { matrix xform = 1; if (index(oname, "/obj/")) { xform = optransform(oname); } else { xform = optransform("/obj/"+oname); } return v * xform; } # Example to find all objects which have their # display flag set string opdisplay() { string objects = run("opls /obj"); string result = ""; nargs = argc(objects); for (i = 0; i < nargs; i++) { string obj = arg(objects, i); if ( index(run("opset " + obj), " -d on") >= 0 ) result += " " + obj; } return result; }