Before 4.0, it was possible to use context override with operators. It was very handy to be able to delete a list of objects without messing with the current selection.
objs = [bpy.context.scene.objects['my_obj_01'], bpy.context.scene.objects['my_obj_02']]
bpy.ops.object.delete({"selected_objects": objs})
This got removed in 4.0: https://projects.blender.org/blender/blender/commit/ac263a9bce53e190d07d679a058a230e91e722be
It now returns an error:
ValueError: 1-2 args execution context is supported
What is the best way to do the same thing now? This is my pretty long workaround:
# Objects to delete
objs = [bpy.context.scene.objects['my_obj_01'], bpy.context.scene.objects['my_obj_02']]
#Store the current object selection
current_sel = context.selected_objects
active_obj = context.view_layer.objects.active
# Check if objects to delete are currently selected and remove them or it will fail at restoring selection.
for o in objs:
if active_obj == o:
active_obj = None
if o in current_sel:
current_sel.remove(o)
# Select objects to delete
bpy.ops.object.select_all(action='DESELECT')
for o in objs:
bpy.data.objects[o].select_set(True)
bpy.ops.object.delete()
#Restore the previous selection
if current_sel:
for o in current_sel:
bpy.data.objects[o.name].select_set(True)
if active_obj:
context.view_layer.objects.active = active_obj
with bpy.context.temp_override(...)
$\endgroup$