78 lines
2.5 KiB
Python
78 lines
2.5 KiB
Python
bl_info = {
|
|
"name": "Preserve View on Local View Toggle",
|
|
"blender": (2, 80, 0),
|
|
"category": "3D View",
|
|
"description": "Toggles local view without changing the 3D or camera view",
|
|
}
|
|
|
|
import bpy
|
|
|
|
class VIEW3D_OT_preserve_local_view(bpy.types.Operator):
|
|
bl_idname = "view3d.preserve_local_view"
|
|
bl_label = "Toggle Local View (Preserve View)"
|
|
bl_options = {'REGISTER', 'UNDO'}
|
|
|
|
def execute(self, context):
|
|
for area in context.screen.areas:
|
|
if area.type != 'VIEW_3D':
|
|
continue
|
|
space = area.spaces.active
|
|
region_3d = space.region_3d
|
|
|
|
# Detect if in camera view
|
|
in_camera_view = region_3d.view_perspective == 'CAMERA'
|
|
active_camera = context.scene.camera if in_camera_view else None
|
|
|
|
# Save regular view
|
|
view_location = region_3d.view_location.copy()
|
|
view_rotation = region_3d.view_rotation.copy()
|
|
view_distance = region_3d.view_distance
|
|
|
|
# Save camera transform
|
|
if in_camera_view and active_camera:
|
|
cam_location = active_camera.location.copy()
|
|
cam_rotation = active_camera.rotation_euler.copy()
|
|
|
|
# Toggle Local View
|
|
bpy.ops.view3d.localview()
|
|
|
|
# Restore regular view
|
|
region_3d.view_location = view_location
|
|
region_3d.view_rotation = view_rotation
|
|
region_3d.view_distance = view_distance
|
|
|
|
# Restore camera transform and force camera view again
|
|
if in_camera_view and active_camera:
|
|
active_camera.location = cam_location
|
|
active_camera.rotation_euler = cam_rotation
|
|
|
|
# Make sure we're still in camera view
|
|
region_3d.view_perspective = 'CAMERA'
|
|
|
|
break
|
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
addon_keymaps = []
|
|
|
|
def register():
|
|
bpy.utils.register_class(VIEW3D_OT_preserve_local_view)
|
|
|
|
# Add keymap: bind to Numpad /
|
|
wm = bpy.context.window_manager
|
|
kc = wm.keyconfigs.addon
|
|
if kc:
|
|
km = kc.keymaps.new(name="3D View", space_type='VIEW_3D')
|
|
kmi = km.keymap_items.new("view3d.preserve_local_view", type='NUMPAD_SLASH', value='PRESS')
|
|
addon_keymaps.append((km, kmi))
|
|
|
|
def unregister():
|
|
for km, kmi in addon_keymaps:
|
|
km.keymap_items.remove(kmi)
|
|
addon_keymaps.clear()
|
|
|
|
bpy.utils.unregister_class(VIEW3D_OT_preserve_local_view)
|
|
|
|
if __name__ == "__main__":
|
|
register()
|