You can set the index of the list to 0
, iterate through all slots and override the context of material_slot_remove()
:.
Blender 3.2+
import bpy
from bpy import context
for obj in context.selected_editable_objects:
obj.active_material_index = 0
for i in range(len(obj.material_slots)):
with context.temp_override(object=obj):
bpy.ops.object.material_slot_remove()
Blender 2.8+
import bpy
for obj in bpy.context.selected_editable_objects:
obj.active_material_index = 0
for i in range(len(obj.material_slots)):
bpy.ops.object.material_slot_remove({'object': obj})