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 '19 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 '19 at 12:36
0
$\begingroup$

The number 'zero' is just a zero based enumerator (eg: 0th,1st,2nd...etc) for the material used on your polygon.

I think, that the enumerator order corresponds to it's position in the (Materials?) editor scroll box.

If you want to see '1'. Create a new material, select your object or a face of it and "Assign" (Button) the object to the second Material in the list. Re-run your script.

..oh, also "set" only contains unique values. So if all your polgons use material '0' there will only be one entry.

| improve this answer | |
$\endgroup$

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.