This section provides simple examples of commonly used XGen expressions. They are intended to further your understanding of how expressions are constructed. You can also use these expressions as building blocks for more complex expressions. To use these examples in a Description, click this icon
beside the attribute you want to control, and then add the expression in the
XGen Expression Editor.
If you are new to using expression, see
XGen expression basics.
Map expressions
Map expressions set paths to Ptex map files. When you use the
Paint Tool in XGen, the map expression is created automatically. If you want to use a Ptex map created outside of Maya, you need to use a map expression to apply it to a Description.
Painting a Ptex map for the Length creates this map expression:
$a =map('${DESC}/paintmaps/length/'); #3dpaint, 200
$a
Expression explanation
-
$a—Default local variable modified by the expression.
-
map()—Function that describes the file path to the location of the Ptex map. The file path includes:
-
${DESC}—Relative file path based on the project location of the Description (see
Ptex map file locations in XGen).
-
paintmaps/length—Continuation of the file path to the Ptex file.
-
#3dpaint, 200—Parameter that defines the texel resolution of the map. If you change this value, the map must be resaved.
-
$a—Calls the expression to apply the Ptex map.
See
Map expression examples for more complex map expressions. See also
Work with Ptex maps and expressions.
Randomize expression
This randomize expression generates random output values based on minimum and maximum input values. You can use it to vary attribute values such as
Length and
Width. It can be used as a global expression to affect a number of attributes (see
Create global expressions).
$min= 0.4000; #0.10,1.00
$max=3.000; #1.0,5.0
rand($min,$max)
Expression explanation
-
$min and
$max—Declared local variables for the minimum and maximum value inputs for the randomization function. Slider controls are created for the variables. A maximum value ($max), is initially set to 3.000 with a slider control range of 1.0 to 5.0. A minimum value ($min) is initially set to 0.400 with a slider control range of 0.10 to 1.00. Use the slider controls to change the input values.
-
rand ($min, $max)—Randomization function evaluates using the local variables to generate random values within the ranges set by the
$min and
$max local variables.
Noise expression
Noise expressions include a noise function with vector or float values as inputs variables. Use noise expressions to generate variation in attributes values. For example, a noise expression can add subtle variations to the effects of a
Clumping modifier or hair length.
In this simple expression, noise is calculated using a single Perlin noise function. You can add complexity to the expression by including other mathematical functions, such as contrast() or
smoothstep() to generate a layered noise effect. Multiple layers of noise can break up the repetitive nature of a single noise function. See
Noise expression examples for a more complex noise expressions.
$hi =50.0; #0.00,100.00
$lo =-10.00; #-100.00,0.00
noise($lo, $hi)
Expression explanation
-
$hi =50; #0.00, 100.0 and
$lo =-10.00; #-100.00,0.00 —Declared local variables for the input values the noise function uses to evaluate. A high value ($hi), is initially set to 50 with a slider control range of 0 to 100. A low value ($lo) is initially set to -10 with a slider control range of -100 to 0. Use the slider controls to change the output of the noise function.
- The
noise () function outputs two-dimensional noise patterns based on the
$hi and $lo local variables. You can add another local variable to the expression and include it as a third input value to the function to create three-dimensional noise.
Conditional expression
Use this type of expression to set attribute values or apply modifiers based on whether or not a condition is met. For example, when used with a Clumping modifier, you can set the amount of clumping applied to each hair based on its length.
This expression can be used to apply an attribute or modifier value, such a
Magnitude, based on final primitive length.
$a =$cLength;
$max =7.00; #0.00,10.0
clamp($a,0,$max)
Expression explanation
-
$a =$cLength;—Global variable that represents final computed primitive length. For example, if you are generating random values for hair length (see the
Randomize expression example above), these values would be represented by
$a in this expression.
-
$max—Declared local variable for the maximum value input. In this expression, it sets the maximum hair length. A slider control is created for this value.
-
clamp($a,0,$max)—The
clamp() function, when used in the length attribute, takes the product of the final computed length of each primitive ($a) and sets the length between 0 and the set
$max value. This expression can also be used in a modifier to affect longer hairs more than shorter ones, as shorter hairs will have a lower
$cLength value.
Expand function input value ranges using an expression
Use an expression to add granularity to input values of functions that have limited range. For example,
smoothstep() takes an input range of 0 to 1. This example expression expands the range of this function by using two local variables and the
fit()
function. You can easily modify this expression to expand the output range of other functions.
$Scalemin=0.3;
$Scalemax=4.0;
smoothstep(x,$Scalemin,$Scalemax)->fit(0,1,-100,100)
Expression explanation
-
$Scalemin and
$Scalemax —Declared local variables for setting the value range used by the
smoothstep () function.
-
smoothstep(x,$Scalemin,$Scalemax)—smoothstep() is used as an example of a function with a limited input range. It outputs quadratic-based values based on an input range of 0 to 1. In this example,
x represents the input value you want to use, such as a declared local variable or global variable. You need replace
x with a proper variable before using this expression.$Scalemin and
$Scalemax set the range for the function's output.
-
->
—Operator that applies functions to the right of the arrow to functions on the left. In this case,
fit() is applied to the output of
smoothstep().
-
fit(0,1, -100, 100) —Function that expands the input value range of 0 to 1 by making it -100 to 100 respectively.