1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # -----------------------------------------------------------------------
4 # Author: Alan Odom (Clockmender), Rune Morling (ermo) Copyright (c) 2019
6 # Contains code which was inspired by the "Reset 3D View" plugin authored
7 # by Reiner Prokein (tiles) Copyright (c) 2014 (see T37718)
8 # -----------------------------------------------------------------------
11 from bpy
.types
import Operator
13 from mathutils
import Quaternion
14 from .pdt_functions
import debug
, euler_to_quaternion
17 class PDT_OT_ViewRot(Operator
):
18 """Rotate View by Absolute Coordinates."""
20 bl_idname
= "pdt.viewrot"
21 bl_label
= "Rotate View"
22 bl_options
= {"REGISTER", "UNDO"}
23 bl_description
= "View Rotation by Absolute Values"
25 def execute(self
, context
):
26 """View Rotation by Absolute Values.
29 Rotations are converted to 3x3 Quaternion Rotation Matrix.
30 This is an Absolute Rotation, not an Incremental Orbit.
31 Uses pg.rotation_coords scene variable
34 context: Blender bpy.context instance.
42 roll_value
= euler_to_quaternion(
43 pg
.rotation_coords
.x
* pi
/ 180,
44 pg
.rotation_coords
.y
* pi
/ 180,
45 pg
.rotation_coords
.z
* pi
/ 180,
47 context
.region_data
.view_rotation
= roll_value
51 class PDT_OT_ViewRotL(Operator
):
52 """Rotate View Left."""
54 bl_idname
= "pdt.viewleft"
55 bl_label
= "Rotate Left"
56 bl_options
= {"REGISTER", "UNDO"}
57 bl_description
= "View Orbit Left by Delta Value"
59 def execute(self
, context
):
60 """View Orbit Left by Delta Value.
63 Uses pg.vrotangle scene variable
64 Orbits view to the left about its vertical axis
67 context: Blender bpy.context instance.
75 bpy
.ops
.view3d
.view_orbit(angle
=(pg
.vrotangle
* pi
/ 180), type="ORBITLEFT")
79 class PDT_OT_ViewRotR(Operator
):
80 """Rotate View Right."""
82 bl_idname
= "pdt.viewright"
83 bl_label
= "Rotate Right"
84 bl_options
= {"REGISTER", "UNDO"}
85 bl_description
= "View Orbit Right by Delta Value"
87 def execute(self
, context
):
88 """View Orbit Right by Delta Value.
91 Uses pg.vrotangle scene variable
92 Orbits view to the right about its vertical axis
95 context: Blender bpy.context instance.
101 scene
= context
.scene
103 bpy
.ops
.view3d
.view_orbit(angle
=(pg
.vrotangle
* pi
/ 180), type="ORBITRIGHT")
107 class PDT_OT_ViewRotU(Operator
):
108 """Rotate View Up."""
110 bl_idname
= "pdt.viewup"
111 bl_label
= "Rotate Up"
112 bl_options
= {"REGISTER", "UNDO"}
113 bl_description
= "View Orbit Up by Delta Value"
115 def execute(self
, context
):
116 """View Orbit Up by Delta Value.
119 Uses pg.vrotangle scene variable
120 Orbits view up about its horizontal axis
123 context: Blender bpy.context instance.
129 scene
= context
.scene
131 bpy
.ops
.view3d
.view_orbit(angle
=(pg
.vrotangle
* pi
/ 180), type="ORBITUP")
135 class PDT_OT_ViewRotD(Operator
):
136 """Rotate View Down."""
138 bl_idname
= "pdt.viewdown"
139 bl_label
= "Rotate Down"
140 bl_options
= {"REGISTER", "UNDO"}
141 bl_description
= "View Orbit Down by Delta Value"
143 def execute(self
, context
):
144 """View Orbit Down by Delta Value.
147 Uses pg.vrotangle scene variable
148 Orbits view down about its horizontal axis
151 context: Blender bpy.context instance.
157 scene
= context
.scene
159 bpy
.ops
.view3d
.view_orbit(angle
=(pg
.vrotangle
* pi
/ 180), type="ORBITDOWN")
163 class PDT_OT_ViewRoll(Operator
):
166 bl_idname
= "pdt.viewroll"
167 bl_label
= "Roll View"
168 bl_options
= {"REGISTER", "UNDO"}
169 bl_description
= "View Roll by Delta Value"
171 def execute(self
, context
):
172 """View Roll by Delta Value.
175 Uses pg.vrotangle scene variable
176 Rolls view about its normal axis
179 context: Blender bpy.context instance.
185 scene
= context
.scene
187 bpy
.ops
.view3d
.view_roll(angle
=(pg
.vrotangle
* pi
/ 180), type="ANGLE")
191 class PDT_OT_ViewIso(Operator
):
192 """Set View Isometric."""
194 bl_idname
= "pdt.viewiso"
195 bl_label
= "Isometric View"
196 bl_options
= {"REGISTER", "UNDO"}
197 bl_description
= "Isometric View"
199 def execute(self
, context
):
200 """Set Isometric View.
203 Set view orientation to Orthographic Isometric
206 context: Blender bpy.context instance.
212 # Rotate view 45 degrees about Z then 32.2644 about X
213 context
.region_data
.view_rotation
= Quaternion((0.8205, 0.4247, -0.1759, -0.3399))
214 context
.region_data
.view_perspective
= "ORTHO"
218 class PDT_OT_Reset3DView(Operator
):
219 """Reset Views to Factory Default."""
221 bl_idname
= "pdt.reset_3d_view"
222 bl_label
= "Reset 3D View"
223 bl_options
= {"REGISTER", "UNDO"}
224 bl_description
= "Reset 3D View to Blender Defaults"
226 def execute(self
, context
):
227 """Reset 3D View to Blender Defaults.
230 context: Blender bpy.context instance.
236 # The default view_distance to the origin when starting up Blender
237 default_view_distance
= 17.986562728881836
238 default_view_distance
= (
239 bpy
.data
.screens
["Layout"].areas
[-1].spaces
[0].region_3d
.view_distance
241 # The default view_matrix when starting up Blender
242 default_view_matrix
= (
243 (0.41, -0.4017, 0.8188, 0.0),
244 (0.912, 0.1936, -0.3617, 0.0),
245 (-0.0133, 0.8950, 0.4458, 0.0),
246 (0.0, 0.0, -17.9866, 1.0),
249 view
= context
.region_data
250 debug(f
"is_orthographic_side_view: {view.is_orthographic_side_view}")
251 if view
.is_orthographic_side_view
:
252 # When the view is orthographic, reset the distance and location.
253 # The rotation already fits.
254 debug(f
"view_distance before reset: {view.view_distance}")
255 debug(f
"view_location before reset: {view.view_location}")
256 view
.view_distance
= default_view_distance
257 view
.view_location
= (-0.0, -0.0, -0.0)
259 debug(f
"view_distance AFTER reset: {view.view_distance}")
260 debug(f
"view_location AFTER reset: {view.view_location}")
262 # Otherwise, the view matrix needs to be reset.
263 debug(f
"view_matrix before reset:\n{view.view_matrix}")
264 view
.view_matrix
= default_view_matrix
265 view
.view_distance
= default_view_distance
267 debug(f
"view_matrix AFTER reset:\n{view.view_matrix}")