I want to iterate over the faces of a mesh and get the diffuse color of the material that is assigned to the face. How can I get the material that is assigned to the face of the object and the diffuse color of that material?

  • 2
    $\begingroup$ When writing scripts or doing anything with python it's really useful to pull down the info header, do some action and see the command that is logged. In your case here, add a material and you will see bpy.context.object.active_material.diffuse_color, also try searching on the site, chances are someone had this same issue or something similar, ie: blender.stackexchange.com/questions/27631/…. Combining those should help and give you some pointers. $\endgroup$
    – iKlsR
    May 1, 2016 at 22:37

1 Answer 1

import bpy

context = bpy.context
obj = context.object
mesh = obj.data

for f in mesh.polygons:  # iterate over faces
    print("face", f.index, "material_index", f.material_index)
    slot = obj.material_slots[f.material_index]
    mat = slot.material
    if mat is not None:
        print("No mat in slot", f.material_index)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.