# How to assign a color value to face (object) using python

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
me.update()
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.

• 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. – Duarte Farrajota Ramos Jan 18 '18 at 4:07
• @DuarteFarrajotaRamos Thanks. I have found this blender.stackexchange.com/a/23434/51114 to assign a material to my object. – Michael Jan 18 '18 at 4:16

Set Material.use_object_color to True
# get mat create.