/
utility_function.py
159 lines (96 loc) · 3.21 KB
/
utility_function.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import bpy
import numpy as np
import bmesh
from mathutils import Matrix, Vector
import re
import os
script_file = os.path.realpath(__file__)
addon_directory = os.path.dirname(script_file)
addon_name = os.path.basename(addon_directory)
def get_addon_preferences(context):
addon_preferences = context.preferences.addons[addon_name].preferences
return addon_preferences
def find_center_point(obj):
pts = [(obj.matrix_world @ v.co) for v in obj.data.vertices]
center_pt = np.average(pts, 0)
return center_pt
def find_center_point_from_verts(obj, verts):
pts = [(obj.matrix_world @ v.co) for v in verts]
center_pt = np.average(pts, 0)
return center_pt
def find_lowest_point(obj, center=False):
lowest_pt = min([(obj.matrix_world @ v.co).z for v in obj.data.vertices])
lowest_z = min([(obj.matrix_world @ v.co).z for v in obj.data.vertices])
lowest_pts = [(obj.matrix_world @ v.co) for v in obj.data.vertices if (obj.matrix_world @ v.co).z == lowest_z]
lowest_pt = np.average(lowest_pts, 0)
if center == True:
lowest_pt_center = find_center_point(obj)
lowest_pt_center[2] = lowest_pt[2]
return lowest_pt_center
else:
return lowest_pt
def create_empty(name, input):
o = bpy.data.objects.new( name, None )
bpy.context.scene.collection.objects.link( o )
o.empty_display_size = 2
o.empty_display_type = 'PLAIN_AXES'
try:
o.matrix_world = input.matrix_world
return o
except:
try:
o.matrix_world = input
return o
except:
try:
o.location = input
return o
except:
print("Wrong Type")
def moveOrigin(obj, location):
currentMode = bpy.context.mode
if currentMode == "EDIT_MESH":
bpy.ops.object.mode_set(mode='OBJECT')
location = Vector(location)
ob = obj
cursor_world_loc = location
cursor_local_loc = ob.matrix_world.inverted() @ cursor_world_loc
mat = Matrix.Translation(-cursor_local_loc)
me = ob.data
me.transform(mat)
me.update()
ob.matrix_world.translation = cursor_world_loc
if currentMode == "EDIT_MESH":
bpy.ops.object.mode_set(mode='EDIT')
def updateUI():
for screen in bpy.data.screens:
for area in screen.areas:
area.tag_redraw()
def create_armature(name, input):
data = bpy.data.armatures.new(name)
o = bpy.data.objects.new( name, data)
bpy.context.scene.collection.objects.link( o )
# o.empty_display_size = 2
# o.empty_display_type = 'PLAIN_AXES'
try:
o.matrix_world = input.matrix_world
return o
except:
try:
o.matrix_world = input
return o
except:
try:
o.location = input
return o
except:
print("Wrong Type")
def increment_number(n, padding=True):
number = str(int(n) + 1)
if padding:
number = number.zfill(len(n))
return number
def increment_string_number(n, pad):
# final = re.sub('\d+', lambda x: str(int(x.group(0))+1),n)
final = re.sub('\d+', lambda x: increment_number(x.group(0), pad), n)
return final