Files
Mabulig-Rigger/addon dev/mabulig-rigger/rigger_functionality.py
T

175 lines
5.3 KiB
Python

import bpy
import math
from . import bones_skel_struct
from . import bones_fingers_struct
from . import bones_toes_struct
from . import bones_web_struct
# mabulig_coll_name = None
# new_amt_obj = None
# matrix_world_inv = None
# Modify Bone Function
def modifybone(bone, head_empt, tail_empt, armature, matrixworldinv):
edit_bone = armature.data.edit_bones.get(bone)
print(edit_bone)
empty_head = bpy.data.objects.get(head_empt)
empty_tail = bpy.data.objects.get(tail_empt)
allowhead = False
allowtail = False
alloweditbone = False
# Fetch world coordinates of the empties
if empty_head:
allowhead = True
# else:
# print(f"Helper '{head_empt}' not found.")
if empty_tail:
allowtail = True
# else:
# print(f"Helper '{tail_empt}' not found.")
if edit_bone:
alloweditbone = True
# else:
# print(f"Bone '{bone}' not found.")
if allowhead and allowtail and alloweditbone:
head_world_pos = empty_head.matrix_world.translation
print(f"Head: {head_empt} - success")
tail_world_pos = empty_tail.matrix_world.translation
print(f"Tail: {tail_empt} - success")
# Multiply the inverse matrix by the world positions using the '@' operator
edit_bone.head = matrixworldinv @ head_world_pos
edit_bone.tail = matrixworldinv @ tail_world_pos
# else:
# print("Error")
def startAutoRig():
mabulig_coll_name = "Mabulig-Rig"
og_amt_obj = bpy.data.objects.get("OG_Armature")
# =========== Duplicate OG armature for future editing ===========
target_coll = bpy.data.collections.get(mabulig_coll_name)
armaturecheck = bpy.data.objects.get("Armature")
if armaturecheck:
print("Armature already exist")
new_amt_obj = armaturecheck
else:
# Duplicate the armature object
new_amt_obj = og_amt_obj.copy()
# Duplicate the underlying armature data (bones, constraints, etc.)
new_amt_obj.data = og_amt_obj.data.copy()
new_amt_obj.name = "Armature"
new_amt_obj.data.name = "Armature"
target_coll.objects.link(new_amt_obj)
new_amt_obj.hide_viewport = False
# Calculate the armature's inverse world matrix
matrix_world_inv = new_amt_obj.matrix_world.inverted()
print(new_amt_obj.name)
# =========== Skeleton Bones Setup ===========
bone_data_list = bones_skel_struct.bones_data
for item in bone_data_list:
b_name = item["bone_name"]
h_helper = item["head_helper"]
t_helper = item["tail_helper"]
modifybone(b_name, h_helper, t_helper, new_amt_obj, matrix_world_inv) # Call Modify Bone Function
# # =========== Belly Bones Setup ===========
# belly_l_collection = bpy.data.collections.get("belly_l")
# belly_r_collection = bpy.data.collections.get("belly_r")
# # For Left Belly
# for obj in belly_l_collection.objects:
# modifybone(obj.name, "belly_base_l", obj.name, new_amt_obj, matrix_world_inv) # Call Modify Bone Function
# # For Right Belly
# for obj in belly_r_collection.objects:
# modifybone(obj.name, "belly_base_r", obj.name, new_amt_obj, matrix_world_inv) # Call Modify Bone Function
# # =========== Gullar Bones Setup ===========
# gular_collection = bpy.data.collections.get("gular")
# for obj in gular_collection.objects:
# modifybone(obj.name, "gular_base", obj.name, new_amt_obj, matrix_world_inv) # Call Modify Bone Function
# # =========== Nostril Bones Setup ===========
# nostril_l_collection = bpy.data.collections.get("nostril_l")
# nostril_r_collection = bpy.data.collections.get("nostril_r")
# # For Left Nostril
# for obj in nostril_l_collection.objects:
# modifybone(obj.name, "nostril_base_l", obj.name, new_amt_obj, matrix_world_inv) # Call Modify Bone Function
# # For Right Nostril
# for obj in nostril_r_collection.objects:
# modifybone(obj.name, "nostril_base_r", obj.name, new_amt_obj, matrix_world_inv) # Call Modify Bone Function
# # =========== Metacarpal and Finger Bones Setup ===========
# bones_fingers_data_list = bones_fingers_struct.fingers_data
# for item in bones_fingers_data_list:
# b_name = item["bone_name"]
# h_helper = item["head_helper"]
# t_helper = item["tail_helper"]
# modifybone(b_name, h_helper, t_helper, new_amt_obj, matrix_world_inv) # Call Modify Bone Function
# # =========== Metatarsus and Toe Bones Setup ===========
# bones_toes_data_list = bones_toes_struct.toes_data
# for item in bones_toes_data_list:
# b_name = item["bone_name"]
# h_helper = item["head_helper"]
# t_helper = item["tail_helper"]
# modifybone(b_name, h_helper, t_helper, new_amt_obj, matrix_world_inv) # Call Modify Bone Function
# # =========== Webbing Bones ===========
# bones_web_data_list = bones_web_struct.webbing_data
# for item in bones_web_data_list:
# b_name = item["bone_name"]
# h_helper = item["head_helper"]
# t_helper = item["tail_helper"]
# modifybone(b_name, h_helper, t_helper, new_amt_obj, matrix_world_inv) # Call Modify Bone Function