forked from s-leger/archipack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchipack_viewmanager.py
169 lines (158 loc) · 5.72 KB
/
archipack_viewmanager.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- coding:utf-8 -*-
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
# ----------------------------------------------------------
# Author: Stephen Leger (s-leger)
#
# ----------------------------------------------------------
import bpy
from mathutils import Vector, Matrix, Quaternion
class ViewManager():
"""
A class to set view so it is able to perform knife project
"""
def __init__(self, context):
self.view3d = context.space_data
self.scene = context.scene
self.distance = 0
self.location = Vector()
self.rotation = Quaternion()
self.perspective = ""
self.lens = 0
self.clip_start = 0
self.clip_end = 0
self.camera_name = ""
self.camera_type = ""
self.lock_object_name = ""
def save(self):
view3d = self.view3d
region3d = view3d.region_3d
self.distance = region3d.view_distance
self.location = region3d.view_location.copy()
self.rotation = region3d.view_rotation.copy()
self.perspective = region3d.view_perspective
self.lens = view3d.lens
self.clip_start = view3d.clip_start
self.clip_end = view3d.clip_end
if region3d.view_perspective == 'CAMERA':
self.camera_type = view3d.camera.type
self.camera_name = view3d.camera.name
if view3d.lock_object is not None:
self.lock_object_name = view3d.lock_object.name
else:
self.lock_object_name = ""
def safe_ortho_view(self, radius, location):
"""
Set view projection so it is able to handle knife project
Knife project work best in ortho oblique view
Center view on target, set distance and
optimize view clip so whole target is visible
"""
distance = 2 * radius
bound = Vector((radius, -radius, 0))
v = Vector((1, -1, 1)).normalized()
z = -v
x = z.cross(Vector((0, 0, 1)))
y = x.cross(z)
loc = location + distance * v
# Inverse view matrix
itM = Matrix([
[x.x, y.x, z.x, loc.x],
[x.y, y.y, z.y, loc.y],
[x.z, y.z, z.z, loc.z],
[0, 0, 0, 1]]).inverted()
view3d = self.view3d
view3d.lens = 35
start = abs((itM * -bound).z)
end = abs((itM * bound).z)
if start > end:
end, start = start, end
view3d.clip_start = 0.1
view3d.clip_end = 20 * end
region3d = view3d.region_3d
region3d.view_perspective = 'ORTHO'
region3d.view_rotation = Quaternion((
0.8204732537269592,
0.42470818758010864,
0.17591983079910278,
0.33985117077827454
))
region3d.view_distance = distance
region3d.view_location = location
region3d.update()
def safe_knife_project(self, radius, location):
"""
Set view projection so it is able to handle knife project
Knife project work best in ortho oblique view
Center view on target, set distance and
optimize view clip so whole target is visible
"""
distance = 2 * radius
bound = Vector((radius, -radius, 0))
v = Vector((1, -1, 1)).normalized()
z = -v
x = z.cross(Vector((0, 0, 1)))
y = x.cross(z)
loc = location + distance * v
# Inverse view matrix
itM = Matrix([
[x.x, y.x, z.x, loc.x],
[x.y, y.y, z.y, loc.y],
[x.z, y.z, z.z, loc.z],
[0, 0, 0, 1]]).inverted()
view3d = self.view3d
view3d.lens = 35
start = abs((itM * -bound).z)
end = abs((itM * bound).z)
if start > end:
end, start = start, end
view3d.clip_start = max(0, start - 1)
view3d.clip_end = 1 + end
region3d = view3d.region_3d
region3d.view_perspective = 'ORTHO'
region3d.view_rotation = Quaternion((
0.8204732537269592,
0.42470818758010864,
0.17591983079910278,
0.33985117077827454
))
region3d.view_distance = distance
region3d.view_location = location
region3d.update()
def restore(self):
view3d = self.view3d
region3d = view3d.region_3d
region3d.view_distance = self.distance
region3d.view_location = self.location
region3d.view_rotation = self.rotation
region3d.view_perspective = self.perspective
view3d.lens = self.lens
view3d.clip_start = self.clip_start
view3d.clip_end = self.clip_end
if self.perspective == "CAMERA":
lock_obj = self._get_object(self.lock_object_name)
if lock_obj:
view3d.lock_object = lock_obj
else:
cam = self._get_object(self.camera_name)
if cam:
view3d.camera = cam
region3d.update()
def _get_object(self, name):
return bpy.data.objects.get(name)