Jérôme Belleman
Home  •  Tools  •  Posts  •  Talks  •  Travels  •  Graphics  •  About Me

Randomisation in Blender

1 Apr 2019

An overview of the random ways Blender makes it possible to randomly randomise random things, randomly ranging from colours, to materials, to transforms.

1 Randomising Colours

A very common trick which I first learned from Andrew Price (as with everything else) is to use the Random output from the Object Info node, which generates a random number unique to the object. Connect it to the Factor input of a ColorRamp node, as the Factor socket is used in this node as an index for its gradient. Its resulting Image output will therefore be a random colour picked from the gradient.

Canonical Node Setup for Randomising Colours
Canonical Node Setup for Randomising Colours

You may not want to have this setup choose from the near-infinity of colours the gradient range offers, as in very red, or slightly less red and a bit more green, or somewhere between green and blue, etc. as depicted above. If you want to have just red, or just green, or just blue, etc. open the ColorRamp's Interpolation menu whose value defaults to Linear, and choose Constant instead.

2 Randomising Materials

Randomising colours is a good start, but what about randomising textures? Or even whole materials? Various solutions are offered on the web, many of which suggesting to use the Object Info's Random output together with the GreaterThan and LessThan operations of the Math node. These approaches lead to complex and unmanageable node setups. And easier way might be to leave it to Blender's superb Python interface. Open the Scripting workspace, create a new text data-block and throw the following code in it:


import random
import bpy

for obj in bpy.context.selected_objects:
    obj.active_material = random.choice(bpy.data.materials)

The API is so convenient and Python so readable that this listing pretty much reads like English: for each selected object in the file, set its material to a random choice from all the materials available in the file. This is what the snippet will do every time you hit the Run Script button. So in essence, the randomisation isn't generated at object creation, but rather at script execution.

3 Randomising Transforms

Blender offers several ways to randomise object transforms such as location, rotation and scale. Obvious ones are particle systems – think sprinkles in Andrew Price's doughnut tutorial – and physics – which you could have used to have a large number of object acting as sprinkles be subject to gravity and fall on top of the doughnut (much like in real life). These are two obvious ways which I'm not going to discuss here, there's plenty of other good tutorials explaining how that's done.

I've recently used another way to randomly scatter objects, also haphazardly affecting their scales and rotation in the process: select the objects, and open the Object menu → Transform → Randomize Transform. Use the options pop-up to interactively and randomly affect all the locations, rotations and scales of the selected objects. Naturally, nothing keeps you later on from adjusting each object's transforms manually and more controllably to give a last touch to the random effect.

4 References