Help deleting object in scene

I’d like to delete an object from selected objects, but remain the rest selected.

I’ve tried this:

for ob in bpy.context.selected_objects:
                                        
    if ob.type == 'MESH' and ob.name.startswith("Cluster"):
        bpy.ops.object.delete()

after deleting the object, whatever else was selected, gets deselected. Any way around this? I’ve tried adding ob.select =True, but no luck

anyone know?

I’m not good with python but I solved this making 2 lists . One for selected objects and one for objects which are going to be deleted.

You can also just rename the object so you can find it easily for deletion.

For example : Selected object list might search for “Cluster” to fill the list. And garbage list might search for “junkCluster” . If you rename the object from “Cluster” to “junkCluster” you can loop through “junkCluster” objects and delete each . Then just loop through “Cluster” objects and re populate the list (and select each one).

Anyway its not elegant but it works.

I have been out of the loop on the Python programming for a while, but my bet is you might need to create something to store your objects that you do not want to delete in, deselect all of the ones you do not want to delete, delete the one you need to delete, then using the container you stored the other objects in, iterate through and re-select them.


import bpy


for ob in bpy.context.selected_objects:
                                        
    if ob.type == 'MESH' and ob.name.startswith("Cluster"):
        #bpy.ops.object.delete()
        ob.data.user_clear()
        bpy.data.meshes.remove(ob.data)
        bpy.context.scene.objects.unlink(ob)