Force local view mode off via python

Hey there!
I would like to force blender to go out from local view, not toogle it.
Toggle is not good since if i’m not in local view i don’t want to get into local view mode, but if i’m in local view mode, i want to turn if off (not remove selected object from local view mode)

Thank You!

You can check if a space is already in local view with bpy.types.SpaceView3D.local_view. Then you can only toggle local view if there is one.

iimport bpy

area = next(a for a in bpy.context.screen.areas if a.type == "VIEW_3D")
space = area.spaces.active
if space.local_view:
    with bpy.context.temp_override(area=area):
        bpy.ops.view3d.localview()
1 Like