Random Color Material

Hello,

I’ve been trying to get a material that would create a random color for every object that would be duplicated.

Until now, I couldn’t find a way. So to overcome that I currently have this script, which changes randomly the Diffuse shader color, for every material with a name starting with “rnd”, which means I have a different material for every object I want a random color.

import bpy, random
a=bpy.data.materials
for mat in a:
[INDENT=2]if mat.name[0:3]==‘rnd’:[/INDENT]
[INDENT=3]node=mat.node_tree.nodes[‘Diffuse BSDF’][/INDENT]
[INDENT=3]RGB=(random.random(),random.random(),random.random(),1)[/INDENT]
[INDENT=3]node.inputs[‘Color’].default_value=RGB[/INDENT]

In order to create a single material that could do that, I’ve tried to use the new ‘Object Info’, Random value, as a different seed for the random numbers to be created in the script, but when I run the script it uses than the same seed for every object (probably the first or the last object random value), so the RGB values are the same for every object.
Any idea of how to overcome that?

Hi Alain, thank you for the link.
I’ve checked it but it does not solve the problem.
In order to define a completely random color, I believe we should have 3 random values (either to define it as R,G,B or Hue, Saturation, Value, or other similar way). The problem is that for each object we can get a single random value. If from that value we could create 2 additional random values it could be solved, but from the link you sent, I see that it was only implemented 3 different functions from the same original random value, so the 3 values are not independent, I don’t know if I was clear.

For example if we use the object info random value, we can for sure create random grey colors if we plug that value into R, G and B. We can create variations, other than grey colors, if for example we put the random value (RV) for R, SQRT(RV) for G, and RV^2 for B, but they would never be completely random colors, they would be variations in a certain range due to the dependency of the 3 values.

Ok, I hope I did understand now.

Would this solve your task ?


Edit:
Other Question: What’s the goal of your task ?

Kind regards
Alain

My task is as simple as this: to create a material with completely random color in the full range of possible colors. It is not for a specific project, I’m just trying to do it since a portuguese colleague is making some tests and created this image with a single material and would like to introduce more variation.

The suggestion you make is another possibility to create random colors, it would completely solve the task if it would be possible to include all the possible colors in the color ramp, which I don’t think its feasible.

One option I tried was to input the random number on the hue input of the “hue saturation value” node. It creates colors in the 360º range but the saturation and value are still not possible to be created randomly independent of the hue value.


Crazycourier, I didn’t look at it since I’ve just found a solution, but from your description, your script would imply different materials and assigning random RGB values to each material. if that is the case it is similar to the solution I had with the small script I put above.

After knocking with my head on the wall for several hours, I’ve found a solution to use a single material with random color and without the need of python.
The principle is that if the object info random value gives a single random value for each object and we need 3 to define a color, I extract 3 random values from the object info random.

For example if the object info random value is 0.123456, I put R=0.12, G=0.34 and B= 0.56. The R, G and B values do not have the resolution of the original value, but I have 100 random values for each channel which is quite enough (100x100x100 different colors). It is possible to have 0.123456789 turned into R=0.123, G=0.456, B=0.789, by changing the colored node values to 1000 instead of 100, but I’m not sure how many digits after the decimal point of the original value are valid.(any info on this???)

So here is an example with 841 planes with a unique material and the Blend file with the node group if someone finds it useful and to confirm if any fine tuning is needed.

http://imageshack.us/a/img21/2723/randomcolor.png

Blender file