Cinema 4D lets you type fixed number into almost any numeric field but it also allows you to type in math formulas. When several objects or tags are selected at once, that formula can also give each one a different result. This article shows how to get a genuinely different random value per object, and why the obvious approach gives everything the same number.
Why rnd() on its own gives every object the same value
Select several objects, then type an expression like this into a numeric field in the Attribute Manager:
x+rnd(100)
The x stands for the value the field already had, and rnd(100) asks for a random number between 0 and 100. Without a seed, Cinema 4D generates that random number once and applies the same result to every selected object, so the objects all shift by an identical amount:
How to get a different random value for each object
Add a second argument to rnd(). That second argument is the seed, and the seed is what makes the result vary:
x+rnd(value; num)
-
valueis the upper limit of the random range. Replace it with the largest amount you want to add. -
numis the index of each selected object. Cinema 4D numbers the selected objects in Object Manager order starting at 0, so every object feeds a different seed intornd()and gets its own value.
For example:
x+rnd(100; num)
Entered in the Position X field, this moves each selected object by a different random amount between 0 and 100:
rnd(100; num) and rnd([100][num]) do the same thing.Where you can use this
Anywhere the Attribute Manager accepts a number, and for tags as well as objects. Common examples:
- Position, Rotation, and Scale
- Light Intensity
- Texture Offset and Texture Length on a Texture tag
- Object parameters such as Radius, Height, or Fillet Radius
Select the objects or tags, click into the field, type the expression, and press Enter. The parameter has to be shared, which means every selected item needs to have that same field for the formula to apply.
Other variables you can use with a multiple selection
Random values are only one option. Cinema 4D provides three variables when more than one item is selected:
| Variable | What it means | Example |
|---|---|---|
x |
The value the field already holds for that object |
x+200 adds 200 to whatever each object had |
num |
The index of each selected object, counting from 0 in Object Manager order |
num*20 on 10 lights gives intensities of 0, 20, 40, and so on |
tot |
The total number of selected objects |
100*(num/(tot-1)) spreads values evenly from 0 to 100 across the selection |
num is a fixed index rather than a moving target, entering the same expression on the same selection produces the same set of values every time. That is handy when you want a predictable spread, and it also means you need to change the range or the selection order if you want a different result.Syntax rules worth knowing
- Use a dot as the decimal separator. Commas and thousands separators are not supported.
- Every opening bracket needs a matching closing bracket.
- Spaces are ignored, so you can add them for readability.
- You can mix units in the same expression. For example,
1m + 10inresolves to 1.254 m regardless of the base unit set in your preferences.
Pro tip: using formulas on color values
A color parameter is a single swatch by default, so there is no number field to type into. To expose the numbers, right-click the Color parameter and choose Show Sub-channels from the context menu. That splits the swatch into its individual channel fields, and the same formulas and variables work there.
For example, select several materials, right-click Color, choose Show Sub-channels, and enter rnd(1; num) in one of the channel fields to give each material a different value in that channel.
TL;DR
-
The problem:
x+rnd(100)on a multiple selection generates one random number and applies it to every selected object. -
The fix: add a seed.
x+rnd(100; num)gives each selected object its own random value between 0 and 100. -
The variables:
xis the current value,numis the object index starting at 0, andtotis the number of selected objects. - Where it works: any numeric field in the Attribute Manager, for tags as well as objects, including Position, Rotation, Scale, Light Intensity, and Texture Offset.
- Separator: use a semicolon between arguments, not a comma. Square brackets work too.
If you have any questions or need further assistance, please submit a support ticket, and our team will be happy to help.
Comments
0 comments
Article is closed for comments.