How to get material_index from individual faces in a Blender 2.8 Script Did you know, you can assign different materials (eg:colors) to different faces in a polygon?
Before Blender 2.8 you could get a list of the materials used for each Face with:
for aFace in anObject.data.polygons : print (&quot;material: &quot;, face.material_index)

However, in Blender 2.8, I guess 'material_index' is no longer a property of polygons. I'll show you:
&gt;&gt;&gt; anObject.data.polygons.material_index
Traceback (most recent call last):
  File &quot;&lt;blender_console&gt;&quot;, line 1, in &lt;module&gt;
AttributeError: 'bpy_prop_collection' object has no attribute 'material_index'

How can I walk through polygons or faces and get the material_index for each?
Trying to write a file exporter for faces and their colors.
I used faces, I'm pretty sure that's what you were going after . It requires you are in edit mode to work and a mesh is currently selected.
import bpy
import bmesh

ob = bpy.context.object
assert ob is not None and ob.type == 'MESH', &quot;active object invalid, select a mesh&quot;
materials = ob.data.materials.items()
mesh = bmesh.from_edit_mesh(ob.data)
indexes = [[face.index, face.material_index] for face in mesh.faces]
material_indexes = []
for index in indexes:
    tuple = (index[0], materials[index[1]])
    print(tuple)
    material_indexes.append(tuple)

Result:
(0, ('mat2', bpy.data.materials['mat2']))
(1, ('Material', bpy.data.materials['Material']))
(2, ('Material', bpy.data.materials['Material']))
(3, ('Material', bpy.data.materials['Material']))
(4, ('Material', bpy.data.materials['Material']))
(5, ('Material', bpy.data.materials['Material']))

if you need just the material_index, you can take it back a step:
print(indexes)