Organise Code and fixes.

- Organised some of the code better.
- Fixed sort order loop.
- Added typing in some places there wasn't.

This is a very basic start to Viesme creation, I still need to add translations for some stuff and improve it. This is very much inspired from the Cats Version.
This commit is contained in:
Yusarina
2024-07-08 10:14:30 +01:00
parent 07b2dba51f
commit 2107c11e66
6 changed files with 112 additions and 62 deletions
+34 -17
View File
@@ -42,10 +42,10 @@ def has_shapekeys(mesh_obj: Object) -> bool:
def _get_shape_key_co(shape_key: ShapeKey) -> np.ndarray: def _get_shape_key_co(shape_key: ShapeKey) -> np.ndarray:
return np.array([v.co for v in shape_key.data]) return np.array([v.co for v in shape_key.data])
def simplify_bonename(n): def simplify_bonename(n: str) -> str:
return n.lower().translate(dict.fromkeys(map(ord, u" _."))) return n.lower().translate(dict.fromkeys(map(ord, u" _.")))
def get_armature(context, armature_name=None) -> Optional[Object]: def get_armature(context: Context, armature_name: Optional[str] = None) -> Optional[Object]:
if armature_name: if armature_name:
obj = bpy.data.objects[armature_name] obj = bpy.data.objects[armature_name]
if obj.type == "ARMATURE": if obj.type == "ARMATURE":
@@ -58,14 +58,16 @@ def get_armature(context, armature_name=None) -> Optional[Object]:
return obj return obj
return next((obj for obj in context.view_layer.objects if obj.type == 'ARMATURE'), None) return next((obj for obj in context.view_layer.objects if obj.type == 'ARMATURE'), None)
def has_shapekeys(mesh_obj): def has_shapekeys(mesh_obj: Object) -> bool:
return mesh_obj.data.shape_keys is not None return mesh_obj.data.shape_keys is not None
def has_shapekeys(mesh_obj): def has_shapekeys(mesh_obj: Object) -> bool:
return mesh_obj.data.shape_keys is not None return mesh_obj.data.shape_keys is not None
def sort_shape_keys(mesh): def sort_shape_keys(mesh: Object) -> None:
print("Starting shape key sorting...")
if not has_shapekeys(mesh): if not has_shapekeys(mesh):
print("No shape keys found. Exiting sort function.")
return return
order = [ order = [
@@ -92,23 +94,38 @@ def sort_shape_keys(mesh):
] ]
shape_keys = mesh.data.shape_keys.key_blocks shape_keys = mesh.data.shape_keys.key_blocks
for i, name in enumerate(order): print(f"Total shape keys: {len(shape_keys)}")
if name in shape_keys:
# Create a list of shape key names in their current order
current_order = [key.name for key in shape_keys]
# Create a new order list
new_order = []
# First, add all the keys that are in the predefined order
for name in order:
if name in current_order:
new_order.append(name)
current_order.remove(name)
# Then add any remaining keys that weren't in the predefined order
new_order.extend(current_order)
print("New order:", new_order)
# Now, rearrange the shape keys based on the new order
for i, name in enumerate(new_order):
index = shape_keys.find(name) index = shape_keys.find(name)
if index != i: if index != i:
print(f"Moving {name} from index {index} to {i}")
bpy.context.object.active_shape_key_index = index bpy.context.object.active_shape_key_index = index
for _ in range(abs(index - i)): while bpy.context.object.active_shape_key_index > i:
bpy.ops.object.shape_key_move(type='UP' if index > i else 'DOWN') bpy.ops.object.shape_key_move(type='UP')
# Move any remaining shape keys to the end print("Shape key sorting completed.")
for key in shape_keys:
if key.name not in order:
index = shape_keys.find(key.name)
bpy.context.object.active_shape_key_index = index
for _ in range(len(shape_keys) - index - 1):
bpy.ops.object.shape_key_move(type='DOWN')
def get_shapekeys(mesh, prefix=''):
def get_shapekeys(mesh: Object, prefix: str = '') -> List[tuple]:
if not has_shapekeys(mesh): if not has_shapekeys(mesh):
return [] return []
return [(key.name, key.name, key.name) for key in mesh.data.shape_keys.key_blocks if key.name != 'Basis' and key.name.startswith(prefix)] return [(key.name, key.name, key.name) for key in mesh.data.shape_keys.key_blocks if key.name != 'Basis' and key.name.startswith(prefix)]
+21 -8
View File
@@ -2,7 +2,7 @@ import bpy
from ..functions.translations import t, get_languages_list, update_language from ..functions.translations import t, get_languages_list, update_language
from ..core.addon_preferences import get_preference from ..core.addon_preferences import get_preference
def register(): def register() -> None:
default_language = get_preference("language", 0) default_language = get_preference("language", 0)
bpy.types.Scene.avatar_toolkit_language = bpy.props.EnumProperty( bpy.types.Scene.avatar_toolkit_language = bpy.props.EnumProperty(
@@ -13,15 +13,28 @@ def register():
update=update_language update=update_language
) )
bpy.types.Scene.mouth_a = bpy.props.StringProperty(name=t("Scene.mouth_a.label"), description=t("Scene.mouth_a.desc")) bpy.types.Scene.mouth_a = bpy.props.StringProperty(
bpy.types.Scene.mouth_o = bpy.props.StringProperty(name=t("Scene.mouth_o.label"), description=t("Scene.mouth_o.desc")) name=t("Scene.mouth_a.label"),
bpy.types.Scene.mouth_ch = bpy.props.StringProperty(name=t("Scene.mouth_ch.label"), description=t("Scene.mouth_ch.desc")) description=t("Scene.mouth_a.desc")
bpy.types.Scene.shape_intensity = bpy.props.FloatProperty(name=t("Scene.shape_intensity.label"), description=t("Scene.shape_intensity.desc"), default=1.0, min=0.0, max=2.0) )
bpy.types.Scene.mouth_o = bpy.props.StringProperty(
name=t("Scene.mouth_o.label"),
description=t("Scene.mouth_o.desc")
)
bpy.types.Scene.mouth_ch = bpy.props.StringProperty(
name=t("Scene.mouth_ch.label"),
description=t("Scene.mouth_ch.desc")
)
bpy.types.Scene.shape_intensity = bpy.props.FloatProperty(
name=t("Scene.shape_intensity.label"),
description=t("Scene.shape_intensity.desc"),
default=1.0,
min=0.0,
max=2.0
)
def unregister(): def unregister() -> None:
if hasattr(bpy.types.Scene, "avatar_toolkit_language"):
del bpy.types.Scene.avatar_toolkit_language del bpy.types.Scene.avatar_toolkit_language
del bpy.types.Scene.mouth_a del bpy.types.Scene.mouth_a
del bpy.types.Scene.mouth_o del bpy.types.Scene.mouth_o
del bpy.types.Scene.mouth_ch del bpy.types.Scene.mouth_ch
+23 -16
View File
@@ -2,6 +2,7 @@ import bpy
from ..core import common from ..core import common
from ..core.register import register_wrap from ..core.register import register_wrap
from ..functions.translations import t from ..functions.translations import t
from typing import List, Tuple
@register_wrap @register_wrap
class AutoVisemeButton(bpy.types.Operator): class AutoVisemeButton(bpy.types.Operator):
@@ -11,10 +12,11 @@ class AutoVisemeButton(bpy.types.Operator):
bl_options = {'REGISTER', 'UNDO'} bl_options = {'REGISTER', 'UNDO'}
@classmethod @classmethod
def poll(cls, context): def poll(cls, context: bpy.types.Context) -> bool:
return context.active_object and context.active_object.type == 'MESH' return context.active_object and context.active_object.type == 'MESH'
def execute(self, context): def execute(self, context: bpy.types.Context) -> set:
print("Starting viseme creation...")
mesh = context.active_object mesh = context.active_object
if not mesh or not common.has_shapekeys(mesh): if not mesh or not common.has_shapekeys(mesh):
self.report({'ERROR'}, t('AutoVisemeButton.error.noShapekeys')) self.report({'ERROR'}, t('AutoVisemeButton.error.noShapekeys'))
@@ -24,12 +26,14 @@ class AutoVisemeButton(bpy.types.Operator):
shape_o = context.scene.mouth_o shape_o = context.scene.mouth_o
shape_ch = context.scene.mouth_ch shape_ch = context.scene.mouth_ch
print(f"Selected shapes: A={shape_a}, O={shape_o}, CH={shape_ch}")
if shape_a == "Basis" or shape_o == "Basis" or shape_ch == "Basis": if shape_a == "Basis" or shape_o == "Basis" or shape_ch == "Basis":
self.report({'ERROR'}, t('AutoVisemeButton.error.selectShapekeys')) self.report({'ERROR'}, t('AutoVisemeButton.error.selectShapekeys'))
return {'CANCELLED'} return {'CANCELLED'}
# Create visemes # Create visemes
visemes = [ visemes: List[Tuple[str, List[Tuple[str, float]]]] = [
('vrc.v_aa', [(shape_a, 0.9998)]), ('vrc.v_aa', [(shape_a, 0.9998)]),
('vrc.v_ch', [(shape_ch, 0.9996)]), ('vrc.v_ch', [(shape_ch, 0.9996)]),
('vrc.v_dd', [(shape_a, 0.3), (shape_ch, 0.7)]), ('vrc.v_dd', [(shape_a, 0.3), (shape_ch, 0.7)]),
@@ -48,18 +52,24 @@ class AutoVisemeButton(bpy.types.Operator):
] ]
for viseme_name, shape_mix in visemes: for viseme_name, shape_mix in visemes:
print(f"Creating viseme: {viseme_name}")
self.create_viseme(mesh, viseme_name, shape_mix, context.scene.shape_intensity) self.create_viseme(mesh, viseme_name, shape_mix, context.scene.shape_intensity)
# Sort shape keys print("Sorting shape keys...")
common.sort_shape_keys(mesh) common.sort_shape_keys(mesh)
print("Viseme creation completed.")
self.report({'INFO'}, t('AutoVisemeButton.success')) self.report({'INFO'}, t('AutoVisemeButton.success'))
return {'FINISHED'} return {'FINISHED'}
def create_viseme(self, mesh, viseme_name, shape_mix, intensity): def create_viseme(self, mesh: bpy.types.Object, viseme_name: str, shape_mix: List[Tuple[str, float]], intensity: float) -> None:
print(f" Creating viseme: {viseme_name}")
shape_keys = mesh.data.shape_keys.key_blocks
# Remove existing viseme if it exists # Remove existing viseme if it exists
if viseme_name in mesh.data.shape_keys.key_blocks: if viseme_name in shape_keys:
mesh.shape_key_remove(mesh.data.shape_keys.key_blocks[viseme_name]) print(f" Removing existing viseme: {viseme_name}")
mesh.shape_key_remove(shape_keys[viseme_name])
# Create new viseme # Create new viseme
new_key = mesh.shape_key_add(name=viseme_name, from_mix=False) new_key = mesh.shape_key_add(name=viseme_name, from_mix=False)
@@ -67,15 +77,12 @@ class AutoVisemeButton(bpy.types.Operator):
# Mix shapes # Mix shapes
for shape_name, value in shape_mix: for shape_name, value in shape_mix:
if shape_name in mesh.data.shape_keys.key_blocks: if shape_name in shape_keys:
shape = mesh.data.shape_keys.key_blocks[shape_name] source_shape = shape_keys[shape_name]
shape.value = value * intensity print(f" Mixing shape: {shape_name} with value: {value * intensity}")
for i, vert in enumerate(new_key.data):
vert.co += (source_shape.data[i].co - shape_keys['Basis'].data[i].co) * value * intensity
# Apply mix print(f" Viseme {viseme_name} created successfully.")
mesh.shape_key_add(name=viseme_name, from_mix=True)
# Reset shape key values
for shape in mesh.data.shape_keys.key_blocks:
shape.value = 0.0
new_key.value = 1.0
+5 -1
View File
@@ -31,7 +31,11 @@
"Tools.convert_to_resonite.desc": "Converts bone names on a model to names compatable with Resonite", "Tools.convert_to_resonite.desc": "Converts bone names on a model to names compatable with Resonite",
"Settings.label": "Settings", "Settings.label": "Settings",
"Settings.language.label": "Language", "Settings.language.label": "Language",
"Settings.language.desc": "Select the language for the addon's UI" "Settings.language.desc": "Select the language for the addon's UI",
"Viseme.label": "Visemes",
"Viseme.error.noMesh": "No mesh selected",
"Viseme.error.noShapekeys": "Selected mesh has no shape keys",
"Viseme.info.selectMesh": "Select a mesh to create visemes"
} }
} }
+5 -1
View File
@@ -31,6 +31,10 @@
"Tools.convert_to_resonite.desc": "モデルのボーン名をResoniteと互換性のある名前に変換します", "Tools.convert_to_resonite.desc": "モデルのボーン名をResoniteと互換性のある名前に変換します",
"Settings.label": "設定", "Settings.label": "設定",
"Settings.language.label": "言語", "Settings.language.label": "言語",
"Settings.language.desc": "アドオンのUI言語を選択してください" "Settings.language.desc": "アドオンのUI言語を選択してください",
"Viseme.label": "ビセーム",
"Viseme.error.noMesh": "メッシュが選択されていません",
"Viseme.error.noShapekeys": "選択されたメッシュにシェイプキーがありません",
"Viseme.info.selectMesh": "ビセームを作成するメッシュを選択してください"
} }
} }
+14 -9
View File
@@ -11,18 +11,15 @@ class AvatarToolkitVisemePanel(bpy.types.Panel):
bl_category = "Avatar Toolkit" bl_category = "Avatar Toolkit"
bl_parent_id = "OBJECT_PT_avatar_toolkit" bl_parent_id = "OBJECT_PT_avatar_toolkit"
def draw(self, context): def draw(self, context: bpy.types.Context) -> None:
layout = self.layout layout = self.layout
# Check if there's an active object and it's a mesh
if context.active_object and context.active_object.type == 'MESH':
mesh = context.active_object mesh = context.active_object
if not mesh or mesh.type != 'MESH': # Check if the mesh has shape keys
layout.label(text=t('VisemePanel.error.noMesh'), icon='ERROR') if mesh.data.shape_keys:
return
if not mesh.data.shape_keys:
layout.label(text=t('VisemePanel.error.noShapekeys'), icon='ERROR')
return
layout.prop_search(context.scene, "mouth_a", mesh.data.shape_keys, "key_blocks", text=t('Scene.mouth_a.label')) layout.prop_search(context.scene, "mouth_a", mesh.data.shape_keys, "key_blocks", text=t('Scene.mouth_a.label'))
layout.prop_search(context.scene, "mouth_o", mesh.data.shape_keys, "key_blocks", text=t('Scene.mouth_o.label')) layout.prop_search(context.scene, "mouth_o", mesh.data.shape_keys, "key_blocks", text=t('Scene.mouth_o.label'))
layout.prop_search(context.scene, "mouth_ch", mesh.data.shape_keys, "key_blocks", text=t('Scene.mouth_ch.label')) layout.prop_search(context.scene, "mouth_ch", mesh.data.shape_keys, "key_blocks", text=t('Scene.mouth_ch.label'))
@@ -30,3 +27,11 @@ class AvatarToolkitVisemePanel(bpy.types.Panel):
layout.prop(context.scene, 'shape_intensity') layout.prop(context.scene, 'shape_intensity')
layout.operator("avatar_toolkit.create_visemes", icon='TRIA_RIGHT') layout.operator("avatar_toolkit.create_visemes", icon='TRIA_RIGHT')
else:
layout.label(text=t('VisemePanel.error.noShapekeys'), icon='ERROR')
else:
layout.label(text=t('VisemePanel.error.noMesh'), icon='ERROR')
# Always show some information or options
layout.separator()
layout.label(text=t('VisemePanel.info.selectMesh'))