This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Utility_Functions.py
595 lines (367 loc) · 14.8 KB
/
Utility_Functions.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
import bpy
import os
import pathlib
import numpy
import mathutils
import platform
import subprocess
import math
script_file = os.path.realpath(__file__)
addon_directory = os.path.dirname(script_file)
addon_name = os.path.basename(addon_directory)
SEPARATOR_List = [".", "_", "-"]
Side_List = {"l": "r", "L":"R", "left": "right", "Left":"Right", "LEFT": "RIGHT"}
FRONT_Side_Separator_List = {}
BACK_Side_Separator_List = {}
for key, value in Side_List.items():
for separator in SEPARATOR_List:
FRONT_Side_Separator_List[key+separator] = value+separator
BACK_Side_Separator_List[separator+key] = separator+value
def Parent_Counter(self, Bone):
Checker = Bone.parent
if Checker:
self.Counter += 1
Parent_Counter(self, Checker)
def Find_Chain_Root(Chain_Length, Bone):
bone_chain = []
bone_chain.append(Bone)
parent_finder = Bone.parent
if parent_finder:
bone_chain.append(parent_finder)
Loop_Amount = Chain_Length-2
if Loop_Amount > 0:
for count in range(Loop_Amount):
if parent_finder:
parent_finder = parent_finder.parent
if parent_finder:
bone_chain.append(parent_finder)
else:
bone_chain.append(None)
return bone_chain
def Align_Bone_Roll(object, bone, target):
bone = object.data.edit_bones.get(bone.name)
bpy.ops.armature.select_all(action='DESELECT')
cursor_location = bpy.context.scene.cursor.location.copy()
bpy.context.scene.cursor.location = object.matrix_world @ target
bone.select = True
bpy.ops.armature.calculate_roll(type='CURSOR')
bone.roll -= math.radians(90)
bpy.context.scene.cursor.location = cursor_location
bpy.ops.armature.select_all(action='DESELECT')
def Align_Bones_Roll(object, bones, target):
bone_selection = [select_bone for select_bone in object.data.edit_bones if select_bone.select]
for bone in bones:
Align_Bone_Roll(object, bone, target)
bpy.ops.armature.select_all(action='DESELECT')
for select_bone in bone_selection:
select_bone.select = True
def Get_Pole_Angle(object, bone, target):
original_roll = bone.roll
Align_Bone_Roll(object, bone, target)
bpy.ops.armature.select_all(action='DESELECT')
adjusted_roll = bone.roll
bone.roll = original_roll
pole_angle = original_roll - adjusted_roll
if pole_angle > math.radians(180):
pole_angle = pole_angle - math.radians(360)
return pole_angle
def open_file(path):
if platform.system() == "Windows":
os.startfile(path)
elif platform.system() == "Darwin":
subprocess.Popen(["open", path])
else:
subprocess.Popen(["xdg-open", path])
class Side_Flipper:
def __init__(self, left, right):
self.left = left
self.right = right
def flip_name(self, name):
flipped_name = None
if self.left in name:
flipped_name = name.replace(self.left, self.right)
elif self.right in name:
flipped_name = name.replace(self.right, self.left)
return flipped_name
def get_flipped_bone(self, bones, bone):
flipped_bone = None
if bones:
if bone:
flipped_bone_name = self.flip_name(bone.name)
if flipped_bone_name:
flipped_bone = bones.get(flipped_bone_name)
return flipped_bone
def curve_to_mesh(object, resolution=None):
offset = object.data.offset
extrude = object.data.extrude
taper_object = object.data.taper_object
taper_radius_mode = object.data.taper_radius_mode
bevel_mode = object.data.bevel_mode
bevel_depth = object.data.bevel_depth
use_fill_caps = object.data.use_fill_caps
resolution_u = object.data.resolution_u
object.data.offset = 0
object.data.extrude = 0
object.data.taper_object = None
object.data.taper_radius_mode = "OVERRIDE"
object.data.bevel_mode = "ROUND"
object.data.bevel_depth = 0
object.data.use_fill_caps = False
if resolution:
object.data.resolution_u = resolution
deg = bpy.context.evaluated_depsgraph_get()
me = bpy.data.meshes.new_from_object(object.evaluated_get(deg), depsgraph=deg)
object.data.offset = offset
object.data.extrude = extrude
object.data.taper_object = taper_object
object.data.taper_radius_mode = taper_radius_mode
object.data.bevel_mode = bevel_mode
object.data.bevel_depth = bevel_depth
object.data.use_fill_caps = use_fill_caps
object.data.resolution_u = resolution_u
new_obj = bpy.data.objects.new(object.name + "_mesh", me)
bpy.context.collection.objects.link(new_obj)
new_obj.matrix_world = object.matrix_world
return new_obj
def get_addon_preferences():
addon_preferences = bpy.context.preferences.addons[addon_name].preferences
return addon_preferences
def get_addon_name():
return addon_name
def get_addon_directory():
return addon_directory
def update_UI():
for screen in bpy.data.screens:
for area in screen.areas:
area.tag_redraw()
def draw_subpanel(self, boolean, property, label, layout):
if boolean:
ICON = "TRIA_DOWN"
else:
ICON = "TRIA_RIGHT"
row = layout.row(align=True)
row.alignment = "LEFT"
row.prop(self, property, text=label, emboss=False, icon=ICON)
return boolean
def draw_subpanel_bool(source, boolean, property, bool_source, bool_prop, label, layout):
if boolean:
ICON = "TRIA_DOWN"
else:
ICON = "TRIA_RIGHT"
row = layout.row(align=True)
row.alignment = "LEFT"
row.prop(source, property, text="", emboss=False, icon=ICON)
row.prop(bool_source, bool_prop, text="")
row.prop(source, property, text=label, emboss=False, icon=ICON)
return boolean
def get_bounding_box(object):
bbox_corners = [object.matrix_world * mathutils.Vector(corner) for corner in object.bound_box]
return bbox_corners
def midpoint(coordinates, mode):
if len(coordinates) > 0:
if mode == "BOUNDING_BOX":
x= []
y= []
z= []
for coordinate in coordinates:
x.append(coordinate[0])
y.append(coordinate[1])
z.append(coordinate[2])
range_x = (max(x), min(x))
range_y = (max(y), min(y))
range_z = (max(z), min(z))
bounding_box_coordinate = []
for a in range_x:
for b in range_y:
for c in range_z:
bounding_box_coordinate.append((a, b, c))
return mathutils.Vector(numpy.array(bounding_box_coordinate).mean(axis=0))
if mode == "CENTER":
return mathutils.Vector(numpy.array(coordinates).mean(axis=0))
else:
return None
def object_switch_mode(object, mode):
bpy.context.view_layer.update()
Previous_Mode = object.mode
if not object.visible_get():
if not bpy.context.collection.objects.get(object.name):
bpy.context.collection.objects.link(object)
object.hide_viewport = False
object.hide_set(False)
object.hide_select = False
if object.visible_get():
object.select_set(True)
bpy.context.view_layer.objects.active = object
bpy.ops.object.mode_set(mode=mode, toggle=False)
return Previous_Mode
def create_bone(armature, name, head, tail, deform, Flip_Bone = False):
bone = armature.data.edit_bones.new(name)
if Flip_Bone:
bone.head = tail
bone.tail = head
else:
bone.head = head
bone.tail = tail
bone.use_deform = deform
return bone
def get_object_center(object, mode):
if mode == "ORIGIN":
# return object.matrix_world.inverted() @ object.location
return object.matrix_world.inverted() @ object.matrix_world.to_translation()
if mode in ["CENTER", "BOUNDING_BOX"]:
if not object.type in ["MESH","CURVE" , "ARMATURE"]:
# return object.matrix_world.inverted() @ object.location
return object.matrix_world.inverted() @ object.matrix_world.to_translation()
if object.type == "MESH":
# create_lists = [object.matrix_world @ vert.co for vert in object.data.vertices]
create_lists = [vert.co for vert in object.data.vertices]
if object.type == "CURVE":
create_lists = []
for spline in object.data.splines:
for point in spline.points:
# create_lists.append(object.matrix_world @ point.co)
create_lists.append(point.co.xyz)
for bezier_point in spline.bezier_points:
# create_lists.append(object.matrix_world @ bezier_point.co)
create_lists.append(bezier_point.co.xyz)
if object.type == "ARMATURE":
create_lists = []
for bone in object.data.bones:
# create_lists.append(object.matrix_world @ bone.head)
# create_lists.append(object.matrix_world @ bone.tail)
create_lists.append(bone.head)
create_lists.append(bone.tail)
if mode == "CENTER":
return midpoint(create_lists, "CENTER")
if mode == "BOUNDING_BOX":
return midpoint(create_lists, "BOUNDING_BOX")
def Normal_To_Offset(object, location, normal, offset):
mw = object.matrix_world.copy()
o = location
axis_src = normal
axis_dst = mathutils.Vector((0, 0, 1))
matrix_rotate = mw.to_3x3()
matrix_rotate = matrix_rotate @ axis_src.rotation_difference(axis_dst).to_matrix().inverted()
matrix_translation = mathutils.Matrix.Translation(mw @ o)
Normal_Matrix = matrix_translation @ matrix_rotate.to_4x4() @ mathutils.Vector(offset)
Normal_Offset = object.matrix_world.inverted() @ Normal_Matrix
return Normal_Offset
def Average_Normals(Normals):
average_normals = mathutils.Vector(numpy.sum(Normals, axis=0) / len(Normals))
return average_normals
def Add_Weight(object, bone_name, indices):
Vertex_Group = object.vertex_groups.get(bone_name)
if Vertex_Group == None:
Vertex_Group = object.vertex_groups.new( name = bone_name )
Vertex_Group.add(indices, 1.0, 'REPLACE' )
return Vertex_Group
def Add_Armature_Modifier(object, Armature, name="Armature"):
for modifier in object.modifiers:
if modifier.type == "ARMATURE":
if modifier.object == Armature:
return modifier
modifier = object.modifiers.new(type="ARMATURE", name=name)
modifier.object = Armature
return modifier
def Hook_Vertex_Bone(object, armature, vertex_indices, bone_name, name="Hook"):
modifier = object.modifiers.new(type="HOOK", name=name)
modifier.object = armature
modifier.subtarget = bone_name
modifier.vertex_indices_set(vertex_indices)
return modifier
def get_object_indices(object):
if object.type == "MESH":
indices = [vertex.index for vertex in object.data.vertices]
return indices
else:
return None
def check_bone_select(bone, mode):
if mode == "EDIT_ARMATURE":
return bone.select
if mode == "POSE":
return bone.bone.select
def Create_Armature(name):
armature = bpy.data.armatures.new(name)
object = bpy.data.objects.new(name, armature)
bpy.context.collection.objects.link(object)
return object
def Create_Empty(name):
object = bpy.data.objects.new(name, None)
bpy.context.collection.objects.link(object)
return object
def Hook_Vertex_Empty(object, empty, vertex_indices, name="Hook"):
modifier = object.modifiers.new(type="HOOK", name=name)
modifier.object = empty
modifier.vertex_indices_set(vertex_indices)
return modifier
def Normal_To_Orientation(object, location, normal):
mw = object.matrix_world.copy()
o = location
axis_src = normal
axis_dst = mathutils.Vector((0, 0, 1))
matrix_rotate = mw.to_3x3()
matrix_rotate = matrix_rotate @ axis_src.rotation_difference(axis_dst).to_matrix().inverted()
matrix_translation = mathutils.Matrix.Translation(mw @ o)
Normal_Matrix = matrix_translation @ matrix_rotate.to_4x4()
return Normal_Matrix
def append_bone_shape(path):
objects = []
if path != "None":
path = path
section = "/Object/"
directory = path + section
filename = "Widget"
bpy.ops.wm.append(filename=filename, directory=directory)
objects = [object for object in bpy.context.selected_objects]
return objects
def get_widgets_filepath():
addon_dir = pathlib.Path(addon_directory)
widget_file = pathlib.Path("{}/Widgets/Widget.blend".format(addon_dir))
return widget_file
def get_bone_shape_directory():
addon_dir = addon_directory
bone_shapes_directory = os.path.join(addon_dir, "Widgets")
return bone_shapes_directory
preview_collections = {}
def get_bone_shape_catagories():
pcoll = bpy.utils.previews.new()
pcoll.my_previews = ()
preview_collections["main"] = pcoll
bone_shapes_directory = get_bone_shape_directory()
bone_shapes_catagories = {}
for dir in os.listdir(bone_shapes_directory):
catagory_path = os.path.join(bone_shapes_directory, dir)
if os.path.isdir(catagory_path):
bone_shapes = []
for bone_shape_name in os.listdir(catagory_path):
bone_shape_path = os.path.join(catagory_path, bone_shape_name)
if os.path.isfile(bone_shape_path) and bone_shape_path.endswith(".blend"):
thumb = pcoll.load(bone_shape_path, bone_shape_path, "BLEND")
bone_shape = {"name": bone_shape_name, "path": bone_shape_path, "thumb": thumb}
bone_shapes.append(bone_shape)
bone_shapes_catagory = {"name": dir, "path": catagory_path, "bone_shapes": bone_shapes}
bone_shapes_catagories[dir] = bone_shapes_catagory
return bone_shapes_catagories
def Format_String(Format, Dictionary):
for key, item in Dictionary.items():
Format = Format.replace(key, item)
return Format
def subdivide_bone(object, bone, amount):
edit_bones = object.data.edit_bones
twist_bones = []
for a in range(amount):
newbone_name = "TWIST_" + str(a) + "_" + bone.name
newbone = create_bone(object, newbone_name, bone.head, bone.tail, bone.use_deform, Flip_Bone = False)
newbone.roll = bone.roll
vector = bone.vector
newbone.length = newbone.length / amount
newbone.head += bone.vector/amount * a
newbone.tail += bone.vector/amount * a
if len(twist_bones) > 0:
newbone.parent = twist_bones[-1]
twist_bones.append(newbone)
return twist_bones
def get_bone_layers(layer):
layers = [False for layer in range(32)]
layers[layer] = True
return layers