In this tutorial, we will cover how to assign random colors to different XGen primitives using a combination of expressions and the User Data node. 

Before you start this tutorial, it is advised that you also read the tutorial Controlling Primitives with a Texture Map which shows you how to define the scale of your XGen primitives.

Scene Setup

Ai UserData Color node connected to Color attribute of Standard Surface shader


Add the name 'color' to the Color Attr Name in the Ai UserData Color node



Pill archives are using the black Default Color of the Ai UserData Color node



Custom Shader Parameters

Custom Shader Parameters (bottom)


Custom Shader Parameters set to Color



$a=[rand( 0, 1 ,$id),rand( 0, 1 ,$id+1),rand( 0, 1 ,$id+2)];#-1.0,1.0

...where rand defines the RGB values between 0 and 1.

Expression in XGen Expression editor used to define RGB values from 0 to 1

$a=[1,rand( 0, 0.5 ,$id+1),rand( 0, 0.5 ,$id+2)];#-1.0,1.0

...we get something that has RGB values, but it has more of a red color and less green and blue.


$a=[rand( 0.5, 1 ,$id),rand( 0, 0.8 ,$id+1),rand( 0, 0.8 ,$id+2)];#-1.0,1.0

...the pills have more variety of colors because the red color value is between 0.5 and 1.



$minRed = 0.000;
$maxBlueGreen = 1.000;
$a=[rand( $minRed, 1 ,$id),rand( 0, $maxBlueGreen ,$id+1),rand( 0, $maxBlueGreen ,$id+2)];#-1.0,1.0


You should see two sliders appear. They can be used to interactively preview the colors generated. This saves us from having to test render the scene each time we make a change!

XGen Expression Editor with funky new sliders


That is the end of this tutorial. Have fun playing with XGen's expressions!




Thanks to Pedro Fernando Gómez for his assistance with XGen.