<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>
I think you'll have a error if the mesh is hided and maybe if the mesh is on a no-visible layer.<div>kgeogeo<br><br><div><div id="SkyDrivePlaceholder"></div>> Date: Sun, 6 May 2012 21:24:24 +0200<br>> From: codemanx@gmx.de<br>> To: bf-python@blender.org<br>> Subject: Re: [Bf-python] Activation of the material slots<br>> <br>> my bad, it should use the current object to set active_material_index <br>> and needs to set the ob as the scene's active object before calling the <br>> slot remove operator. Should work for multiple objects now:<br>> <br>> <br>> import bpy<br>> <br>> bpy.ops.object.mode_set(mode='OBJECT', toggle=False)<br>> <br>> for ob in bpy.data.objects:<br>>        if ob.type != 'MESH' or not ob.select:<br>>            continue<br>> <br>>        mat_slots = {}<br>>        for p in ob.data.polygons:<br>>            mat_slots[p.material_index] = 1<br>> <br>>        mat_slots = mat_slots.keys()<br>> <br>>        for i in range(len(ob.material_slots)-1, -1, -1):<br>>            if i not in mat_slots:<br>>                ob.active_material_index = i<br>>                bpy.context.scene.objects.active = ob<br>>                bpy.ops.object.material_slot_remove()<br>> <br>> Am 03.05.2012 17:56, schrieb Jean Montambeault:<br>> > Le 2012-05-02 18:22, CoDEmanX a écrit :<br>> >> I wrote a small script, which should remove unused materials from<br>> >> material slots of all selected objects:<br>> >><br>> >> import bpy<br>> >><br>> >> bpy.ops.object.mode_set(mode='OBJECT', toggle=False)<br>> >><br>> >> for ob in bpy.context.selected_objects:<br>> >>        if ob.type != 'MESH':<br>> >>            continue<br>> >><br>> >>        mat_slots = {}<br>> >>        for p in ob.data.polygons:<br>> >>            mat_slots[p.material_index] = 1<br>> >><br>> >>        mat_slots = mat_slots.keys()<br>> >><br>> >>        for i in range(len(ob.material_slots)-1, -1, -1):<br>> >>            if i not in mat_slots:<br>> >>                bpy.context.object.active_material_index = i<br>> >>                bpy.ops.object.material_slot_remove()<br>> >><br>> >> seems to work well...<br>> >><br>> > Thank you for your answer. :)<br>> ><br>> > Yes it does work very well on one object at a time. For a selection, not<br>> > so well here. Unfortunately I have only three minutes of freedom left<br>> > for now. I'll explore the causes when I'm back in about 10 hours time<br>> > and let you know the results. In any case your script has everything to<br>> > solve the problem for me in the most elegant manner that is afforded to<br>> > us by the API. Yeah!<br>> ><br>> > J.<br>> ><br>> > _______________________________________________<br>> > Bf-python mailing list<br>> > Bf-python@blender.org<br>> > http://lists.blender.org/mailman/listinfo/bf-python<br>> _______________________________________________<br>> Bf-python mailing list<br>> Bf-python@blender.org<br>> http://lists.blender.org/mailman/listinfo/bf-python<br></div></div>                                          </div></body>
</html>