[Bf-python] Activation of the material slots

Jean Montambeault iaminnocent at blenderj.x10.mx
Thu May 3 17:56:43 CEST 2012


Le 2012-05-02 18:22, CoDEmanX a écrit :
> I wrote a small script, which should remove unused materials from
> material slots of all selected objects:
>
> import bpy
>
> bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
>
> for ob in bpy.context.selected_objects:
>       if ob.type != 'MESH':
>           continue
>
>       mat_slots = {}
>       for p in ob.data.polygons:
>           mat_slots[p.material_index] = 1
>
>       mat_slots = mat_slots.keys()
>
>       for i in range(len(ob.material_slots)-1, -1, -1):
>           if i not in mat_slots:
>               bpy.context.object.active_material_index = i
>               bpy.ops.object.material_slot_remove()
>
> seems to work well...
>
Thank you for your answer. :)

Yes it does work very well on one object at a time. For a selection, not 
so well here. Unfortunately I have only three minutes of freedom left 
for now. I'll explore the causes when I'm back in about 10 hours time 
and let you know the results. In any case your script has everything to 
solve the problem for me in the most elegant manner that is afforded to 
us by the API. Yeah!

J.




More information about the Bf-python mailing list