-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalign_object_to_cursor.py
More file actions
64 lines (49 loc) · 1.75 KB
/
align_object_to_cursor.py
File metadata and controls
64 lines (49 loc) · 1.75 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
"""
Align Object to Cursor - Aligns selected objects to the 3D cursor.
This module provides functionality to align one or more selected objects
to the position and rotation of the 3D cursor.
"""
import bpy
from typing import Set, Optional
def align_object_to_cursor(objects: Set[bpy.types.Object]) -> Optional[str]:
"""Align selected objects to the 3D cursor's location and rotation.
Args:
objects: Set of objects to align
Returns:
Error message string if operation fails, None otherwise
"""
if not objects:
return "No objects selected"
cursor = bpy.context.scene.cursor
for obj in objects:
# Skip non-transformable objects
if not hasattr(obj, "location") or not hasattr(obj, "rotation_euler"):
continue
# Apply transformation
obj.location = cursor.location.copy()
obj.rotation_euler = cursor.rotation_euler.copy()
return None
class OBJECT_OT_align_to_cursor(bpy.types.Operator):
"""Align selected objects to 3D cursor"""
bl_idname = "object.align_to_cursor"
bl_label = "Align Objects to Cursor"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return context.mode == 'OBJECT' and len(context.selected_objects) > 0
def execute(self, context):
error = align_object_to_cursor(set(context.selected_objects))
if error:
self.report({'ERROR'}, error)
return {'CANCELLED'}
return {'FINISHED'}
# Registration
classes = (
OBJECT_OT_align_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)