This question already has an answer here:
In Blender 2.80, I've deleted a material and have empty material slots on my objects. I want to clean this up.
I have this bit of code:
def clean_mat_slots(self): bpy.ops.object.select_all(action='DESELECT') for ob in bpy.data.objects: if ob.type != 'MESH': continue ob.select_set(state=True) bpy.context.view_layer.objects.active = ob # Delete all material slots in bounds object for i in range(len(ob.material_slots)): print("mat slot:", i) ob.active_material_index = i - 1 bpy.ops.object.material_slot_remove()
But it does not clean up the slots as I expect. There are no errors reported when I run the code.