forked from jomachim/Cycle-cameras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcycle_cameras.py
75 lines (58 loc) · 2.21 KB
/
cycle_cameras.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
bl_info = {
"name": "Cycle Cameras",
"author": "CoDEmanX",
"version": (1, 1),
"blender": (2, 65, 0),
"location": "View3D > Ctrl + Shift + Left/Right Arrow",
"description": "Switch scene camera to next or previous camera object",
"warning": "",
"wiki_url": "",
"category": "3D View"}
import bpy
class VIEW3D_OT_cycle_cameras(bpy.types.Operator):
"""Cycle through available cameras"""
bl_idname = "view3d.cycle_cameras"
bl_label = "Cycle Cameras"
bl_options = {'REGISTER', 'UNDO'}
direction = bpy.props.EnumProperty(
name="Direction",
items=(
('FORWARD', "Forward", "Next camera (alphabetically)"),
('BACKWARD', "Backward", "Previous camera (alphabetically)"),
),
default='FORWARD'
)
def execute(self, context):
scene = context.scene
cam_objects = [ob for ob in bpy.data.objects if ob.type == 'CAMERA']
if len(cam_objects) == 0:
return {'CANCELLED'}
try:
idx = cam_objects.index(scene.camera)
new_idx = (idx + 1 if self.direction == 'FORWARD' else idx - 1) % len(cam_objects)
except ValueError:
new_idx = 0
context.scene.camera = cam_objects[new_idx]
return {'FINISHED'}
addon_keymaps = []
def register():
bpy.utils.register_module(__name__)
wm = bpy.context.window_manager
kc = wm.keyconfigs.addon
if kc:
km = wm.keyconfigs.addon.keymaps.new(name='3D View', space_type='VIEW_3D')
kmi = km.keymap_items.new(VIEW3D_OT_cycle_cameras.bl_idname, 'RIGHT_ARROW', 'PRESS', ctrl=True, shift=True)
kmi.properties.direction = 'FORWARD'
addon_keymaps.append((km, kmi))
kmi = km.keymap_items.new(VIEW3D_OT_cycle_cameras.bl_idname, 'LEFT_ARROW', 'PRESS', ctrl=True, shift=True)
kmi.properties.direction = 'BACKWARD'
addon_keymaps.append((km, kmi))
bpy.utils.register_class(VIEW3D_OT_cycle_cameras)
def unregister():
for km, kmi in addon_keymaps:
km.keymap_items.remove(kmi)
addon_keymaps.clear()
bpy.utils.unregister_module(__name__)
bpy.utils.unregister_class(VIEW3D_OT_cycle_cameras)
if __name__ == "__main__":
register()