Merge branch 'main' into connect_bones
This commit is contained in:
@@ -33,6 +33,11 @@ def register():
|
|||||||
#finally register properties that may use some classes.
|
#finally register properties that may use some classes.
|
||||||
core.register.register_properties()
|
core.register.register_properties()
|
||||||
|
|
||||||
|
from functions.mesh_tools import AvatarToolkit_OT_ApplyShapeKey
|
||||||
|
|
||||||
|
bpy.types.MESH_MT_shape_key_context_menu.append((lambda self, context: self.layout.separator()))
|
||||||
|
bpy.types.MESH_MT_shape_key_context_menu.append((lambda self, context: self.layout.operator(AvatarToolkit_OT_ApplyShapeKey.bl_idname, icon="KEY_HLT")))
|
||||||
|
|
||||||
def unregister():
|
def unregister():
|
||||||
print("Unregistering Avatar Toolkit")
|
print("Unregistering Avatar Toolkit")
|
||||||
# Unregister the UI classes
|
# Unregister the UI classes
|
||||||
|
|||||||
@@ -144,6 +144,36 @@ def select_current_armature(context: Context) -> bool:
|
|||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def apply_shapekey_to_basis(context: bpy.types.Context, obj: bpy.types.Object, shape_key_name: str, delete_old: bool = False) -> bool:
|
||||||
|
if shape_key_name not in obj.data.shape_keys.key_blocks:
|
||||||
|
return False
|
||||||
|
shapekeynum = obj.data.shape_keys.key_blocks.find(shape_key_name)
|
||||||
|
|
||||||
|
bpy.ops.object.mode_set(mode="EDIT")
|
||||||
|
|
||||||
|
bpy.ops.mesh.select_all(action='SELECT')
|
||||||
|
|
||||||
|
|
||||||
|
obj.active_shape_key_index = 0
|
||||||
|
bpy.ops.mesh.blend_from_shape(shape = shape_key_name, add=True, blend=1)
|
||||||
|
obj.active_shape_key_index = shapekeynum
|
||||||
|
bpy.ops.mesh.select_all(action='SELECT')
|
||||||
|
bpy.ops.mesh.blend_from_shape(shape = shape_key_name, add=True, blend=-2)
|
||||||
|
|
||||||
|
|
||||||
|
bpy.ops.mesh.select_all(action='DESELECT')
|
||||||
|
|
||||||
|
bpy.ops.object.mode_set(mode="OBJECT")
|
||||||
|
print("blended!")
|
||||||
|
|
||||||
|
if delete_old:
|
||||||
|
obj.active_shape_key_index = shapekeynum
|
||||||
|
bpy.ops.object.shape_key_remove(all=False)
|
||||||
|
else:
|
||||||
|
mesh: bpy.types.Mesh = obj.data
|
||||||
|
mesh.shape_keys.key_blocks[shape_key_name].name = shape_key_name + "_reversed"
|
||||||
|
return True
|
||||||
|
|
||||||
def apply_pose_as_rest(context: Context, armature_obj: bpy.types.Object, meshes: list[bpy.types.Object]) -> bool:
|
def apply_pose_as_rest(context: Context, armature_obj: bpy.types.Object, meshes: list[bpy.types.Object]) -> bool:
|
||||||
for obj in meshes:
|
for obj in meshes:
|
||||||
mesh_data: Mesh = obj.data
|
mesh_data: Mesh = obj.data
|
||||||
|
|||||||
@@ -63,6 +63,11 @@ def register() -> None:
|
|||||||
min=0.0,
|
min=0.0,
|
||||||
max=2.0
|
max=2.0
|
||||||
)))
|
)))
|
||||||
|
register_property((bpy.types.Scene, "merge_twist_bones", bpy.props.BoolProperty(
|
||||||
|
name=t("Tools.merge_twist_bones.label"),
|
||||||
|
description=t("Tools.merge_twist_bones.desc"),
|
||||||
|
default=True
|
||||||
|
)))
|
||||||
|
|
||||||
register_property((bpy.types.Scene, "selected_armature", bpy.props.EnumProperty(
|
register_property((bpy.types.Scene, "selected_armature", bpy.props.EnumProperty(
|
||||||
items=get_armatures,
|
items=get_armatures,
|
||||||
|
|||||||
+27
-2
@@ -1,7 +1,7 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
import bpy
|
import bpy
|
||||||
from bpy.types import Context
|
from bpy.types import Context
|
||||||
from ..core.common import get_selected_armature, get_all_meshes, is_valid_armature
|
from ..core.common import get_selected_armature, get_all_meshes, is_valid_armature, apply_shapekey_to_basis, has_shapekeys
|
||||||
from ..functions.translations import t
|
from ..functions.translations import t
|
||||||
from ..core.register import register_wrap
|
from ..core.register import register_wrap
|
||||||
|
|
||||||
@@ -53,4 +53,29 @@ class AvatarToolkit_OT_RemoveUnusedShapekeys(bpy.types.Operator):
|
|||||||
for kb_name in to_delete:
|
for kb_name in to_delete:
|
||||||
if ("-" in kb_name) or ("=" in kb_name) or ("~" in kb_name): #don't delete category names. - @989onan
|
if ("-" in kb_name) or ("=" in kb_name) or ("~" in kb_name): #don't delete category names. - @989onan
|
||||||
continue
|
continue
|
||||||
ob.shape_key_remove(ob.data.shape_keys.key_blocks[kb_name])
|
ob.shape_key_remove(ob.data.shape_keys.key_blocks[kb_name])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@register_wrap
|
||||||
|
class AvatarToolkit_OT_ApplyShapeKey(bpy.types.Operator):
|
||||||
|
bl_idname = "avatar_toolkit.apply_shape_key"
|
||||||
|
bl_label = t("Tools.apply_shape_key.label")
|
||||||
|
bl_description = t("Tools.apply_shape_key.desc")
|
||||||
|
bl_options = {'REGISTER', 'UNDO'}
|
||||||
|
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def poll(cls, context: Context) -> bool:
|
||||||
|
armature = get_selected_armature(context)
|
||||||
|
return armature is not None and is_valid_armature(armature) and (len(get_all_meshes(context)) > 0) and (context.mode == "OBJECT") and context.view_layer.objects.active is not None and has_shapekeys(context.view_layer.objects.active)
|
||||||
|
|
||||||
|
def execute(self, context: Context) -> set[str]:
|
||||||
|
obj: bpy.types.Object = context.view_layer.objects.active
|
||||||
|
|
||||||
|
|
||||||
|
if (apply_shapekey_to_basis(context,obj,obj.active_shape_key.name,False)):
|
||||||
|
return {'FINISHED'}
|
||||||
|
else:
|
||||||
|
self.report({'ERROR'}, t("Tools.apply_shape_key.error"))
|
||||||
|
return {'FINISHED'}
|
||||||
@@ -0,0 +1,184 @@
|
|||||||
|
# This code is heavily based on the Rigify-Move-DEF by NyankoNyan (https://github.com/NyankoNyan/Rigify-Move-DEF), which is licensed under the MIT License. We just heavily improve the code and add some new features.
|
||||||
|
|
||||||
|
import bpy
|
||||||
|
from ..core.register import register_wrap
|
||||||
|
from ..core.common import get_selected_armature, is_valid_armature
|
||||||
|
from ..functions.translations import t
|
||||||
|
from bpy.types import Operator, Context
|
||||||
|
|
||||||
|
import bpy
|
||||||
|
from ..core.register import register_wrap
|
||||||
|
from ..core.common import get_selected_armature, is_valid_armature
|
||||||
|
from ..functions.translations import t
|
||||||
|
from bpy.types import Operator, Context
|
||||||
|
|
||||||
|
@register_wrap
|
||||||
|
class AvatarToolKit_OT_ConvertRigifyToUnity(Operator):
|
||||||
|
bl_idname = "avatar_toolkit.convert_rigify_to_unity"
|
||||||
|
bl_label = t("Tools.convert_rigify_to_unity.label")
|
||||||
|
bl_description = t("Tools.convert_rigify_to_unity.desc")
|
||||||
|
bl_options = {'REGISTER', 'UNDO'}
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def poll(cls, context: Context) -> bool:
|
||||||
|
armature = get_selected_armature(context)
|
||||||
|
return armature is not None and is_valid_armature(armature) and "DEF-spine" in armature.data.bones
|
||||||
|
|
||||||
|
def execute(self, context: Context) -> set[str]:
|
||||||
|
armature = get_selected_armature(context)
|
||||||
|
if not armature:
|
||||||
|
self.report({'ERROR'}, t("Tools.no_armature_selected"))
|
||||||
|
return {'CANCELLED'}
|
||||||
|
|
||||||
|
self.move_def_bones(armature)
|
||||||
|
self.rename_bones_for_unity(armature)
|
||||||
|
if context.scene.merge_twist_bones:
|
||||||
|
self.handle_twist_bones(armature)
|
||||||
|
self.report({'INFO'}, t("Tools.convert_rigify_to_unity.success"))
|
||||||
|
return {'FINISHED'}
|
||||||
|
|
||||||
|
def move_def_bones(self, armature):
|
||||||
|
remap = self.get_org_remap(armature)
|
||||||
|
remap.update(self.get_special_remap())
|
||||||
|
|
||||||
|
remove_bones_in_chain = [
|
||||||
|
'DEF-upper_arm.L.001', 'DEF-forearm.L.001',
|
||||||
|
'DEF-upper_arm.R.001', 'DEF-forearm.R.001',
|
||||||
|
'DEF-thigh.L.001', 'DEF-shin.L.001',
|
||||||
|
'DEF-thigh.R.001', 'DEF-shin.R.001'
|
||||||
|
]
|
||||||
|
|
||||||
|
transform_copies = self.get_transform_copies(armature)
|
||||||
|
|
||||||
|
# Add missing constraints
|
||||||
|
bpy.ops.object.mode_set(mode='POSE')
|
||||||
|
for bone_name in transform_copies:
|
||||||
|
bone = armature.pose.bones[bone_name]
|
||||||
|
org_name = 'ORG-' + self.get_proto_name(bone_name)
|
||||||
|
if org_name in armature.pose.bones:
|
||||||
|
constraint = bone.constraints.new('COPY_TRANSFORMS')
|
||||||
|
constraint.target = armature
|
||||||
|
constraint.subtarget = org_name
|
||||||
|
constr_count = len(bone.constraints)
|
||||||
|
if constr_count > 1:
|
||||||
|
bone.constraints.move(constr_count-1, 0)
|
||||||
|
|
||||||
|
# Apply new parents
|
||||||
|
bpy.ops.object.mode_set(mode='EDIT')
|
||||||
|
for remap_key in remap:
|
||||||
|
if remap_key in armature.data.edit_bones and remap[remap_key] in armature.data.edit_bones:
|
||||||
|
armature.data.edit_bones[remap_key].parent = armature.data.edit_bones[remap[remap_key]]
|
||||||
|
|
||||||
|
# Remove extra bones in chains
|
||||||
|
bpy.ops.object.mode_set(mode='OBJECT')
|
||||||
|
for bone_name in remove_bones_in_chain:
|
||||||
|
if bone_name in armature.data.bones:
|
||||||
|
armature.data.bones[bone_name].use_deform = False
|
||||||
|
|
||||||
|
bpy.ops.object.mode_set(mode='EDIT')
|
||||||
|
for bone_name in remove_bones_in_chain:
|
||||||
|
if bone_name in armature.data.bones:
|
||||||
|
remove_bone = armature.data.edit_bones[bone_name]
|
||||||
|
parent_bone = remove_bone.parent
|
||||||
|
parent_bone.tail = remove_bone.tail
|
||||||
|
retarget_bones = list(remove_bone.children)
|
||||||
|
for bone in retarget_bones:
|
||||||
|
bone.parent = parent_bone
|
||||||
|
armature.data.edit_bones.remove(remove_bone)
|
||||||
|
|
||||||
|
def rename_bones_for_unity(self, armature):
|
||||||
|
unity_bone_names = {
|
||||||
|
"DEF-spine": "Hips",
|
||||||
|
"DEF-spine.001": "Spine",
|
||||||
|
"DEF-spine.002": "Chest",
|
||||||
|
"DEF-spine.003": "UpperChest",
|
||||||
|
"DEF-neck": "Neck",
|
||||||
|
"DEF-head": "Head",
|
||||||
|
"DEF-shoulder.L": "LeftShoulder",
|
||||||
|
"DEF-upper_arm.L": "LeftUpperArm",
|
||||||
|
"DEF-forearm.L": "LeftLowerArm",
|
||||||
|
"DEF-hand.L": "LeftHand",
|
||||||
|
"DEF-shoulder.R": "RightShoulder",
|
||||||
|
"DEF-upper_arm.R": "RightUpperArm",
|
||||||
|
"DEF-forearm.R": "RightLowerArm",
|
||||||
|
"DEF-hand.R": "RightHand",
|
||||||
|
"DEF-thigh.L": "LeftUpperLeg",
|
||||||
|
"DEF-shin.L": "LeftLowerLeg",
|
||||||
|
"DEF-foot.L": "LeftFoot",
|
||||||
|
"DEF-toe.L": "LeftToes",
|
||||||
|
"DEF-thigh.R": "RightUpperLeg",
|
||||||
|
"DEF-shin.R": "RightLowerLeg",
|
||||||
|
"DEF-foot.R": "RightFoot",
|
||||||
|
"DEF-toe.R": "RightToes"
|
||||||
|
}
|
||||||
|
|
||||||
|
for old_name, new_name in unity_bone_names.items():
|
||||||
|
bone = armature.pose.bones.get(old_name)
|
||||||
|
if bone:
|
||||||
|
bone.name = new_name
|
||||||
|
|
||||||
|
def handle_twist_bones(self, armature):
|
||||||
|
twist_bones = [
|
||||||
|
("DEF-upper_arm_twist.L", "DEF-upper_arm.L"),
|
||||||
|
("DEF-upper_arm_twist.R", "DEF-upper_arm.R"),
|
||||||
|
("DEF-forearm_twist.L", "DEF-forearm.L"),
|
||||||
|
("DEF-forearm_twist.R", "DEF-forearm.R"),
|
||||||
|
("DEF-thigh_twist.L", "DEF-thigh.L"),
|
||||||
|
("DEF-thigh_twist.R", "DEF-thigh.R")
|
||||||
|
]
|
||||||
|
|
||||||
|
bpy.ops.object.mode_set(mode='EDIT')
|
||||||
|
for twist_bone, parent_bone in twist_bones:
|
||||||
|
if twist_bone in armature.data.edit_bones and parent_bone in armature.data.edit_bones:
|
||||||
|
twist = armature.data.edit_bones[twist_bone]
|
||||||
|
parent = armature.data.edit_bones[parent_bone]
|
||||||
|
parent.tail = twist.tail
|
||||||
|
for child in twist.children:
|
||||||
|
child.parent = parent
|
||||||
|
armature.data.edit_bones.remove(twist)
|
||||||
|
|
||||||
|
bpy.ops.object.mode_set(mode='OBJECT')
|
||||||
|
|
||||||
|
def get_org_remap(self, armature):
|
||||||
|
remap = {}
|
||||||
|
for bone in armature.data.bones:
|
||||||
|
if self.is_def_bone(bone.name):
|
||||||
|
name = self.get_proto_name(bone.name)
|
||||||
|
parent = bone.parent
|
||||||
|
while parent:
|
||||||
|
parent_name = self.get_proto_name(parent.name)
|
||||||
|
if parent_name != name:
|
||||||
|
if ('DEF-' + parent_name) in armature.data.bones:
|
||||||
|
remap[bone.name] = 'DEF-' + parent_name
|
||||||
|
break
|
||||||
|
parent = parent.parent
|
||||||
|
return remap
|
||||||
|
|
||||||
|
def get_special_remap(self):
|
||||||
|
return {
|
||||||
|
'DEF-thigh.L': 'DEF-pelvis.L',
|
||||||
|
'DEF-thigh.R': 'DEF-pelvis.R',
|
||||||
|
'DEF-upper_arm.L': 'DEF-shoulder.L',
|
||||||
|
'DEF-upper_arm.R': 'DEF-shoulder.R',
|
||||||
|
}
|
||||||
|
|
||||||
|
def get_transform_copies(self, armature):
|
||||||
|
result = []
|
||||||
|
for bone in armature.pose.bones:
|
||||||
|
if self.is_def_bone(bone.name) and not self.has_transform_copies(bone):
|
||||||
|
result.append(bone.name)
|
||||||
|
return result
|
||||||
|
|
||||||
|
def has_transform_copies(self, bone):
|
||||||
|
return any(constraint.type == 'COPY_TRANSFORMS' for constraint in bone.constraints)
|
||||||
|
|
||||||
|
def is_def_bone(self, bone_name):
|
||||||
|
return bone_name.startswith('DEF-')
|
||||||
|
|
||||||
|
def is_org_bone(self, bone_name):
|
||||||
|
return bone_name.startswith('ORG-')
|
||||||
|
|
||||||
|
def get_proto_name(self, bone_name):
|
||||||
|
if self.is_def_bone(bone_name) or self.is_org_bone(bone_name):
|
||||||
|
return bone_name[4:]
|
||||||
|
return bone_name
|
||||||
@@ -152,6 +152,9 @@
|
|||||||
"Tools.remove_unused_shapekeys.tolerance.desc": "Min movement for position on any coordinate\n for any vertex for a shapekey to be kept.",
|
"Tools.remove_unused_shapekeys.tolerance.desc": "Min movement for position on any coordinate\n for any vertex for a shapekey to be kept.",
|
||||||
"Tools.remove_unused_shapekeys.desc": "Remove shapekeys that don't move anything.\nDoesn't get rid of category shapekeys.\n(ex: has \"~\", \"-\", or \"=\" in the name.)",
|
"Tools.remove_unused_shapekeys.desc": "Remove shapekeys that don't move anything.\nDoesn't get rid of category shapekeys.\n(ex: has \"~\", \"-\", or \"=\" in the name.)",
|
||||||
"Tools.remove_unused_shapekeys.tolerance.label": "Position Tolerance",
|
"Tools.remove_unused_shapekeys.tolerance.label": "Position Tolerance",
|
||||||
|
"Tools.apply_shape_key.label": "Apply Shapekey to Basis",
|
||||||
|
"Tools.apply_shape_key.desc": "Apply the selected shapekey to the basis, making it default on.",
|
||||||
|
"Tools.apply_shape_key.error": "The shape keys were not merged for some reason!",
|
||||||
"Tools.remove_zero_weight_bones.success": "Zero weight bones removed successfully",
|
"Tools.remove_zero_weight_bones.success": "Zero weight bones removed successfully",
|
||||||
"Tools.remove_zero_weight_bones.label": "Remove Zero Weight Bones",
|
"Tools.remove_zero_weight_bones.label": "Remove Zero Weight Bones",
|
||||||
"Tools.remove_zero_weight_bones.desc": "Remove bones from the armature that have weights less than threshold.",
|
"Tools.remove_zero_weight_bones.desc": "Remove bones from the armature that have weights less than threshold.",
|
||||||
@@ -171,6 +174,9 @@
|
|||||||
"Tools.connect_bones.min_distance.label": "Minimum Distance",
|
"Tools.connect_bones.min_distance.label": "Minimum Distance",
|
||||||
"Tools.connect_bones.min_distance.desc": "Minimum distance between bones to connect them",
|
"Tools.connect_bones.min_distance.desc": "Minimum distance between bones to connect them",
|
||||||
"Tools.connect_bones.success": "Connected {bones_connected} bones successfully",
|
"Tools.connect_bones.success": "Connected {bones_connected} bones successfully",
|
||||||
|
"Tools.convert_rigify_to_unity.label": "Convert Rigify to Unity",
|
||||||
|
"Tools.convert_rigify_to_unity.desc": "Prepare Rigify armature for use in Unity",
|
||||||
|
"Tools.convert_rigify_to_unity.success": "Rigify armature successfully converted for Unity",
|
||||||
"MergeArmatures.select_armature": "Please select an armature",
|
"MergeArmatures.select_armature": "Please select an armature",
|
||||||
"MergeArmatures.title.label": "Merge Armatures:",
|
"MergeArmatures.title.label": "Merge Armatures:",
|
||||||
"MergeArmatures.label": "Merge Armatures",
|
"MergeArmatures.label": "Merge Armatures",
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ from ..functions.mesh_tools import AvatarToolkit_OT_RemoveUnusedShapekeys
|
|||||||
from ..functions.seperate_by import AvatarToolKit_OT_SeparateByMaterials, AvatarToolKit_OT_SeparateByLooseParts
|
from ..functions.seperate_by import AvatarToolKit_OT_SeparateByMaterials, AvatarToolKit_OT_SeparateByLooseParts
|
||||||
from ..functions.additional_tools import AvatarToolKit_OT_ApplyTransforms, AvatarToolKit_OT_ConnectBones
|
from ..functions.additional_tools import AvatarToolKit_OT_ApplyTransforms, AvatarToolKit_OT_ConnectBones
|
||||||
from ..functions.armature_modifying import AvatarToolkit_OT_RemoveZeroWeightBones, AvatarToolkit_OT_MergeBonesToActive, AvatarToolkit_OT_MergeBonesToParents
|
from ..functions.armature_modifying import AvatarToolkit_OT_RemoveZeroWeightBones, AvatarToolkit_OT_MergeBonesToActive, AvatarToolkit_OT_MergeBonesToParents
|
||||||
|
from ..functions.rigify_functions import AvatarToolKit_OT_ConvertRigifyToUnity
|
||||||
|
|
||||||
@register_wrap
|
@register_wrap
|
||||||
class AvatarToolkit_PT_ToolsPanel(bpy.types.Panel):
|
class AvatarToolkit_PT_ToolsPanel(bpy.types.Panel):
|
||||||
@@ -49,5 +50,8 @@ class AvatarToolkit_PT_ToolsPanel(bpy.types.Panel):
|
|||||||
row.operator(AvatarToolkit_OT_MergeBonesToParents.bl_idname, text=t("Tools.merge_bones_to_parents.label"), icon='BONE_DATA')
|
row.operator(AvatarToolkit_OT_MergeBonesToParents.bl_idname, text=t("Tools.merge_bones_to_parents.label"), icon='BONE_DATA')
|
||||||
row = layout.row(align=True)
|
row = layout.row(align=True)
|
||||||
row.operator(AvatarToolKit_OT_ConnectBones.bl_idname, text=t("Tools.connect_bones.label"), icon='BONE_DATA')
|
row.operator(AvatarToolKit_OT_ConnectBones.bl_idname, text=t("Tools.connect_bones.label"), icon='BONE_DATA')
|
||||||
|
row.operator(AvatarToolKit_OT_ConvertRigifyToUnity.bl_idname, text=t("Tools.convert_rigify_to_unity.label"), icon='ARMATURE_DATA')
|
||||||
|
row = layout.row()
|
||||||
|
row.prop(context.scene, "merge_twist_bones")
|
||||||
else:
|
else:
|
||||||
layout.label(text=t("Tools.select_armature"), icon='ERROR')
|
layout.label(text=t("Tools.select_armature"), icon='ERROR')
|
||||||
|
|||||||
Reference in New Issue
Block a user