0
$\begingroup$

I am new to bpy and I am wondering why the values material_index returns are all zeros.

This is the code I am using:

bpy.ops.object.select_all(action='SELECT')
for obj in bpy.context.selected_objects:
    bpy.context.scene.objects.active = obj
    obj.select = True    
    material_ids = set()
    if obj.data is not None and hasattr(obj.data, "polygons"):
        for polygon in obj.data.polygons:
            material_ids.add(polygon.material_index)

material_ids is a set that only contains 0

Thank you for your help!

$\endgroup$
  • 4
    $\begingroup$ polygon.material_index indicates the material to use for this polygon. If your object has only 1 material, all polygons will have the same/first material which index is zero. material_index is considered to be the index in the object material list (not in the overall material list). $\endgroup$ – lemon Oct 14 at 11:22
  • $\begingroup$ and for that matter,also if the mesh object is created for an object with no materials (0 is the default material index value for a face). To be consise it is the index of obj.material_slots Suggest create a test object that has more than one material applied to more than one face. Re code: See no need to select all with operator, or set active object for this. For all meshes in scene = bpy.context.scene ie the set : meshes = set(o.data for o in scene.objects if o.type == 'MESH') Then for each mesh in meshes, material_ids = set(f.material_index for f in mesh.polygons) $\endgroup$ – batFINGER Oct 15 at 12:36

Your Answer

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

Browse other questions tagged or ask your own question.