Random colours in single object


Today I’ve ran into this problem. I literaly got 50 000 cubes in single object, I’ve tried separating them but after 1 hour blender was just not responding. I need to make them different colours (random colours taken from a ramp).
Any ideas? Thanks in advance!

if the cubes need to be a random solid colour then that will be hard… if it doesnt matter that the cubes could have gradients in it, manipulate the geometry>position output… you will need to manipulate it with math nodes and what not so it is in a 0-1 space but yeh that is your best bet if you cant separate them.

That being said, just thinking about it, what you could do is set up 30 materials… and manually assign them in the viewport by going select > random entering a low percentage and then pressing ctrl + l to select the linked faces… then assign those faces to a material and then hide, repeat until nothing is left.

check the script forum there is a script somewhere that can add random colors faces on object
normally you add specific mat with color on object!

select an object

then run this script in text editor
and it will add random color for each face

import bpy
import bmesh
def edit_mode_out():
    bpy.ops.object.mode_set(mode = 'OBJECT')
def edit_mode_in():
    bpy.ops.object.mode_set(mode = 'EDIT')
edit_mode_out()                                            # go into object mode

ob_act = bpy.context.active_object
me = ob_act.data
                                                        # Creates new materials
mat = bpy.data.materials.new('red1')
mat.diffuse_color = ( 1.0, 0.0, 0.0 )
mat1 = bpy.data.materials.new('blue1')
mat1.diffuse_color = ( 0.0, 0.0, 1.0 )
mat2 = bpy.data.materials.new('green1')
mat2.diffuse_color = ( 0.0, 1.0, 0.0 )
list_0 = [m.name for m in bpy.data.materials]
print ()
print (' Mat List will be in alphabetical order')
print ()
for i1 in range(0,len(list_0 )):
    print (' color =',list_0 [i1],'   ',i1)
print ()

print ('List of materials =',list_0)
print ()
mi2 = list_0.index(colorselect)
print ('mi  selected color in mat list =',colorselect,' mi2=',mi2)
print ()
mi = list_0.index(colorselect)
print ()
print ('Note  The first color will be added to all the faces by default')
print ()
print ('mi  selected color in mat list =',colorselect,' mi=',mi)
print ()
#    Note  The first color will be added to all the faces by default
#    Add a base color to the mesh
#    http://www.blender.org/documentation/blender_python_api_2_62_0/contents.html
#    http://www.blender.org/documentation/blender_python_api_2_62_0/bpy.types.MeshFace.html?highlight=meshface#bpy.types.MeshFace
#    http://www.blender.org/documentation/blender_python_api_2_62_0/bpy.types.MeshPolygon.html?highlight=polygon#bpy.types.MeshPolygon.material_index
me.polygons[0].material_index=2                                # add a Red base color to all faces automatically
print ('me.polygons =',me.polygons)            # represent number of faces  tris quads  polygon
print ()
print ('me.polygons =',me.polygons)
print ('polygon lenght =',len(me.polygons))        # number of faces ( 3 + 4+ n ) 
print ()
print ('me.polygons[0] =',me.polygons[0])
print ()
print ('me.polygons[0].material_index =',me.polygons[0].material_index)
print ()
print ('me.polygons[1].material_index =',me.polygons[1].material_index)
print ()
print ()
print (    'Note  The first color will be added to all the faces by default' )
print (     'Add a base color to the mesh')
#    red            blue            green
print ()
for p1 in me.polygons:                                    # assign material if faces selected
    print ('p1 =',p1,' p1 select =',p1.select)
    if p1.select:
        print ('before material index  =',p1.material_index)
#        p1.material_index = mi
        p1.material_index = 1
        print ('after material index  =',p1.material_index)
print ()
print ('me.polygons[0].material_index =',me.polygons[0].material_index)
print ()
print ('me.polygons[1].material_index =',me.polygons[1].material_index)
print ()
print ('faces color =',)
print ()
for p1 in me.polygons:
    print ('KK=',kk,'material index  =',p1.material_index)
    print (' color =  list_0[ ',kk1,'] = ',list_0[kk1])
    print (' len verts',len(p1.vertices))
    print (' verts',p1.vertices)
    for j1 in range(0,len(p1.vertices)):
        print ('vert[',j1,']=',p1.vertices[j1])
print ()
bme = bmesh.new()
edit_mode_in()                                # go into edit mode

happy bl

Thanks everyone, I’ve tried manipulating the geometry - position but it just gives (often) a solid ramp. It doesn’t look random at all. And I tought about doing 30 materials but that was pretty stupid if there are addons for it, now I’ll try to do it with the code, thanks!

It gave me this. Only one line is affected due to the Array modifier, but why it is only one colour?

You’re better off using particles

Really nice idea. Haven’t tought about this :smiley: I already made it with 6 materials but this is nice too. Thanks!

Assigning random materials onto different objects under the array is not possible… you would have to either make it in to instances or apply the arrays.

It’s the same object. I knew it that’s why I’ve aplied one modifier (to show how it works).

did you try the little script I uploaded
is it like what you wanted or what is missing ?

happy bl

Try this.
random_cubes.blend (837 KB)


1 Like