-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
105 lines (80 loc) · 3.37 KB
/
__init__.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
bl_info = {
"name": "Origin to Floor",
'author': 'AssisrMatheus',
'version': (1, 0, 0),
'blender': (2, 80, 0),
'location': 'Object > Origin to Floor',
'description': 'Centers the origin on the floor of the selected object',
"category": "Object",
"tracker_url": "https://github.com/AssisrMatheus/OriginToFloor/issues",
"wiki_url": "https://github.com/AssisrMatheus/OriginToFloor",
}
import bpy
def set_origin_to_floor(shouldClear):
# Get the selected object
obj = bpy.context.active_object
# Stores the current edit mode so we can rollback later
current_mode = obj.mode
# The method used to get the vertices depends on being on object mode
bpy.ops.object.mode_set(mode = 'OBJECT')
# Stores the current pivot point transform setting
current_pivot = bpy.context.scene.tool_settings.transform_pivot_point
# Sets the pivot point transform setting so the origin calculation is exactly at the center
bpy.context.scene.tool_settings.transform_pivot_point = "BOUNDING_BOX_CENTER"
# Puts the origin in the center of the object
bpy.ops.object.origin_set(type = 'ORIGIN_GEOMETRY')
# Resets back to the previous pivot point transform setting
bpy.context.scene.tool_settings.transform_pivot_point = current_pivot
# Grabs all the vertices, then their coordinates, then their Z position
z_verts = [v.co.z for v in obj.data.vertices]
# Grabs the most negative Z position(floor position)
deepest_vert_z = min(z_verts)
# We're going to translate only the origin for now
bpy.context.scene.tool_settings.use_transform_data_origin = True
# Translate the origin only on Z, to the same position as vertex that is closest to the bottom
bpy.ops.transform.translate(value=(0, 0, deepest_vert_z))
# Reset the origin manipulation back
bpy.context.scene.tool_settings.use_transform_data_origin = False
if shouldClear:
bpy.ops.object.location_clear(clear_delta=False)
# Resets the object mode back to what it was previously
bpy.ops.object.mode_set(mode = current_mode)
class OriginToFloor(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.origin_to_floor"
bl_label = "Origin to Floor"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
obj = context.active_object
# Only if there's a selected object
return obj is not None
def execute(self, context):
set_origin_to_floor(False)
return {'FINISHED'}
class OriginToFloorClear(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.origin_to_floor_clear"
bl_label = "Origin to Floor(Clear position)"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
obj = context.active_object
# Only if there's a selected object
return obj is not None
def execute(self, context):
set_origin_to_floor(True)
return {'FINISHED'}
def menu_func(self, context):
layout = self.layout
layout.operator(OriginToFloor.bl_idname)
layout.operator(OriginToFloorClear.bl_idname)
def register():
bpy.utils.register_class(OriginToFloor)
bpy.utils.register_class(OriginToFloorClear)
bpy.types.VIEW3D_MT_object.append(menu_func)
def unregister():
bpy.utils.unregister_class(OriginToFloor)
bpy.utils.unregister_class(OriginToFloorClear)
if __name__ == "__main__":
register()