-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetOriginToSelected.py
More file actions
82 lines (62 loc) · 2.72 KB
/
Copy pathSetOriginToSelected.py
File metadata and controls
82 lines (62 loc) · 2.72 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
72
73
74
75
76
77
78
79
80
81
82
## Move an object to the center of scene by selected face's center
## Generally used for objects to stand on a flat surface on game design
## by Arda Erdikmen https://github.com/ref-xx
bl_info = {
"name": "Origin to Here",
"description": "Set origin to center of selected vertices",
"author": "Arda Erdikmen",
"version": (1, 0),
"blender": (3, 4, 0),
"location": "View3D > Edit Mode > Right-click menu",
"warning": "", # used for warning icon and text in addons panel
"doc_url": "https://github.com/ref-xx",
"tracker_url": "https://github.com/ref-xx",
"support": "COMMUNITY",
"category": "Mesh",
}
import bpy
import bmesh
import mathutils
from bpy.types import Operator
from bpy_extras.object_utils import world_to_camera_view
class SetOriginToSelected(Operator):
bl_idname = "object.set_origin_to_selected"
bl_label = "Set Origin to Selected"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
obj = bpy.context.object
if obj.mode == 'EDIT':
obj = bpy.context.object
bm = bmesh.from_edit_mesh(obj.data)
selected_verts = [v for v in bm.verts if v.select]
center=None
if selected_verts:
center = mathutils.Vector((0.0, 0.0, 0.0))
for v in selected_verts:
center += v.co
center /= len(selected_verts)
if center!= None:
backup=bpy.context.scene.cursor.location
bpy.context.scene.cursor.location = obj.matrix_world @ center
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
bpy.context.scene.cursor.rotation_euler = (0,0,0)
bpy.ops.object.mode_set(mode='EDIT')
bpy.context.scene.cursor.location= backup
bpy.context.view_layer.update()
else:
self.report({'INFO'},"Cannot set origin. Please select a vertex first.")
return {'FINISHED'}
def menu_func(self, context):
self.layout.operator(SetOriginToSelected.bl_idname)
def register():
bpy.utils.register_class(SetOriginToSelected)
bpy.types.VIEW3D_MT_edit_mesh_context_menu.append(menu_func)
def unregister():
bpy.types.VIEW3D_MT_edit_mesh_context_menu.remove(menu_func)
bpy.utils.unregister_class(SetOriginToSelected)
if __name__ == "__main__":
register()
#bpy.context.scene.cursor.location = (0.0, 0.0, 0.0)
#obj.location = bpy.context.scene.cursor.location
#bpy.ops.view3d.snap_cursor_to_center()