Fix T71100: Node Wrangler creates nodes on linked node trees
[blender-addons.git] / precision_drawing_tools / pdt_view.py
blobc56b27afbea9c7a12bf78547791daf573dc8199d
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 # -----------------------------------------------------------------------
10 import bpy
11 from bpy.types import Operator
12 from math import pi
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.
28 Note:
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
33 Args:
34 context: Blender bpy.context instance.
36 Returns:
37 Status Set.
38 """
40 scene = context.scene
41 pg = scene.pdt_pg
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
48 return {"FINISHED"}
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.
62 Note:
63 Uses pg.vrotangle scene variable
64 Orbits view to the left about its vertical axis
66 Args:
67 context: Blender bpy.context instance.
69 Returns:
70 Status Set.
71 """
73 scene = context.scene
74 pg = scene.pdt_pg
75 bpy.ops.view3d.view_orbit(angle=(pg.vrotangle * pi / 180), type="ORBITLEFT")
76 return {"FINISHED"}
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.
90 Note:
91 Uses pg.vrotangle scene variable
92 Orbits view to the right about its vertical axis
94 Args:
95 context: Blender bpy.context instance.
97 Returns:
98 Status Set.
99 """
101 scene = context.scene
102 pg = scene.pdt_pg
103 bpy.ops.view3d.view_orbit(angle=(pg.vrotangle * pi / 180), type="ORBITRIGHT")
104 return {"FINISHED"}
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.
118 Note:
119 Uses pg.vrotangle scene variable
120 Orbits view up about its horizontal axis
122 Args:
123 context: Blender bpy.context instance.
125 Returns:
126 Status Set.
129 scene = context.scene
130 pg = scene.pdt_pg
131 bpy.ops.view3d.view_orbit(angle=(pg.vrotangle * pi / 180), type="ORBITUP")
132 return {"FINISHED"}
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.
146 Note:
147 Uses pg.vrotangle scene variable
148 Orbits view down about its horizontal axis
150 Args:
151 context: Blender bpy.context instance.
153 Returns:
154 Status Set.
157 scene = context.scene
158 pg = scene.pdt_pg
159 bpy.ops.view3d.view_orbit(angle=(pg.vrotangle * pi / 180), type="ORBITDOWN")
160 return {"FINISHED"}
163 class PDT_OT_ViewRoll(Operator):
164 """Roll View."""
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.
174 Note:
175 Uses pg.vrotangle scene variable
176 Rolls view about its normal axis
178 Args:
179 context: Blender bpy.context instance.
181 Returns:
182 Status Set.
185 scene = context.scene
186 pg = scene.pdt_pg
187 bpy.ops.view3d.view_roll(angle=(pg.vrotangle * pi / 180), type="ANGLE")
188 return {"FINISHED"}
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.
202 Note:
203 Set view orientation to Orthographic Isometric
205 Args:
206 context: Blender bpy.context instance.
208 Returns:
209 Status Set.
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"
215 return {"FINISHED"}
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.
229 Args:
230 context: Blender bpy.context instance.
232 Returns:
233 Status Set.
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)
258 view.update()
259 debug(f"view_distance AFTER reset: {view.view_distance}")
260 debug(f"view_location AFTER reset: {view.view_location}")
261 else:
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
266 view.update()
267 debug(f"view_matrix AFTER reset:\n{view.view_matrix}")
269 return {"FINISHED"}