-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_Camera 28x.py
79 lines (58 loc) · 2.53 KB
/
add_Camera 28x.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
bl_info = {
"name": "Add Camera to View",
"author": "SayPRODUCTIONS",
"version": (1, 0),
"blender": (2, 80, 0),
"location": "View3D > Add > Camera > Camera to View",
"description": "Add Camera to View",
"warning": "",
"wiki_url": "",
"category": "Camera",
}
import bpy
from bpy.types import Operator
from bpy_extras.object_utils import AddObjectHelper, object_data_add
def CamToView(self, context):
# --------------------------------------------------------
def camera_position(matrix):
t = (matrix[0][3], matrix[1][3], matrix[2][3])
r = ((matrix[0][0], matrix[0][1], matrix[0][2]),
(matrix[1][0], matrix[1][1], matrix[1][2]),
(matrix[2][0], matrix[2][1], matrix[2][2]))
rp = ((-r[0][0], -r[1][0], -r[2][0]),
(-r[0][1], -r[1][1], -r[2][1]),
(-r[0][2], -r[1][2], -r[2][2]))
output = (rp[0][0] * t[0] + rp[0][1] * t[1] + rp[0][2] * t[2],
rp[1][0] * t[0] + rp[1][1] * t[1] + rp[1][2] * t[2],
rp[2][0] * t[0] + rp[2][1] * t[1] + rp[2][2] * t[2])
return output
for v in bpy.context.window.screen.areas:
if v.type == 'VIEW_3D':
L = v.spaces[0].region_3d.view_location
M = v.spaces[0].region_3d.view_matrix
R = v.spaces[0].region_3d.view_rotation
P = camera_position(M)
cam = bpy.data.cameras.new("Camera")
cam_ob = bpy.data.objects.new("Camera", cam)
bpy.context.scene.collection.objects.link(cam_ob)
bpy.context.scene.camera = cam_ob
cam_ob.location = P
cam_ob.rotation_mode = 'QUATERNION'
cam_ob.rotation_quaternion = R
cam_ob.rotation_mode = 'XYZ'
v.spaces[0].region_3d.view_perspective = 'CAMERA'
# --------------------------------------------------------
class OBJECT_OT_cam2view(Operator, AddObjectHelper):
bl_idname = "add.cam2view"
bl_label = "Camera to View"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
CamToView(self, context)
return {'FINISHED'}
# - Registration
def cam2view(self, context):
self.layout.operator(OBJECT_OT_cam2view.bl_idname, text="Add Camera to View", icon='OUTLINER_DATA_CAMERA')
def register(): bpy.utils.register_class(OBJECT_OT_cam2view);bpy.types.VIEW3D_MT_camera_add.append(cam2view)
def unregister(): bpy.utils.unregister_class(OBJECT_OT_cam2view);bpy.types.VIEW3D_MT_camera_add.remove(cam2view)
if __name__ == "__main__":
register()