Create an Army of Toy Soldiers


In this tutorial, you are going to regress to a fun part of your childhood when you may have enjoyed playing with toy soldiers. Unlike your childhood, you will be using XGen to arrange the soldiers into interesting formations using expressions and texture maps as masks. We will also cover ways to add color to the individual soldiers using expressions and texture maps. For some real world inspiration, you should also look at some of the work by this artist and also this artist. Both artists use toy soldiers, among other things, to create interesting works of art.

The tutorial is broken up into the following chapters:


Creating the Archive Files

Creating the XGen Description

Generator Attributes

Assigning Random Colors to the Soldiers using an Expression

Using an Expression as a Mask

Using a Texture Map as a Mask

Workflow issue - Primitive Bound


This video demonstrates IPR rendering of an XGen scene containing soldier primitive archives.

The toy soldier models are available to download here.

Remember to always share your toy soldiers with your friends and not hog them to yourself.

Creating the Archive Files

  • Start off by opening the toy soldiers file in Maya. We need to convert the individual soldier models to XGen archives first, so that we can then play with them in XGen.


  • Select each soldier individually and go to Generate> Export Selection as Archive(s)... Choose an archive name and destination folder and select Export.


Creating the XGen Description

  • Create a polygon plane and scale it to the size that you want the soldiers to fill. Increase the number of polygon plane subdivisions in Height and Width to at least 5.

The number of XGen archive primitives is related to the number of subdivisions in the polygon plane. If you notice that your soldiers are not filling the entire size of the plane, then you will need to add more subdivisions to the polygon plane.

Polygon plane with only 1 subdivision. Soldiers do not fill the plane

Polygon plane with 5 subdivisions. Soldiers fill the size of the plane


  • With the polygon plane selected, go to Generate> Create XGen Description. Choose Custom Geometry/Archives (use for any model you have created).


    • It is good practice to lower the Percent value in the Preview/Output tab before continuing. If the Percent value is too high, you may find that your computer will stall due to a large number of primitives generated.


  • Under Archive Files, select Add and go to the location where you saved the soldier archives previously.
  • It is possible to change the color of the icon for each soldier and even add an image icon that represents each archive. To do this, right click on the red square for one of the archive files and choose Edit.
  • Click on the Thumbnail folder icon and choose a bitmap that represents each soldier. In the examples below, you can see that we have screen grabbed the Maya viewport for each soldier and saved those images to files. We can also change the color of the icon and increase its size by adjusting the slider above:


  • Increase the Density to around 500. This will increase the number of soldier primitives on the polygon plane.

Positioning the Soldiers

Generator Attributes

When Generate Primitives is set to In uniform rows and columns the Spacing attribute replaces DensityIncreasing the Spacing attribute increases the distance between the primitives.


Assigning Random Colors to the Soldiers using an Expression

More information on how to do this can be found in the Assigning Random Colors to Primitives tutorial.

  • Create an Ai UserData Color node and connect it to the Color attribute of the Standard Surface shader that is assigned to the soldiers XGen description.

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


  • Type color in the Color Attr Name of the Ai UserData color node. We will use the same 'Color' name in the XGen description.

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


  • MtoA will not gamma correct the Ai UserData Color node. Therefore you will need to add a Maya Gamma Correct node in-between the Ai UserData Color node and the Color attribute of the Standard Surface shader as shown below. Change the Gamma RGB values to 0.454.


  • Render the scene. The soldiers are black because the Ai UserData Color Default Value is black. We need to connect it to the XGen description using the Custom Shader Parameters in XGen.
  • Click on the Preview/Output tab in XGen and open up Output SettingsUnderneath you should see the Custom Shader Parameters. This is where we will add our expression.

Custom Shader Parameters (bottom)


  • In the Name text field, type the same name that was used for the Color Attr Name with the Ai UserData Color node. In this case, we used the word 'color'. Click on float and change it to Color because this is the attribute that we want to change.

Custom Shader Parameters set to Color


  • Click on the XGen Expression Editor icon  to the right of our new 'color color' parameter and add the following text in the Expression Editor:

$factor = 0.1920;

$a=[rand( 0.3, 0.65 ,$id)*$factor,rand( 0.4, 0.45 ,$id+1)*$factor,rand( 0.25, 0.55 ,$id+2)*$factor];#-1.0,1.0


XGen Expression Editor for the custom color


  • Render the scene. You should now see that the colors of the soldiers are randomly created based on the rand values that we used above.


Working with Masks

Using an Expression as a Mask

We can also use an expression to define how many soldiers appear on each polygon face. 

  • Click on the Expression icon  to the right of the mask slider control and type the following text in the XGen Expression Editor:
$border = 0.1150;
$u > $border && $u < (1-$border) && $v > $border && $v < (1-$border)

With each face, you can get the UV coordinates with $u and $v. With this expression, you only populate places where U is greater than 0.2, and less than 0.8 and V is greater than 0.2 and less than 0.8. This way we are getting a square in the middle of each face of the polygon plane.


Using a Texture Map as a Mask

We can also use a black and white grid texture to map out a formation for our army. 

  • Click on the downward pointing arrow  to the right of Mask. Select Create Map... (it is important to note that this will only work if the plane has a Maya shader assigned to it).


  • The Map Name should say 'mask'. Increase the Map Resolution to around 200. This sets the resolution of the PTEX map in texels per-face. When using high-resolution textures, use larger Map Resolution values. After you have done that, click on Create.  


The example below shows another example when using a color texture to drive the density mask. 


  • If we invert the map used for the mask, we can get transparency in the white areas used for the color map.

The map used for the mask is inverted, and therefore soldiers do not appear in the black areas.



Workflow issue - Primitive Bound  

In the render below, you can see some 'clipping' in the top right corner. This is because the bounding box area is set too low and the soldiers are not being rendered outside of this area. Increasing the Primitive Bound resolves this problem. This can be found in Output Settings under the Preview/Output tab.

Primitive Bound has not been set to a large enough value for the XGen archive primitives



That concludes this tutorial on how to create masks and colorize primitive archive toy soldier models in XGen. I hope you had fun with this tutorial. However, its now time to put your toy soldiers away and clean up your bedroom!





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

  • No labels