This cookbook refers to the old expression language. For information on Python expressions, see the Houdini Object Model section.
Choosing a random number seed
The rand(seed) function always returns the same number when given
seed value. This makes “random” values repeatable between playbacks.
However, you must be careful to supply a seed value that varies.
The current frame number (
$F) is usually a good seed value, however:
If you need random values for a sub-frame process, use the fractional frame number
If you need more than one random value, such as R, G, and B values for a random color, give each call to
randa different seed value by modifying the
$F, otherwise each call will return the same number and the color will be gray.
rand($F * 10)
rand($F * 100)
If you need random values that vary across geometry, use a variable like
$PT(the current point number) as the seed. If the value needs to be random across the geometry and across time, combine
rand($PT / $F)
Random number from X to Y
The rand(seed) function returns a random number from 0 to 1. Use the
fit01(value, min, max) function to map the random value to
a different range. For example, use
fit01(rand($F), -10, -10) for a random
number from -10 to 10.
(This is the same as
rand($F)*20 - 10 but it saves you from having to do the
calculation necessary to work out what to multiply and subtract to get the
Use modulus to cycle expression values
The modulus operator, a % b , returns the remainder after dividing a by b.
This is very useful in expressions, because no matter how big a gets, the result
will keep cycling from 0 to b-1. For example,
$F % 120 is a ramp from 0 to 119
which repeats at frame 120, 240, 360, and so on.
The modulus operator works on any real number, not just integers. Therefore, the
value returned might not be what you expect. For example:
3.1415 % 1 returns
34.999 % 5 returns
Use sin to smoothly cycle between two values
Use an expression like sin($F * 10) in an object’s translate-X parameter to animate it moving smoothly back and forth.
Take the absolute (always positive) value of the expression, abs(sin($F * 10)), in an object’s translate-Y parameter to make it bounce up and down, reversing smoothly at the top and sharply at the bottom.
To make the bounce decay over time, create a spare parameter to hold the decay
rate. Change the bounce expression to multiply by the decay parameter:
(abs(sin($F * 10)) * ch("./decay").
Then, edit the curve of the decay parameter to go from 1 to 0 over time.
Grab the value of another parameter
ch("pathname") function to grab the value of another parameter.
For example, to have a Tube SOP change its height based on the translation of an
object named logo, set the tube’s Height parameter to the expression
ch() references easily, press on the
source parameter and choose Copy Parameter,
then press on the target parameter and choose
Paste Copied References.
You can use the channel value within an arithmetic expression, for example
ch("/obj/$OS/spare1") + ch("/obj/$OS/tx") + 2 takes the value of the channel
$OS/spare1 and adds the X value of that point plus two.
chf() function is the same as ch() function, but you can specify the frame
at which to sample the value of the named parameter. For example,
chf("/obj/logo/ty", 14) grabs the value of the logo’s translate-Y at frame 14.
$F to offset from the current frame, for example
chf("/obj/logo/ty", $F + 14) grabs the value 14 frames in the future.
The chs() function returns the value as a string. This is also useful for checkbox parameters because it returns “on” or “off” instead of float values.
Get the nodes connected to the current node
You can get the path of the node connected to a node’s input with the opinput(operator_path, input_index) function. You can use this to automate path references in nodes. For example, if you want to get the number of incoming points from the node connected to the current node’s first input, use:
npoints( "../" + opinput(".", 0) )
The following expression examples were created by using a Grid op set to 1 row by 180 columns, and Connectivity set to Rows. To this, a Point op was appended. To use these as a segment expression, use the same expression in a channel, and replace the position variable ($PT, $TX or $BBZ) with $T.
Sine and cosine
The sine (and cosine) function are extremely versatile for the creation of all kinds of shapes. The basic sine function can be used to transform a line or surface on the XZ plane into an oscillating wave.
The general form for a geometric sine wave is:
For wave motion, you could use this segment expression:
The basis for the wave: to animate the geometry of the wave, this could be set instead to ($TX + $F).
Controls the number of waves. You could set this to the bounding box position, $BBX, to make the waves more frequent towards the end of the line.
Controls the height of waves. You could set this to the bounding box position, $BBX, to flatten the waves toward the end of the line, or the current frame, $F, to make the waves larger as the animation progresses.
Translates the waves.
To get ripples radiating from a center, base the sine function on the distance of the point to the center of the surface:
To animate this, add a time-based variable to the expression:
You can make Arcs, Circles, Ellipses and Spirals by modifying a point’s position in two axes using a sine and a cosine function:
The angle subtended by this expression equals arcangle * total number of points.
radiusX and radiusY are the radius of the circle. If differing, then an ellipse is generated. If set to a variable such as
$PTthena spiral may be generated. offsetX and offsetY translate the arc.
This expression is useful as a segment expression when you want an object to go in the path of a circle and maintain the same orientation of the object (if you use a rotation channel, the orientation will rotate as well).
Change values of the multiplier (2 and 0.7 in the example) to change the aspect ratio of the arc.
This creates a logarithmic spiral, reminiscent of nautilus shells.
The height of the spiral versus the spiral size can be controlled by the Position in Z.
clamp ensures a value stays in a certain range. For example, clamp($TX, minvalue, maxvalue) clamps the x position to be between minvalue and maxvalue. Step
Stepping triangular wave
You can use a Point SOP referencing a spare parameter to shear, taper, squash, bend, and apply other deformations to geometry.
You can also achieve these effects with the Twist.
Shears geometry in any direction, X, Y, or Z. To shear about the bottom of an object in the X direction, first add a Point sop and set it’s display flag, then add the channel spare1 (use Edit > Add User Defined Channels...). Then in the Point sop, change the Translate X to:
You can animate the amount of shearing with the spare parameter.
Taper is similar to shear; you can taper in any direction. To taper in the X direction, use a Point op, and add a spare channel. Change pos x to any of the following:
You can animate the amount of shearing with the spare parameter. For this expression, the spare parameter must be greater than 0 or an error will occur.
Squash and Stretch
Squash and stretch is a traditional animation technique where if an object scales up in one direction (stretch) there will be a corresponding scale down in the other direction.
A simple way of doing squash and stretch is with a Transform sop and using only the scaling channels: sx, sy, and sz. Use the sy channel to scale the object using the any function ( ease(), bezier(), etc. ). Then in the sx and sz channel segment use:
Now when you animate the Scale Y (sy) channel, the relative volume of the object is preserved.
Bulge and Pinch
To get a squash and stretch where the only the middle of the object is affected (so it bulges or pinches), use a Point op with spare1 channel as your controlling channel. Change pos x and pos z to:
To get a smooth bulge/pinch, precede the Point op with a Refine op, and increase the U and V Divisions over a range of 0 - 1. You can also use a Twist or Lattice op to get bulging effects.
To twist an object around its center, append a Point op and create a spare channel. Change the pos x and pos z to:
For greater control, precede the Point op with a Refine op, and increase the U and V Divisions over a range of 0 - 1. You can also use a Twist or Lattice op to get twist effects.
To bend an object, use a Point sop and create a spare channel. Change the pos x and pos z to:
As with twist, you may want finer control by preceding the Point op with a Refine op, and increase the U and V Divisions over a range of 0 - 1. You can also use a Twist or Lattice op to get bend effects.
Repeat animation curves
Use the repeat function to copy or repeat repetitive motion. The basic form for the repeat function is:
repeat (start_keyframe, end_keyframe)
If you have keyframes at 0, 22, and 36, and you want to repeat the curve from 0 to 36, you select the segment from frames 36- , and enter:
This works within a channel, without referencing another channel.
Your repeat range must be outside the range of the currently selected segment.
For example, if you have keyframes at frames 0, 22, and 36, you cannot select
the segment from 0-22 and assign a
cycle() function with values within the
range of 0-22, because it would be trying to repeat values of itself that have
not yet been determined.
Iso-surface node equations
The Iso Surface SOP creates surfaces from equations.
You can set up the IsoSurface SOP with an expression having three dimensions, each taking a real value. The IsoSurface SOP can then create a surface from every point inside a bounding box where the expression equals 0.
For example, the default expression
$X*$X + $Y*$Y + $Z*$Z - 1
...plots a sphere (the expression is less than 0 inside a unit sphere, greater than 0 outside a unit sphere, and equal to 0 at the surface of a unit sphere).
This may seem like a difficult way to define a sphere, but combined with the rich array of functions available to expressions, it has the potential to create complex and strange surfaces. For example, noise($X, $Y, $Z) plots a random jumble of surfaces.
Here is an expression to define a flaming star:
-10 -10 -10
10 10 10
40 40 40
In this equation, the rectangular (Cartesian) coordinate system has been moved to a spherical (polar) coordinate system. The star diminishes as the distance from the center increases.
Use $F (current frame) as a variable in an equation to make it animate over time.
The if function is perfectly valid in IsoSurface SOP equations, and you can use it to construct geometry piecewise.