[Bf-python] Activation of the material slots

CoDEmanX codemanx at gmx.de
Thu May 3 00:22:37 CEST 2012


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...


Am 01.05.2012 11:08, schrieb Jean Montambeault:
> When separating>  By loose parts, each object inherit of all the
> materials of the original object. This means, quite often that it is
> burdened with materials that are not assigned to any face and yet are
> persistent. Cleaning this up implies a long workaround in Python while
> making it possible to activate them, e.g. in the manner that we already
> activate vertex groups, for testing and eventual removing would simplify
> everything.
>
> Thank you.
>
> J.
> _______________________________________________
> Bf-python mailing list
> Bf-python at blender.org
> http://lists.blender.org/mailman/listinfo/bf-python



More information about the Bf-python mailing list