deleting objects with type ?

how can you delete all object in scene of a certain type like MESH or Curve

but not the other ones

i can make a list and check but problem is how you delete one object without changing the intitial list ?

an example would be nice here

Thanks and happy 2.5

for ob in bpy.data.objects:
    if ob.type == 'MESH':
        bpy.data.objects.delete(ob)

That won’t work because it you delete an item from bpy.data.objects the list doesn’t get dynamically updated during your for loop.

Or maybe it will work – too lazy to actually test – but the list still won’t get updated either way.

You need to start over at the beginning of the list every time you delete an object to make sure your references are valid so you don’t crash blender…just the way things work unfortunately.

guess it changes the list and it does not like it

but found another way not elegant but it works fine


#    lets remove all the objects except the basic one
 
    kk1=0
    listname1=[]
 
    for each in range(len(sceobj)):
        print ('KK =',kk1,'Object name =',sceobj[each].name,' Type=',sceobj[each].type)
        if sceobj[each].type =="MESH" or sceobj[each].type =="TEXT" or sceobj[each].type =="CURVE" or sceobj[each].type =="EMPTY":
            listname1.append(sceobj[each].name)
        kk1+=1
 
    print ()
    print ('List of delete objects=',listname1)
 
    print ()
 
    kk1=0
 
 
    print ()
    print ('Qty objects to delete =',len(listname1))
    print ()
 
    kk2=0
    for i1 in range(len(listname1)):
 
        print ('KK2 =',kk2,'Delete  Object name =',listname1[i1])
        ob1 = bpy.data.objects[listname1[i1]]
        bpy.context.scene.objects.unlink(ob1)
        bpy.data.objects.remove(ob1)
        kk2+=1
 
    print ()
    print ()
 
 
 

that one works fine
but wish there was a more elegant way of dong it !

is it necessary to have the unlink and the remove commands to delete and object ?

and there must be another way using BPY.data or BPYtype to deletes objects but how ?

Thanks

You can’t delete an object if it’s linked into a scene so…

I tried to get a scene.objects.delete() function in with the scene.objects.unlink() bugfix (which was actually deleting objects instead of just unlinking) but it was veto’d by Campbell.

ok so you need the unlink and then the remove one for BPU.OPS

now how to do it with other way in BPY.DATA or BPY.type ?

Thanks