1
$\begingroup$

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.link(ob)
    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.

$\endgroup$
2
  • $\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$ Commented 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$ Commented Jan 18, 2018 at 4:16

1 Answer 1

0
$\begingroup$

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
$\endgroup$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.