-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalign_view_to_cursor.py
More file actions
71 lines (53 loc) · 2.22 KB
/
align_view_to_cursor.py
File metadata and controls
71 lines (53 loc) · 2.22 KB
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
"""
Align View to Cursor - Aligns the 3D viewport to the cursor's orientation.
This module provides functionality to orient the 3D viewport to match
the position and rotation of the 3D cursor, effectively letting the user
"look through" the cursor.
"""
import bpy
import mathutils
def align_view_to_cursor(context):
"""Align the 3D viewport to match the cursor's orientation.
Calculates a view direction from the cursor rotation and repositions
the viewport to look from the cursor's perspective.
Args:
context: Current Blender context
"""
cursor_location = context.scene.cursor.location
cursor_rotation = context.scene.cursor.rotation_euler
# Calculate the view direction vector from cursor rotation
view_direction = cursor_rotation.to_matrix() @ mathutils.Vector((0, 0, -1))
view_location = cursor_location + view_direction
# Build a view rotation matrix with Y-up orientation
view_rotation = view_direction.to_track_quat('-Z', 'Y').to_matrix().to_4x4()
view_rotation.translation = view_location
# Apply to the first 3D viewport found on screen
for area in context.screen.areas:
if area.type == 'VIEW_3D':
for region in area.regions:
if region.type == 'WINDOW':
region_3d = area.spaces.active.region_3d
region_3d.view_matrix = view_rotation.inverted()
region_3d.view_location = view_location
bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)
break
break
class VIEW3D_OT_align_view_to_cursor(bpy.types.Operator):
"""Align the 3D viewport to the cursor's position and orientation"""
bl_idname = "view3d.align_view_to_cursor"
bl_label = "Align View to Cursor"
bl_description = "Orient the 3D viewport to look from the 3D cursor's perspective"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
align_view_to_cursor(context)
return {'FINISHED'}
# Registration
classes = (
VIEW3D_OT_align_view_to_cursor,
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
def unregister():
for cls in reversed(classes):
bpy.utils.unregister_class(cls)