25

Is it possible to generate a random number for use in the node editor?

I am aware of the Object Info node, but I want a random number which I have more control over.
Ideally I could change it with some sort of "seed" vale and be able to define limits for maximum and minimum values.

How is this possible?

gandalf3
  • 157,169
  • 58
  • 601
  • 1,133
  • 1
    Here is a good tutorial wich can help you http://cgcookie.com/blender/2013/10/03/tip-randomizing-leaf-color/ – lucblender Dec 09 '13 at 09:04
  • 1
    @lucblender I know about the object info node, I want a random number which I can change (e.g. with a seed). – gandalf3 Dec 09 '13 at 09:07

5 Answers5

22

That would be through drivers. You can add an Input->Value node to feed into other nodes or add a driver to an editable value on any other node.

A simple way to get random values is edit the value and enter #noise.random() This will give random values between 0 and 1, you can expand the entry to multiply the value if you want it larger or add a minimum value.

To get more control over the generated value you can create a script and use bpy.app.driver_namespace["name"] = funcname to provide access to any function you define. The script name must end in .py so that you can register it and auto run python scripts also needs to be enabled.

sambler
  • 55,387
  • 3
  • 59
  • 192
  • 1
    I tried using noise.random() as a scripted expression, however the debug value stayed as 0 and it did not appear to work in a render. – gandalf3 Mar 27 '14 at 07:02
  • You may need to update dependencies, but one thing I have seen stop this working is if the interpolation type of the driver is set to constant. The other is not enabling auto-run python scripts, you should see a notice if that is the case. – sambler Mar 27 '14 at 23:08
  • I got it working :) I had to save the file first. Something to do with auto run perhaps? I have it enabled but with some excluded directories. – gandalf3 Mar 27 '14 at 23:25
  • Does this expression evaluate on each frame or on each sample? – Matt Nov 21 '20 at 03:11
  • @Matt If you use random for a colour input you get the same colour over the object, per sample would give you variation. – sambler Nov 21 '20 at 06:23
  • Yes, there are some cases where you want per-object consistency. I'm actually looking for per-sample variation (it would be a great way to feather the edges of some procedural inputs, like "pointiness"), but I suspect that making per-sample calls requires an OSL shader. – Matt Nov 22 '20 at 14:55
  • In blender 2.93.5 noise.random() is considered an invalid python statement – Acebone Oct 22 '21 at 11:14
  • 1
    @Acebone It is not. You have to enable Automatic Execution of Python Scripts in User Preferences > Save&Load > Auto Run Python Scripts. Then just update the driver. Expressions can call functions. This is why this works and it is limited by default, otherwise it could be unsafe to auto-execute code from unknown sources. – Chaos Feb 01 '22 at 16:13
12

There is now a White Noise Texture node, which takes input Coordinates and outputs a totally different value (either RGB or between 0 and 1) for even the slightest variation in input.

The input can be set to use 1D, 2D, 3D or 4D. If using 4D, the 4th input, W, can be used as an additional seed to the node.

enter image description here

Gorgious
  • 30,723
  • 2
  • 44
  • 101
12

If you are texturing, there is a "random" value in the object info nodeenter image description here

Not entirely sure what your application is, but there's more info on the object info node at http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Nodes/More

Also found this post here on stack exchange: Random maps in cycles

Sam Schad
  • 137
  • 2
10

I took a little different approach. I added a "value" node. Then key framed it to 0.5 at frame 1 and also to 0.5 at the last frame. Then using the F-curve modifier, I added noise to the F-curve. This has worked well for me to use with flickering lights and such.

Dell Rosa
  • 101
  • 1
  • 2
2

Another approach, if you don't need it to be truly random, is to take the same random output from the object info node, pass it through math.multiply and multiply it by some quite large value (100?), then through a math.modulo with modulo 1.

screenshot of nodes, with the same random output of an object info node feeding to two separate inputs, one via a multiply node and a modulo node

I think for almost all purposes this is going to seem like a brand new random number. At least, when you wouldn't notice a minute difference of the main random number. We're essentially chopping up the digits of the random number we were given into chunks, and using each chunk as a different random number.

So say we got 0.9172352114 as our random number, we can use that is is for one purpose, then multiply and modulo it to get 0.72352114 as our second. We could do it again and get 0.352114 is our third one.

tremby
  • 121
  • 3