[Bf-python] Activation of the material slots

CoDEmanX codemanx at gmx.de
Mon May 7 03:35:40 CEST 2012


you're right, hidden objects should be skipped to avoid poll errors.

After object deletion there's no context, setting first object in scene 
as active to avoid incorrect context.

Dunno about layers, now checking if selected mesh is currently visible 
(on a selected layer):

------------------------------------------------------------
import bpy

def main():
     if len(bpy.data.objects) < 1:
         print("No objects")
         return

     if bpy.context.object is None:
         print("No context, new active: %s" % bpy.data.objects[0].name)
         bpy.context.scene.objects.active = bpy.data.objects[0]


     bpy.ops.object.mode_set(mode='OBJECT', toggle=False)

     for ob in bpy.data.objects:
            if ob.type != 'MESH' or not ob.select or ob.hide:
                continue

            for i, l in enumerate(bpy.context.scene.layers):
                if ob.layers[i] and ob.layers[i] == l:
                    break
            else:
                continue

            mat_slots = {}
            for p in ob.data.polygons:
                mat_slots[p.material_index] = 1

            mat_slots = mat_slots.keys()

            for i in reversed(range(len(ob.material_slots))):
                if i not in mat_slots:
                    bpy.context.scene.objects.active = ob
                    ob.active_material_index = i
                    bpy.ops.object.material_slot_remove()
main()

------------------------------------------------------------

Am 07.05.2012 02:23, schrieb geof kgeo:
> I think you'll have a error if the mesh is hided and maybe if the mesh
> is on a no-visible layer.
> kgeogeo
>
>  > Date: Sun, 6 May 2012 21:24:24 +0200
>  > From: codemanx at gmx.de
>  > To: bf-python at blender.org
>  > Subject: Re: [Bf-python] Activation of the material slots
>  >
>  > my bad, it should use the current object to set active_material_index
>  > and needs to set the ob as the scene's active object before calling the
>  > slot remove operator. Should work for multiple objects now:
>  >
>  >
>  > import bpy
>  >
>  > bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
>  >
>  > for ob in bpy.data.objects:
>  > if ob.type != 'MESH' or not ob.select:
>  > 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:
>  > ob.active_material_index = i
>  > bpy.context.scene.objects.active = ob
>  > bpy.ops.object.material_slot_remove()
>  >
>  > Am 03.05.2012 17:56, schrieb Jean Montambeault:
>  > > 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.
>  > >
>  > > _______________________________________________
>  > > Bf-python mailing list
>  > > Bf-python at blender.org
>  > > http://lists.blender.org/mailman/listinfo/bf-python
>  > _______________________________________________
>  > Bf-python mailing list
>  > Bf-python at blender.org
>  > http://lists.blender.org/mailman/listinfo/bf-python
>
>
> _______________________________________________
> 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