-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera_bookmark.py
154 lines (130 loc) · 5.39 KB
/
camera_bookmark.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import bpy
bl_info = {
"name": "Camera Bookmark",
"author": "Valérien",
"version": (1, 0),
"blender": (4, 0, 1),
"location": "View3D > Sidebar > Camera Bookmarks",
"description": "Allows bookmarking camera positions in Blender.",
"category": "Camera"
}
class BookmarkProp(bpy.types.PropertyGroup):
"""
Property group corresponding to a bookmark and storing its name, associated location and rotation.
"""
name: bpy.props.StringProperty(name="Name")
position: bpy.props.FloatVectorProperty(name="Position", subtype='XYZ')
rotation: bpy.props.FloatVectorProperty(name="Rotation", subtype='XYZ')
focal_length: bpy.props.FloatProperty(name="Focal Length")
sensor_size: bpy.props.FloatVectorProperty(name="Sensor Size", size=2)
shift_x: bpy.props.FloatProperty(name="Shift X")
shift_y: bpy.props.FloatProperty(name="Shift Y")
clip_start: bpy.props.FloatProperty(name="Clip Start")
clip_end: bpy.props.FloatProperty(name="Clip End")
use_dof: bpy.props.BoolProperty(name="Use Depth of Field")
class GoToBookmarkOperator(bpy.types.Operator):
"""
Operator that jumps to the currently selected (active) bookmark from the bookmark list.
"""
bl_idname = "camera.go_to_bookmark"
bl_label = "Go to Bookmark"
def execute(self, context):
bookmark_index = context.scene.bookmark_list_active_index
bookmark = context.scene.bookmark_list[bookmark_index]
if bookmark.position:
bpy.context.scene.camera.location = bookmark.position
if bookmark.rotation:
bpy.context.scene.camera.rotation_euler = bookmark.rotation
if bookmark.focal_length:
bpy.context.scene.camera.data.angle = bookmark.focal_length
if bookmark.sensor_size:
bpy.context.scene.camera.data.sensor_width = bookmark.sensor_size[0]
bpy.context.scene.camera.data.sensor_height = bookmark.sensor_size[1]
if bookmark.shift_x:
bpy.context.scene.camera.data.shift_x = bookmark.shift_x
if bookmark.shift_y:
bpy.context.scene.camera.data.shift_y = bookmark.shift_y
if bookmark.clip_start:
bpy.context.scene.camera.data.clip_start = bookmark.clip_start
if bookmark.clip_end:
bpy.context.scene.camera.data.clip_end = bookmark.clip_end
if not bookmark.use_dof is None:
bpy.context.scene.camera.data.dof.use_dof = bookmark.use_dof
return {'FINISHED'}
class BookmarkPanel(bpy.types.Panel):
"""
UI pannel in the N menu for registering bookmarks and jumping to them.
"""
bl_label = "Camera Bookmarks"
bl_idname = "VIEW3D_PT_camera_bookmarks"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = "Camera Bookmarks"
def draw(self, context):
layout = self.layout
scene = context.scene
row = layout.row()
row.template_list(
"UI_UL_list",
"BookmarkList", scene,
"bookmark_list", scene,
"bookmark_list_active_index"
)
col = row.column(align=True)
col.operator("camera.add_bookmark", text="", icon='ADD')
col.operator("camera.remove_bookmark", text="", icon='REMOVE')
layout.operator("camera.go_to_bookmark",
text="Go to Selected Bookmark")
class AddBookmarkOperator(bpy.types.Operator):
bl_idname = "camera.add_bookmark"
bl_label = "Add Bookmark"
def execute(self, context):
scene = context.scene
camera = context.scene.camera
if camera is not None:
bookmark = scene.bookmark_list.add()
bookmark.name = "Bookmark " + str(len(scene.bookmark_list))
bookmark.position = camera.location.copy()
bookmark.rotation = camera.rotation_euler.copy()
bookmark.focal_length = camera.data.angle
bookmark.sensor_size = (
camera.data.sensor_width, camera.data.sensor_height)
bookmark.shift_x = camera.data.shift_x
bookmark.shift_y = camera.data.shift_y
bookmark.clip_start = camera.data.clip_start
bookmark.clip_end = camera.data.clip_end
bookmark.use_dof = camera.data.dof.use_dof
return {'FINISHED'}
class RemoveBookmarkOperator(bpy.types.Operator):
bl_idname = "camera.remove_bookmark"
bl_label = "Remove Bookmark"
@classmethod
def poll(cls, context):
return context.scene.bookmark_list_active_index != -1
def execute(self, context):
scene = context.scene
scene.bookmark_list.remove(scene.bookmark_list_active_index)
context.scene.bookmark_list_active_index = min(max(0, context.scene.bookmark_list_active_index - 1),
len(scene.bookmark_list) - 1)
return {'FINISHED'}
classes = [
BookmarkProp,
BookmarkPanel,
GoToBookmarkOperator,
AddBookmarkOperator,
RemoveBookmarkOperator
]
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.Scene.bookmark_list = bpy.props.CollectionProperty(
type=BookmarkProp)
bpy.types.Scene.bookmark_list_active_index = bpy.props.IntProperty(
default=-1)
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)
del bpy.types.Scene.bookmark_list
del bpy.types.Scene.bookmark_list_active_index
if __name__ == "__main__":
register()