-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera_switch.py
85 lines (69 loc) · 2.13 KB
/
camera_switch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
bl_info = {
"name": "Camera Switcher",
"author": "Jakub Jaszewski",
"description": "Switch between cameras in a scene with Shift+Num 0",
"version": (1, 0),
"blender": (3, 5, 0),
"location": "3D Viewport",
"category": "3D View"
}
import bpy
def get_cameras(scene):
"""
Get a list of all cameras in the scene
"""
cameras = []
for obj in scene.objects:
if obj.type == 'CAMERA':
cameras.append(obj)
return cameras
class CameraSwitcherOperator(bpy.types.Operator):
"""
Switch between cameras in the scene
"""
bl_idname = "view3d.camera_switch"
bl_label = "Camera Switcher"
@classmethod
def poll(cls, context):
"""
Only show the operator in the 3D Viewport when cameras are available
"""
return len(get_cameras(context.scene)) > 1
def execute(self, context):
"""
Cycle through cameras in the scene
"""
cameras = get_cameras(context.scene)
active_camera = context.scene.camera
# Find the index of the active camera in the list of cameras
try:
index = cameras.index(active_camera)
except ValueError:
index = 0
# Set the next camera in the list as active
if index == len(cameras) - 1:
context.scene.camera = cameras[0]
else:
context.scene.camera = cameras[index + 1]
return {'FINISHED'}
addon_keymaps = []
def register():
bpy.utils.register_class(CameraSwitcherOperator)
# Add keymap
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(CameraSwitcherOperator.bl_idname, 'NUMPAD_0', 'PRESS', shift=True)
addon_keymaps.append((km, kmi))
def unregister():
bpy.utils.unregister_class(CameraSwitcherOperator)
# Remove keymap
wm = bpy.context.window_manager
kc = wm.keyconfigs.addon
if kc:
for km, kmi in addon_keymaps:
km.keymap_items.remove(kmi)
addon_keymaps.clear()
if __name__ == "__main__":
register()