Further logging and typing, couple of fixes
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import bpy
|
import bpy
|
||||||
from typing import Dict, List, Set, Optional
|
from typing import Dict, List, Set, Optional, Tuple, Any
|
||||||
from bpy.types import Operator, Context, Object, PoseBone, EditBone, Bone, Constraint
|
from bpy.types import Operator, Context, Object, PoseBone, EditBone, Bone, Constraint
|
||||||
from ...core.common import get_active_armature, validate_armature
|
from ...core.common import get_active_armature, validate_armature
|
||||||
from ...core.logging_setup import logger
|
from ...core.logging_setup import logger
|
||||||
@@ -23,81 +23,93 @@ class AvatarToolkit_OT_ConvertRigifyToUnity(Operator):
|
|||||||
|
|
||||||
def execute(self, context: Context) -> Set[str]:
|
def execute(self, context: Context) -> Set[str]:
|
||||||
try:
|
try:
|
||||||
|
logger.info("Starting Rigify to Unity conversion")
|
||||||
armature = get_active_armature(context)
|
armature = get_active_armature(context)
|
||||||
if not armature:
|
if not armature:
|
||||||
|
logger.error("No armature found")
|
||||||
self.report({'ERROR'}, t("Tools.no_armature"))
|
self.report({'ERROR'}, t("Tools.no_armature"))
|
||||||
return {'CANCELLED'}
|
return {'CANCELLED'}
|
||||||
|
|
||||||
logger.info("Starting Rigify to Unity conversion")
|
logger.debug(f"Converting armature: {armature.name}")
|
||||||
|
|
||||||
# Rename armature object to "Armature"
|
|
||||||
armature.name = "Armature"
|
armature.name = "Armature"
|
||||||
armature.data.name = "Armature"
|
armature.data.name = "Armature"
|
||||||
|
logger.debug("Renamed armature to 'Armature'")
|
||||||
|
|
||||||
if "DEF-spine" in armature.data.bones:
|
if "DEF-spine" in armature.data.bones:
|
||||||
|
logger.info("Processing DEF bones")
|
||||||
self.move_def_bones(armature)
|
self.move_def_bones(armature)
|
||||||
self.rename_bones_for_unity(armature)
|
self.rename_bones_for_unity(armature)
|
||||||
else:
|
else:
|
||||||
|
logger.info("Processing basic bones")
|
||||||
self.cleanup_extra_bones(armature)
|
self.cleanup_extra_bones(armature)
|
||||||
self.rename_basic_bones_for_unity(armature)
|
self.rename_basic_bones_for_unity(armature)
|
||||||
|
|
||||||
|
logger.debug("Cleaning up bone collections")
|
||||||
self.cleanup_bone_collections(armature)
|
self.cleanup_bone_collections(armature)
|
||||||
|
|
||||||
if context.scene.avatar_toolkit.merge_twist_bones:
|
if context.scene.avatar_toolkit.merge_twist_bones:
|
||||||
logger.debug("Merging twist bones")
|
logger.info("Merging twist bones")
|
||||||
self.handle_twist_bones(armature)
|
self.handle_twist_bones(armature)
|
||||||
|
|
||||||
|
logger.info("Successfully converted Rigify armature to Unity format")
|
||||||
self.report({'INFO'}, t("Tools.rigify_converted"))
|
self.report({'INFO'}, t("Tools.rigify_converted"))
|
||||||
return {'FINISHED'}
|
return {'FINISHED'}
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to convert Rigify: {str(e)}")
|
logger.error(f"Failed to convert Rigify: {str(e)}", exc_info=True)
|
||||||
self.report({'ERROR'}, str(e))
|
self.report({'ERROR'}, str(e))
|
||||||
return {'CANCELLED'}
|
return {'CANCELLED'}
|
||||||
|
|
||||||
def cleanup_extra_bones(self, armature: Object) -> None:
|
def cleanup_extra_bones(self, armature: Object) -> None:
|
||||||
"""Remove unnecessary bones and merge neck bones"""
|
"""Remove unnecessary bones and merge neck bones"""
|
||||||
|
logger.debug("Starting cleanup of extra bones")
|
||||||
|
|
||||||
|
# Set armature as active object before mode switch
|
||||||
|
bpy.context.view_layer.objects.active = armature
|
||||||
bpy.ops.object.mode_set(mode='EDIT')
|
bpy.ops.object.mode_set(mode='EDIT')
|
||||||
|
|
||||||
# Remove bones matching patterns from dictionary
|
bones_to_remove: List[str] = []
|
||||||
bones_to_remove = []
|
|
||||||
for bone in armature.data.edit_bones:
|
for bone in armature.data.edit_bones:
|
||||||
if any(pattern in bone.name.lower() for pattern in rigify_unnecessary_bones):
|
if any(pattern in bone.name.lower() for pattern in rigify_unnecessary_bones):
|
||||||
bones_to_remove.append(bone.name)
|
bones_to_remove.append(bone.name)
|
||||||
|
|
||||||
for bone_name in bones_to_remove:
|
for bone_name in bones_to_remove:
|
||||||
if bone_name in armature.data.edit_bones:
|
if bone_name in armature.data.edit_bones:
|
||||||
|
logger.debug(f"Removing bone: {bone_name}")
|
||||||
armature.data.edit_bones.remove(armature.data.edit_bones[bone_name])
|
armature.data.edit_bones.remove(armature.data.edit_bones[bone_name])
|
||||||
|
|
||||||
# Handle neck bones
|
|
||||||
if 'spine.004' in armature.data.edit_bones and 'spine.005' in armature.data.edit_bones:
|
if 'spine.004' in armature.data.edit_bones and 'spine.005' in armature.data.edit_bones:
|
||||||
|
logger.debug("Merging neck bones")
|
||||||
neck_start = armature.data.edit_bones['spine.004']
|
neck_start = armature.data.edit_bones['spine.004']
|
||||||
neck_end = armature.data.edit_bones['spine.005']
|
neck_end = armature.data.edit_bones['spine.005']
|
||||||
|
|
||||||
# Merge neck bones
|
|
||||||
neck_start.tail = neck_end.tail
|
neck_start.tail = neck_end.tail
|
||||||
armature.data.edit_bones.remove(neck_end)
|
armature.data.edit_bones.remove(neck_end)
|
||||||
neck_start.name = "Neck"
|
neck_start.name = "Neck"
|
||||||
|
|
||||||
# Rename head bone
|
|
||||||
if 'spine.006' in armature.data.edit_bones:
|
if 'spine.006' in armature.data.edit_bones:
|
||||||
|
logger.debug("Renaming head bone")
|
||||||
head_bone = armature.data.edit_bones['spine.006']
|
head_bone = armature.data.edit_bones['spine.006']
|
||||||
head_bone.name = "Head"
|
head_bone.name = "Head"
|
||||||
|
|
||||||
def move_def_bones(self, armature: Object) -> None:
|
def move_def_bones(self, armature: Object) -> None:
|
||||||
"""Move DEF bones to their correct positions"""
|
"""Move DEF bones to their correct positions"""
|
||||||
remap = self.get_org_remap(armature)
|
logger.debug("Moving DEF bones to correct positions")
|
||||||
|
|
||||||
|
# Set armature as active object
|
||||||
|
bpy.context.view_layer.objects.active = armature
|
||||||
|
remap: Dict[str, str] = self.get_org_remap(armature)
|
||||||
remap.update(self.get_special_remap())
|
remap.update(self.get_special_remap())
|
||||||
|
|
||||||
remove_bones_in_chain = [
|
remove_bones_in_chain: List[str] = [
|
||||||
'DEF-upper_arm.L.001', 'DEF-forearm.L.001',
|
'DEF-upper_arm.L.001', 'DEF-forearm.L.001',
|
||||||
'DEF-upper_arm.R.001', 'DEF-forearm.R.001',
|
'DEF-upper_arm.R.001', 'DEF-forearm.R.001',
|
||||||
'DEF-thigh.L.001', 'DEF-shin.L.001',
|
'DEF-thigh.L.001', 'DEF-shin.L.001',
|
||||||
'DEF-thigh.R.001', 'DEF-shin.R.001'
|
'DEF-thigh.R.001', 'DEF-shin.R.001'
|
||||||
]
|
]
|
||||||
|
|
||||||
transform_copies = self.get_transform_copies(armature)
|
transform_copies: List[str] = self.get_transform_copies(armature)
|
||||||
|
|
||||||
|
logger.debug("Setting up transform copies")
|
||||||
bpy.ops.object.mode_set(mode='POSE')
|
bpy.ops.object.mode_set(mode='POSE')
|
||||||
for bone_name in transform_copies:
|
for bone_name in transform_copies:
|
||||||
bone = armature.pose.bones[bone_name]
|
bone = armature.pose.bones[bone_name]
|
||||||
@@ -110,11 +122,13 @@ class AvatarToolkit_OT_ConvertRigifyToUnity(Operator):
|
|||||||
if constr_count > 1:
|
if constr_count > 1:
|
||||||
bone.constraints.move(constr_count-1, 0)
|
bone.constraints.move(constr_count-1, 0)
|
||||||
|
|
||||||
|
logger.debug("Remapping bone parents")
|
||||||
bpy.ops.object.mode_set(mode='EDIT')
|
bpy.ops.object.mode_set(mode='EDIT')
|
||||||
for remap_key in remap:
|
for remap_key in remap:
|
||||||
if remap_key in armature.data.edit_bones and remap[remap_key] in armature.data.edit_bones:
|
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]]
|
armature.data.edit_bones[remap_key].parent = armature.data.edit_bones[remap[remap_key]]
|
||||||
|
|
||||||
|
logger.debug("Processing bone chain removal")
|
||||||
bpy.ops.object.mode_set(mode='OBJECT')
|
bpy.ops.object.mode_set(mode='OBJECT')
|
||||||
for bone_name in remove_bones_in_chain:
|
for bone_name in remove_bones_in_chain:
|
||||||
if bone_name in armature.data.bones:
|
if bone_name in armature.data.bones:
|
||||||
@@ -133,33 +147,38 @@ class AvatarToolkit_OT_ConvertRigifyToUnity(Operator):
|
|||||||
|
|
||||||
def rename_bones_for_unity(self, armature: Object) -> None:
|
def rename_bones_for_unity(self, armature: Object) -> None:
|
||||||
"""Rename bones to Unity-compatible names"""
|
"""Rename bones to Unity-compatible names"""
|
||||||
|
logger.debug("Renaming bones to Unity format")
|
||||||
for old_name, new_name in rigify_unity_names.items():
|
for old_name, new_name in rigify_unity_names.items():
|
||||||
bone = armature.pose.bones.get(old_name)
|
bone = armature.pose.bones.get(old_name)
|
||||||
if bone:
|
if bone:
|
||||||
|
logger.debug(f"Renaming bone: {old_name} -> {new_name}")
|
||||||
bone.name = new_name
|
bone.name = new_name
|
||||||
|
|
||||||
def rename_basic_bones_for_unity(self, armature: Object) -> None:
|
def rename_basic_bones_for_unity(self, armature: Object) -> None:
|
||||||
"""Rename basic metarig bones to Unity-compatible names"""
|
"""Rename basic metarig bones to Unity-compatible names"""
|
||||||
|
logger.debug("Renaming basic metarig bones")
|
||||||
for old_name, new_name in rigify_basic_unity_names.items():
|
for old_name, new_name in rigify_basic_unity_names.items():
|
||||||
bone = armature.pose.bones.get(old_name)
|
bone = armature.pose.bones.get(old_name)
|
||||||
if bone:
|
if bone:
|
||||||
|
logger.debug(f"Renaming basic bone: {old_name} -> {new_name}")
|
||||||
bone.name = new_name
|
bone.name = new_name
|
||||||
|
|
||||||
def cleanup_bone_collections(self, armature: Object) -> None:
|
def cleanup_bone_collections(self, armature: Object) -> None:
|
||||||
"""Remove all bone collections since they're not needed for Unity"""
|
"""Remove all bone collections since they're not needed for Unity"""
|
||||||
|
logger.debug("Cleaning up bone collections")
|
||||||
if hasattr(armature.data, 'collections') and armature.data.collections:
|
if hasattr(armature.data, 'collections') and armature.data.collections:
|
||||||
while len(armature.data.collections) > 0:
|
while len(armature.data.collections) > 0:
|
||||||
collection = armature.data.collections[0]
|
collection = armature.data.collections[0]
|
||||||
armature.data.collections.remove(collection)
|
armature.data.collections.remove(collection)
|
||||||
|
|
||||||
# Remove other collections
|
|
||||||
while len(armature.data.collections) > 1:
|
while len(armature.data.collections) > 1:
|
||||||
collection = armature.data.collections[1]
|
collection = armature.data.collections[1]
|
||||||
armature.data.collections.remove(collection)
|
armature.data.collections.remove(collection)
|
||||||
|
|
||||||
def handle_twist_bones(self, armature: Object) -> None:
|
def handle_twist_bones(self, armature: Object) -> None:
|
||||||
"""Handle twist bones during conversion"""
|
"""Handle twist bones during conversion"""
|
||||||
twist_bones = [
|
logger.debug("Processing twist bones")
|
||||||
|
twist_bones: List[Tuple[str, str]] = [
|
||||||
("DEF-upper_arm_twist.L", "DEF-upper_arm.L"),
|
("DEF-upper_arm_twist.L", "DEF-upper_arm.L"),
|
||||||
("DEF-upper_arm_twist.R", "DEF-upper_arm.R"),
|
("DEF-upper_arm_twist.R", "DEF-upper_arm.R"),
|
||||||
("DEF-forearm_twist.L", "DEF-forearm.L"),
|
("DEF-forearm_twist.L", "DEF-forearm.L"),
|
||||||
@@ -171,6 +190,7 @@ class AvatarToolkit_OT_ConvertRigifyToUnity(Operator):
|
|||||||
bpy.ops.object.mode_set(mode='EDIT')
|
bpy.ops.object.mode_set(mode='EDIT')
|
||||||
for twist_bone, parent_bone in twist_bones:
|
for twist_bone, parent_bone in twist_bones:
|
||||||
if twist_bone in armature.data.edit_bones and parent_bone in armature.data.edit_bones:
|
if twist_bone in armature.data.edit_bones and parent_bone in armature.data.edit_bones:
|
||||||
|
logger.debug(f"Merging twist bone: {twist_bone} into {parent_bone}")
|
||||||
twist = armature.data.edit_bones[twist_bone]
|
twist = armature.data.edit_bones[twist_bone]
|
||||||
parent = armature.data.edit_bones[parent_bone]
|
parent = armature.data.edit_bones[parent_bone]
|
||||||
parent.tail = twist.tail
|
parent.tail = twist.tail
|
||||||
@@ -182,7 +202,8 @@ class AvatarToolkit_OT_ConvertRigifyToUnity(Operator):
|
|||||||
|
|
||||||
def get_org_remap(self, armature: Object) -> Dict[str, str]:
|
def get_org_remap(self, armature: Object) -> Dict[str, str]:
|
||||||
"""Get original bone remapping"""
|
"""Get original bone remapping"""
|
||||||
remap = {}
|
logger.debug("Getting original bone remapping")
|
||||||
|
remap: Dict[str, str] = {}
|
||||||
for bone in armature.data.bones:
|
for bone in armature.data.bones:
|
||||||
if self.is_def_bone(bone.name):
|
if self.is_def_bone(bone.name):
|
||||||
name = self.get_proto_name(bone.name)
|
name = self.get_proto_name(bone.name)
|
||||||
@@ -198,6 +219,7 @@ class AvatarToolkit_OT_ConvertRigifyToUnity(Operator):
|
|||||||
|
|
||||||
def get_special_remap(self) -> Dict[str, str]:
|
def get_special_remap(self) -> Dict[str, str]:
|
||||||
"""Get special bone remapping cases"""
|
"""Get special bone remapping cases"""
|
||||||
|
logger.debug("Getting special bone remapping")
|
||||||
return {
|
return {
|
||||||
'DEF-thigh.L': 'DEF-pelvis.L',
|
'DEF-thigh.L': 'DEF-pelvis.L',
|
||||||
'DEF-thigh.R': 'DEF-pelvis.R',
|
'DEF-thigh.R': 'DEF-pelvis.R',
|
||||||
@@ -207,7 +229,8 @@ class AvatarToolkit_OT_ConvertRigifyToUnity(Operator):
|
|||||||
|
|
||||||
def get_transform_copies(self, armature: Object) -> List[str]:
|
def get_transform_copies(self, armature: Object) -> List[str]:
|
||||||
"""Get bones that need transform copies"""
|
"""Get bones that need transform copies"""
|
||||||
result = []
|
logger.debug("Getting transform copy bones")
|
||||||
|
result: List[str] = []
|
||||||
for bone in armature.pose.bones:
|
for bone in armature.pose.bones:
|
||||||
if self.is_def_bone(bone.name) and not self.has_transform_copies(bone):
|
if self.is_def_bone(bone.name) and not self.has_transform_copies(bone):
|
||||||
result.append(bone.name)
|
result.append(bone.name)
|
||||||
|
|||||||
Reference in New Issue
Block a user