I have the following two functions:

def get_random_color():
    ''' generate rgb using a list comprehension '''
    r, g, b = [random.random() for i in range(3)]
    return r, g, b, 1

def createMeshFromData(name, origin, verts, faces):
    # Create mesh and object
    me = bpy.data.meshes.new(name + 'Mesh')
    ob = bpy.data.objects.new(name, me)
    ob.location = origin
    ob.show_name = True

    # Link object to scene and make active
    scn = bpy.context.scene
    scn.objects.active = ob
    ob.select = True
    ob.color = get_random_color()

    # Create mesh from given verts, faces.
    me.from_pydata(verts, [], faces)
    # Update mesh with new data
    return ob

createMeshFromData gets its data from a database for verts and faces. Later on it will also get a color value.

So far I am trying to assign a random color using the function get_random_color. I assign this color with ob.color = get_random_color() but this does not seem to work once I press F12 to render.

How to assign a color value to an object?

I am not familiar with blender (coming from matplotlib) and I would apprecite your help.

  • $\begingroup$ You cant just blindly assign color values to an object or even a face, as far as I know. Colors go either into materials, textures, or at best vertex color layers. $\endgroup$ Jan 18, 2018 at 4:07
  • $\begingroup$ @DuarteFarrajotaRamos Thanks. I have found this blender.stackexchange.com/a/23434/51114 to assign a material to my object. $\endgroup$
    – Michael
    Jan 18, 2018 at 4:16

1 Answer 1


Set Material.use_object_color to True

From code in this answer which demonstrates use of object colour

# get mat create.
mat = bpy.data.materials.get("randobjcol")
if not mat:
    mat = bpy.data.materials.new("randobjcol")
    mat.use_object_color = True

# assign to object
ob.active_material = mat

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .