3
$\begingroup$

I need some help in removing all material slots in one go using a python script. I am able to use this, would prefer to ave all removed.

bpy.context.object.active_material_index = 0
bpy.ops.object.material_slot_remove()
bpy.context.object.active_material_index = 1
bpy.ops.object.material_slot_remove()
bpy.context.object.active_material_index = 2
bpy.ops.object.material_slot_remove()
$\endgroup$
  • $\begingroup$ You'll find some good answers here $\endgroup$ – Robin Betts Jul 29 at 17:07
2
$\begingroup$
import bpy #import the blender python library
for x in bpy.context.object.material_slots: #For all of the materials in the selected object:
    bpy.context.object.active_material_index = 0 #select the top material
    bpy.ops.object.material_slot_remove() #delete it
$\endgroup$
2
$\begingroup$

You can set the index of the list to 0, iterate through all slots and override the context of material_slot_remove():

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})
$\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.