andrea andrea - 29 days ago 10
Python Question

Add random color to active object python blender

Basically I want to add a random color to an active object in blender using python

I want to use random.randrange and i want the rgb- scale to start at 0.00 and stop at 1.0 with a difference of at least 0.3.

This is my code but somehow it doesn't work

r, g, b = random.randrange(0.0, 1.0[, 0.3])

mat.diffuse_color = (r,g,b)


Im new to blender and not sure how to this. What is the best solution?

Thanks!

Answer

You can accomplish the same with use of random.uniform(stat, end), but here you won't get any option for skipping 0.3.

So, it would be better to be with random.randrange() and here is the solution:

import random

rgb = ()
for i in range(3) :
    rgb += ((random.randrange(1, 10, 3) /10),)

mat.diffuse_color = rgb