Context.selected_objects not reverting after temp_override (selected_objects=...) block

print([o.name for o in bpy.context.selected_objects])
temp_selected_objects = [...]
with bpy.context.temp_override(selected_objects=temp_selected_objects):
    bpy.ops.object.duplicate(linked=True)
print([o.name for o in bpy.context.selected_objects])

I would expect the 2 prints to output the same, however after ‘bpy.ops.object.duplicate’ which changes the selection to the new duplicates, it appears to overwrite the original context.selected_objects, despite being scoped in a temp_override call.
It would be useful for my purposes so it remembered the original selection once outside of the ‘with’ scope, which is easily doable… but I’m just wondering why does it do that?

The with clause only lets you provide overriden input arguments, it does not have any effect on side effects or output variables. bpy.ops.object.duplicate will select the duplicated objects wether it was called from an override or from a button in the interface or from the shortcut. But as you said it would be pretty easy to store a list of selected object names beforehand (don’t store a list of object pointers, they get invalidated easily) and retrieve it afterwards.

1 Like