From 07b2dba51f1ad8bc5b87fec52b66e6469c7919cc Mon Sep 17 00:00:00 2001 From: Yusarina Date: Mon, 8 Jul 2024 09:41:46 +0100 Subject: [PATCH 1/6] Basic Viseme Creation Support Does not work yet, but it's the start --- core/common.py | 55 ++++++++++++++++++++++++++++++ core/properties.py | 12 ++++++- core/register.py | 1 + functions/viseme.py | 81 +++++++++++++++++++++++++++++++++++++++++++++ ui/viseme.py | 32 ++++++++++++++++++ 5 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 functions/viseme.py create mode 100644 ui/viseme.py diff --git a/core/common.py b/core/common.py index 6e08f93..297b37d 100644 --- a/core/common.py +++ b/core/common.py @@ -57,3 +57,58 @@ def get_armature(context, armature_name=None) -> Optional[Object]: if obj.type == "ARMATURE": return obj return next((obj for obj in context.view_layer.objects if obj.type == 'ARMATURE'), None) + +def has_shapekeys(mesh_obj): + return mesh_obj.data.shape_keys is not None + +def has_shapekeys(mesh_obj): + return mesh_obj.data.shape_keys is not None + +def sort_shape_keys(mesh): + if not has_shapekeys(mesh): + return + + order = [ + 'Basis', + 'vrc.blink_left', + 'vrc.blink_right', + 'vrc.lowerlid_left', + 'vrc.lowerlid_right', + 'vrc.v_aa', + 'vrc.v_ch', + 'vrc.v_dd', + 'vrc.v_e', + 'vrc.v_ff', + 'vrc.v_ih', + 'vrc.v_kk', + 'vrc.v_nn', + 'vrc.v_oh', + 'vrc.v_ou', + 'vrc.v_pp', + 'vrc.v_rr', + 'vrc.v_sil', + 'vrc.v_ss', + 'vrc.v_th', + ] + + shape_keys = mesh.data.shape_keys.key_blocks + for i, name in enumerate(order): + if name in shape_keys: + index = shape_keys.find(name) + if index != i: + bpy.context.object.active_shape_key_index = index + for _ in range(abs(index - i)): + bpy.ops.object.shape_key_move(type='UP' if index > i else 'DOWN') + + # Move any remaining shape keys to the end + 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=''): + if not has_shapekeys(mesh): + 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)] diff --git a/core/properties.py b/core/properties.py index 9bf108d..33b7f6e 100644 --- a/core/properties.py +++ b/core/properties.py @@ -13,6 +13,16 @@ def register(): 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_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(): if hasattr(bpy.types.Scene, "avatar_toolkit_language"): - del bpy.types.Scene.avatar_toolkit_language \ No newline at end of file + del bpy.types.Scene.avatar_toolkit_language + + del bpy.types.Scene.mouth_a + del bpy.types.Scene.mouth_o + del bpy.types.Scene.mouth_ch + del bpy.types.Scene.shape_intensity \ No newline at end of file diff --git a/core/register.py b/core/register.py index 449050e..aad610f 100644 --- a/core/register.py +++ b/core/register.py @@ -1,4 +1,5 @@ import bpy +import typing from typing import List, Type # List to store the classes to register diff --git a/functions/viseme.py b/functions/viseme.py new file mode 100644 index 0000000..e3fce99 --- /dev/null +++ b/functions/viseme.py @@ -0,0 +1,81 @@ +import bpy +from ..core import common +from ..core.register import register_wrap +from ..functions.translations import t + +@register_wrap +class AutoVisemeButton(bpy.types.Operator): + bl_idname = 'avatar_toolkit.create_visemes' + bl_label = t('AutoVisemeButton.label') + bl_description = t('AutoVisemeButton.desc') + bl_options = {'REGISTER', 'UNDO'} + + @classmethod + def poll(cls, context): + return context.active_object and context.active_object.type == 'MESH' + + def execute(self, context): + mesh = context.active_object + if not mesh or not common.has_shapekeys(mesh): + self.report({'ERROR'}, t('AutoVisemeButton.error.noShapekeys')) + return {'CANCELLED'} + + shape_a = context.scene.mouth_a + shape_o = context.scene.mouth_o + shape_ch = context.scene.mouth_ch + + if shape_a == "Basis" or shape_o == "Basis" or shape_ch == "Basis": + self.report({'ERROR'}, t('AutoVisemeButton.error.selectShapekeys')) + return {'CANCELLED'} + + # Create visemes + visemes = [ + ('vrc.v_aa', [(shape_a, 0.9998)]), + ('vrc.v_ch', [(shape_ch, 0.9996)]), + ('vrc.v_dd', [(shape_a, 0.3), (shape_ch, 0.7)]), + ('vrc.v_e', [(shape_a, 0.5), (shape_ch, 0.2)]), + ('vrc.v_ff', [(shape_a, 0.2), (shape_ch, 0.4)]), + ('vrc.v_ih', [(shape_ch, 0.7), (shape_o, 0.3)]), + ('vrc.v_kk', [(shape_a, 0.7), (shape_ch, 0.4)]), + ('vrc.v_nn', [(shape_a, 0.2), (shape_ch, 0.7)]), + ('vrc.v_oh', [(shape_a, 0.2), (shape_o, 0.8)]), + ('vrc.v_ou', [(shape_o, 0.9994)]), + ('vrc.v_pp', [(shape_a, 0.0004), (shape_o, 0.0004)]), + ('vrc.v_rr', [(shape_ch, 0.5), (shape_o, 0.3)]), + ('vrc.v_sil', [(shape_a, 0.0002), (shape_ch, 0.0002)]), + ('vrc.v_ss', [(shape_ch, 0.8)]), + ('vrc.v_th', [(shape_a, 0.4), (shape_o, 0.15)]) + ] + + for viseme_name, shape_mix in visemes: + self.create_viseme(mesh, viseme_name, shape_mix, context.scene.shape_intensity) + + # Sort shape keys + common.sort_shape_keys(mesh) + + self.report({'INFO'}, t('AutoVisemeButton.success')) + return {'FINISHED'} + + def create_viseme(self, mesh, viseme_name, shape_mix, intensity): + # Remove existing viseme if it exists + if viseme_name in mesh.data.shape_keys.key_blocks: + mesh.shape_key_remove(mesh.data.shape_keys.key_blocks[viseme_name]) + + # Create new viseme + new_key = mesh.shape_key_add(name=viseme_name, from_mix=False) + new_key.value = 1.0 + + # Mix shapes + for shape_name, value in shape_mix: + if shape_name in mesh.data.shape_keys.key_blocks: + shape = mesh.data.shape_keys.key_blocks[shape_name] + shape.value = value * intensity + + # Apply mix + 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 \ No newline at end of file diff --git a/ui/viseme.py b/ui/viseme.py new file mode 100644 index 0000000..5250fb3 --- /dev/null +++ b/ui/viseme.py @@ -0,0 +1,32 @@ +import bpy +from ..core.register import register_wrap +from ..functions.translations import t + +@register_wrap +class AvatarToolkitVisemePanel(bpy.types.Panel): + bl_label = t("Viseme.label") + bl_idname = "OBJECT_PT_avatar_toolkit_viseme" + bl_space_type = 'VIEW_3D' + bl_region_type = 'UI' + bl_category = "Avatar Toolkit" + bl_parent_id = "OBJECT_PT_avatar_toolkit" + + def draw(self, context): + layout = self.layout + mesh = context.active_object + + if not mesh or mesh.type != 'MESH': + layout.label(text=t('VisemePanel.error.noMesh'), icon='ERROR') + 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_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(context.scene, 'shape_intensity') + + layout.operator("avatar_toolkit.create_visemes", icon='TRIA_RIGHT') From 2107c11e6607151c8b81660323e695ba147adf05 Mon Sep 17 00:00:00 2001 From: Yusarina Date: Mon, 8 Jul 2024 10:14:30 +0100 Subject: [PATCH 2/6] 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. --- core/common.py | 55 ++++++++++++++++++++----------- core/properties.py | 33 +++++++++++++------ functions/viseme.py | 39 +++++++++++++--------- resources/translations/en_US.json | 6 +++- resources/translations/ja_JP.json | 6 +++- ui/viseme.py | 35 +++++++++++--------- 6 files changed, 112 insertions(+), 62 deletions(-) diff --git a/core/common.py b/core/common.py index 297b37d..f5dde6d 100644 --- a/core/common.py +++ b/core/common.py @@ -42,10 +42,10 @@ def has_shapekeys(mesh_obj: Object) -> bool: def _get_shape_key_co(shape_key: ShapeKey) -> np.ndarray: 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" _."))) -def get_armature(context, armature_name=None) -> Optional[Object]: +def get_armature(context: Context, armature_name: Optional[str] = None) -> Optional[Object]: if armature_name: obj = bpy.data.objects[armature_name] if obj.type == "ARMATURE": @@ -58,14 +58,16 @@ def get_armature(context, armature_name=None) -> Optional[Object]: return obj 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 -def has_shapekeys(mesh_obj): +def has_shapekeys(mesh_obj: Object) -> bool: 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): + print("No shape keys found. Exiting sort function.") return order = [ @@ -92,23 +94,38 @@ def sort_shape_keys(mesh): ] shape_keys = mesh.data.shape_keys.key_blocks - for i, name in enumerate(order): - if name in shape_keys: - index = shape_keys.find(name) - if index != i: - bpy.context.object.active_shape_key_index = index - for _ in range(abs(index - i)): - bpy.ops.object.shape_key_move(type='UP' if index > i else 'DOWN') + print(f"Total shape keys: {len(shape_keys)}") - # Move any remaining shape keys to the end - for key in shape_keys: - if key.name not in order: - index = shape_keys.find(key.name) + # 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) + if index != i: + print(f"Moving {name} from index {index} to {i}") bpy.context.object.active_shape_key_index = index - for _ in range(len(shape_keys) - index - 1): - bpy.ops.object.shape_key_move(type='DOWN') + while bpy.context.object.active_shape_key_index > i: + bpy.ops.object.shape_key_move(type='UP') -def get_shapekeys(mesh, prefix=''): + print("Shape key sorting completed.") + + +def get_shapekeys(mesh: Object, prefix: str = '') -> List[tuple]: if not has_shapekeys(mesh): 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)] diff --git a/core/properties.py b/core/properties.py index 33b7f6e..6e72fb2 100644 --- a/core/properties.py +++ b/core/properties.py @@ -2,7 +2,7 @@ import bpy from ..functions.translations import t, get_languages_list, update_language from ..core.addon_preferences import get_preference -def register(): +def register() -> None: default_language = get_preference("language", 0) bpy.types.Scene.avatar_toolkit_language = bpy.props.EnumProperty( @@ -13,16 +13,29 @@ def register(): 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_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(): - if hasattr(bpy.types.Scene, "avatar_toolkit_language"): - del bpy.types.Scene.avatar_toolkit_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_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() -> None: + del bpy.types.Scene.avatar_toolkit_language del bpy.types.Scene.mouth_a del bpy.types.Scene.mouth_o del bpy.types.Scene.mouth_ch - del bpy.types.Scene.shape_intensity \ No newline at end of file + del bpy.types.Scene.shape_intensity diff --git a/functions/viseme.py b/functions/viseme.py index e3fce99..7d60f00 100644 --- a/functions/viseme.py +++ b/functions/viseme.py @@ -2,6 +2,7 @@ import bpy from ..core import common from ..core.register import register_wrap from ..functions.translations import t +from typing import List, Tuple @register_wrap class AutoVisemeButton(bpy.types.Operator): @@ -11,10 +12,11 @@ class AutoVisemeButton(bpy.types.Operator): bl_options = {'REGISTER', 'UNDO'} @classmethod - def poll(cls, context): + def poll(cls, context: bpy.types.Context) -> bool: 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 if not mesh or not common.has_shapekeys(mesh): self.report({'ERROR'}, t('AutoVisemeButton.error.noShapekeys')) @@ -24,12 +26,14 @@ class AutoVisemeButton(bpy.types.Operator): shape_o = context.scene.mouth_o 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": self.report({'ERROR'}, t('AutoVisemeButton.error.selectShapekeys')) return {'CANCELLED'} # Create visemes - visemes = [ + visemes: List[Tuple[str, List[Tuple[str, float]]]] = [ ('vrc.v_aa', [(shape_a, 0.9998)]), ('vrc.v_ch', [(shape_ch, 0.9996)]), ('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: + print(f"Creating viseme: {viseme_name}") self.create_viseme(mesh, viseme_name, shape_mix, context.scene.shape_intensity) - # Sort shape keys + print("Sorting shape keys...") common.sort_shape_keys(mesh) + print("Viseme creation completed.") self.report({'INFO'}, t('AutoVisemeButton.success')) 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 - if viseme_name in mesh.data.shape_keys.key_blocks: - mesh.shape_key_remove(mesh.data.shape_keys.key_blocks[viseme_name]) + if viseme_name in shape_keys: + print(f" Removing existing viseme: {viseme_name}") + mesh.shape_key_remove(shape_keys[viseme_name]) # Create new viseme new_key = mesh.shape_key_add(name=viseme_name, from_mix=False) @@ -67,15 +77,12 @@ class AutoVisemeButton(bpy.types.Operator): # Mix shapes for shape_name, value in shape_mix: - if shape_name in mesh.data.shape_keys.key_blocks: - shape = mesh.data.shape_keys.key_blocks[shape_name] - shape.value = value * intensity + if shape_name in shape_keys: + source_shape = shape_keys[shape_name] + 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 - mesh.shape_key_add(name=viseme_name, from_mix=True) + print(f" Viseme {viseme_name} created successfully.") - # Reset shape key values - for shape in mesh.data.shape_keys.key_blocks: - shape.value = 0.0 - new_key.value = 1.0 \ No newline at end of file diff --git a/resources/translations/en_US.json b/resources/translations/en_US.json index c9f60cc..742b705 100644 --- a/resources/translations/en_US.json +++ b/resources/translations/en_US.json @@ -31,7 +31,11 @@ "Tools.convert_to_resonite.desc": "Converts bone names on a model to names compatable with Resonite", "Settings.label": "Settings", "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" } } \ No newline at end of file diff --git a/resources/translations/ja_JP.json b/resources/translations/ja_JP.json index 0fb43c5..ed79468 100644 --- a/resources/translations/ja_JP.json +++ b/resources/translations/ja_JP.json @@ -31,6 +31,10 @@ "Tools.convert_to_resonite.desc": "モデルのボーン名をResoniteと互換性のある名前に変換します", "Settings.label": "設定", "Settings.language.label": "言語", - "Settings.language.desc": "アドオンのUI言語を選択してください" + "Settings.language.desc": "アドオンのUI言語を選択してください", + "Viseme.label": "ビセーム", + "Viseme.error.noMesh": "メッシュが選択されていません", + "Viseme.error.noShapekeys": "選択されたメッシュにシェイプキーがありません", + "Viseme.info.selectMesh": "ビセームを作成するメッシュを選択してください" } } diff --git a/ui/viseme.py b/ui/viseme.py index 5250fb3..cede8c5 100644 --- a/ui/viseme.py +++ b/ui/viseme.py @@ -11,22 +11,27 @@ class AvatarToolkitVisemePanel(bpy.types.Panel): bl_category = "Avatar Toolkit" bl_parent_id = "OBJECT_PT_avatar_toolkit" - def draw(self, context): + def draw(self, context: bpy.types.Context) -> None: layout = self.layout - mesh = context.active_object + + # 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 + + # Check if the mesh has shape keys + if mesh.data.shape_keys: + 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_ch", mesh.data.shape_keys, "key_blocks", text=t('Scene.mouth_ch.label')) - if not mesh or mesh.type != 'MESH': + layout.prop(context.scene, 'shape_intensity') + + 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') - 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_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(context.scene, 'shape_intensity') - - layout.operator("avatar_toolkit.create_visemes", icon='TRIA_RIGHT') + # Always show some information or options + layout.separator() + layout.label(text=t('VisemePanel.info.selectMesh')) From 63c8fe5ca63da5201eb1b623845cdd0ff6c41099 Mon Sep 17 00:00:00 2001 From: Yusarina Date: Mon, 22 Jul 2024 01:09:16 +0100 Subject: [PATCH 3/6] Add missing translations --- resources/translations/en_US.json | 7 ++++--- resources/translations/ja_JP.json | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/resources/translations/en_US.json b/resources/translations/en_US.json index 742b705..34c133a 100644 --- a/resources/translations/en_US.json +++ b/resources/translations/en_US.json @@ -9,6 +9,7 @@ "Quick_Access.import_menu.label": "Import Menu", "Quick_Access.import": "Import", "Quick_Access.export": "Export", + "Quick_Access.import_menu.desc": "Import a Model", "Quick_Access.import_pmx": "Import PMX", "Quick_Access.import_pmx.desc": "Import MMD PMX Model", "Quick_Access.import_pmd": "Import PMD", @@ -33,9 +34,9 @@ "Settings.language.label": "Language", "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" + "VisemePanel.error.noMesh": "No mesh selected", + "VisemePanel.error.noShapekeys": "Selected mesh has no shape keys", + "VisemePanel.info.selectMesh": "Select a mesh to create visemes" } } \ No newline at end of file diff --git a/resources/translations/ja_JP.json b/resources/translations/ja_JP.json index ed79468..921bd06 100644 --- a/resources/translations/ja_JP.json +++ b/resources/translations/ja_JP.json @@ -9,6 +9,7 @@ "Quick_Access.import_menu.label": "インポートメニュー", "Quick_Access.import": "インポート", "Quick_Access.export": "エクスポート", + "Quick_Access.import_menu.desc": "モデルをインポート", "Quick_Access.import_pmx": "PMXインポート", "Quick_Access.import_pmx.desc": "MMD PMXモデルをインポート", "Quick_Access.import_pmd": "PMDインポート", @@ -33,8 +34,8 @@ "Settings.language.label": "言語", "Settings.language.desc": "アドオンのUI言語を選択してください", "Viseme.label": "ビセーム", - "Viseme.error.noMesh": "メッシュが選択されていません", - "Viseme.error.noShapekeys": "選択されたメッシュにシェイプキーがありません", - "Viseme.info.selectMesh": "ビセームを作成するメッシュを選択してください" + "VisemePanel.error.noMesh": "メッシュが選択されていません", + "VisemePanel.error.noShapekeys": "選択されたメッシュにシェイプキーがありません", + "VisemePanel.info.selectMesh": "ビセームを作成するメッシュを選択してください" } } From 0c331bb8575b125e17b253420041d3e38e2b38b9 Mon Sep 17 00:00:00 2001 From: Yusarina Date: Mon, 22 Jul 2024 01:21:13 +0100 Subject: [PATCH 4/6] Added Further missing translataions --- resources/translations/en_US.json | 12 ++++++++++-- resources/translations/ja_JP.json | 32 +++++++++++++++++++------------ ui/viseme.py | 8 ++++---- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/resources/translations/en_US.json b/resources/translations/en_US.json index 34c133a..e58eab8 100644 --- a/resources/translations/en_US.json +++ b/resources/translations/en_US.json @@ -33,10 +33,18 @@ "Settings.label": "Settings", "Settings.language.label": "Language", "Settings.language.desc": "Select the language for the addon's UI", - "Viseme.label": "Visemes", + "VisemePanel.label": "Visemes", "VisemePanel.error.noMesh": "No mesh selected", "VisemePanel.error.noShapekeys": "Selected mesh has no shape keys", - "VisemePanel.info.selectMesh": "Select a mesh to create visemes" + "VisemePanel.info.selectMesh": "Select a mesh to create visemes", + "VisemePanel.mouth_a.label": "Mouth A", + "VisemePanel.mouth_o.label": "Mouth O", + "VisemePanel.mouth_ch.label": "Mouth CH", + "AutoVisemeButton.label": "Create Visemes", + "AutoVisemeButton.desc": "Create visemes automatically, based on shape keys", + "AutoVisemeButton.error.noShapekeys": "No shape keys found", + "AutoVisemeButton.error.selectShapekeys": "Please Select shape keys", + "AutoVisemeButton.success": "Visemes created successfully" } } \ No newline at end of file diff --git a/resources/translations/ja_JP.json b/resources/translations/ja_JP.json index 921bd06..e9288cf 100644 --- a/resources/translations/ja_JP.json +++ b/resources/translations/ja_JP.json @@ -1,7 +1,7 @@ { "messages": { - "Language.auto": "Automatic", - "Language.en_US": "English", + "Language.auto": "自動", + "Language.en_US": "英語", "Language.ja_JP": "日本語", "Quick_Access.label": "クイックアクセス", "Quick_Access.import_export.label": "インポート/エクスポート", @@ -10,32 +10,40 @@ "Quick_Access.import": "インポート", "Quick_Access.export": "エクスポート", "Quick_Access.import_menu.desc": "モデルをインポート", - "Quick_Access.import_pmx": "PMXインポート", + "Quick_Access.import_pmx": "PMXをインポート", "Quick_Access.import_pmx.desc": "MMD PMXモデルをインポート", - "Quick_Access.import_pmd": "PMDインポート", + "Quick_Access.import_pmd": "PMDをインポート", "Quick_Access.import_pmd.desc": "MMD PMDモデルをインポート", "Quick_Access.export_menu.label": "エクスポートメニュー", "Quick_Access.select_export.label": "エクスポート方法を選択", "Quick_Access.select_export_resonite.label": "Resonite", "Export.resonite.label": "Resoniteにエクスポート", - "Export.resonite.desc": "すべてのアニメーションとマテリアルを含むGLBをエクスポートします。アニメーションデータについては以下を参照してください:", + "Export.resonite.desc": "すべてのアニメーションとマテリアルを含むGLBをエクスポート。アニメーションデータについては以下を参照:", "Optimization.label": "最適化", "Optimization.options.label": "最適化オプション", "Optimization.combine_materials.label": "マテリアルを結合", - "Optimization.combine_materials.desc": "類似したマテリアルを結合してモデルを最適化します", + "Optimization.combine_materials.desc": "類似したマテリアルを結合してモデルを最適化", "Optimization.join_all_meshes.label": "すべてのメッシュを結合", - "Optimization.join_all_meshes.desc": "すべてのメッシュを1つに結合します", + "Optimization.join_all_meshes.desc": "すべてのメッシュを1つに結合", "Optimization.join_selected_meshes.label": "選択したメッシュを結合", - "Optimization.join_selected_meshes.desc": "現在選択されているすべてのメッシュを1つに結合します", + "Optimization.join_selected_meshes.desc": "現在選択されているすべてのメッシュを1つに結合", "Tools.tools_title.label": "ツール", "Tools.convert_to_resonite.label": "Resoniteに変換", - "Tools.convert_to_resonite.desc": "モデルのボーン名をResoniteと互換性のある名前に変換します", + "Tools.convert_to_resonite.desc": "モデルのボーン名をResoniteと互換性のある名前に変換", "Settings.label": "設定", "Settings.language.label": "言語", - "Settings.language.desc": "アドオンのUI言語を選択してください", - "Viseme.label": "ビセーム", + "Settings.language.desc": "アドオンのUIの言語を選択", + "VisemePanel.label": "ビセーム", "VisemePanel.error.noMesh": "メッシュが選択されていません", "VisemePanel.error.noShapekeys": "選択されたメッシュにシェイプキーがありません", - "VisemePanel.info.selectMesh": "ビセームを作成するメッシュを選択してください" + "VisemePanel.info.selectMesh": "ビセームを作成するメッシュを選択してください", + "VisemePanel.mouth_a.label": "口 A", + "VisemePanel.mouth_o.label": "口 O", + "VisemePanel.mouth_ch.label": "口 CH", + "AutoVisemeButton.label": "ビセームを作成", + "AutoVisemeButton.desc": "シェイプキーに基づいて自動的にビセームを作成", + "AutoVisemeButton.error.noShapekeys": "シェイプキーが見つかりません", + "AutoVisemeButton.error.selectShapekeys": "シェイプキーを選択してください", + "AutoVisemeButton.success": "ビセームが正常に作成されました" } } diff --git a/ui/viseme.py b/ui/viseme.py index cede8c5..8706cf4 100644 --- a/ui/viseme.py +++ b/ui/viseme.py @@ -4,7 +4,7 @@ from ..functions.translations import t @register_wrap class AvatarToolkitVisemePanel(bpy.types.Panel): - bl_label = t("Viseme.label") + bl_label = t("VisemePanel.label") bl_idname = "OBJECT_PT_avatar_toolkit_viseme" bl_space_type = 'VIEW_3D' bl_region_type = 'UI' @@ -20,9 +20,9 @@ class AvatarToolkitVisemePanel(bpy.types.Panel): # Check if the mesh has shape keys if mesh.data.shape_keys: - 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_ch", mesh.data.shape_keys, "key_blocks", text=t('Scene.mouth_ch.label')) + layout.prop_search(context.scene, "mouth_a", mesh.data.shape_keys, "key_blocks", text=t('VisemePanel.mouth_a.label')) + layout.prop_search(context.scene, "mouth_o", mesh.data.shape_keys, "key_blocks", text=t('VisemePanel.mouth_o.label')) + layout.prop_search(context.scene, "mouth_ch", mesh.data.shape_keys, "key_blocks", text=t('VisemePanel.mouth_ch.label')) layout.prop(context.scene, 'shape_intensity') From deaada347a9c048215a040715dbfbe4e3384a1eb Mon Sep 17 00:00:00 2001 From: Yusarina Date: Thu, 25 Jul 2024 01:42:34 +0100 Subject: [PATCH 5/6] Shapey Key Update. - Viesmes will now use selected armature. - New dropdown menu in the viseme UI so the user can select which mesh to create visemes on. - New helper function get_armature_meshes - Added a new check before we create the new visemes to see if any exist, if there do we will remove them and create the new ones. - fixed several issues and errors. --- core/common.py | 12 +++++++++--- core/properties.py | 8 +++++++- functions/viseme.py | 18 ++++++++++++++---- resources/translations/en_US.json | 9 ++++----- resources/translations/ja_JP.json | 7 +++---- ui/viseme.py | 31 ++++++++++++++++++------------- 6 files changed, 55 insertions(+), 30 deletions(-) diff --git a/core/common.py b/core/common.py index 8b2ce3f..8463734 100644 --- a/core/common.py +++ b/core/common.py @@ -96,6 +96,9 @@ def get_all_meshes(context: Context) -> List[Object]: return [obj for obj in bpy.data.objects if obj.type == 'MESH' and obj.parent == armature] return [] +def get_mesh_items(self, context): + return [(obj.name, obj.name, "") for obj in get_all_meshes(context)] + def open_web_after_delay_multi_threaded(delay: typing.Optional[float] = 1.0, url: typing.Union[str, typing.Any] = ""): thread = threading.Thread(target=open_web_after_delay,args=[delay,url],name="open_browser_thread") thread.start() @@ -128,6 +131,10 @@ def sort_shape_keys(mesh: Object) -> None: print("No shape keys found. Exiting sort function.") return + # Set the mesh as the active object + bpy.context.view_layer.objects.active = mesh + bpy.ops.object.mode_set(mode='OBJECT') + order = [ 'Basis', 'vrc.blink_left', @@ -176,13 +183,12 @@ def sort_shape_keys(mesh: Object) -> None: index = shape_keys.find(name) if index != i: print(f"Moving {name} from index {index} to {i}") - bpy.context.object.active_shape_key_index = index - while bpy.context.object.active_shape_key_index > i: + mesh.active_shape_key_index = index + while mesh.active_shape_key_index > i: bpy.ops.object.shape_key_move(type='UP') print("Shape key sorting completed.") - def get_shapekeys(mesh: Object, prefix: str = '') -> List[tuple]: if not has_shapekeys(mesh): return [] diff --git a/core/properties.py b/core/properties.py index 4943514..a79721f 100644 --- a/core/properties.py +++ b/core/properties.py @@ -2,7 +2,7 @@ import bpy from ..functions.translations import t, get_languages_list, update_language from ..core.addon_preferences import get_preference -from .common import get_armatures +from .common import get_armatures, get_mesh_items def register() -> None: default_language = get_preference("language", 0) @@ -14,6 +14,12 @@ def register() -> None: default=default_language, update=update_language ) + + bpy.types.Scene.selected_mesh = bpy.props.EnumProperty( + items=get_mesh_items, + name="Selected Mesh", + description="The currently selected mesh for viseme operations" + ) bpy.types.Scene.avatar_toolkit_language_changed = bpy.props.BoolProperty(default=False) diff --git a/functions/viseme.py b/functions/viseme.py index 7d60f00..9ba8849 100644 --- a/functions/viseme.py +++ b/functions/viseme.py @@ -3,6 +3,7 @@ from ..core import common from ..core.register import register_wrap from ..functions.translations import t from typing import List, Tuple +from ..core.common import get_selected_armature, is_valid_armature, get_all_meshes @register_wrap class AutoVisemeButton(bpy.types.Operator): @@ -13,15 +14,19 @@ class AutoVisemeButton(bpy.types.Operator): @classmethod def poll(cls, context: bpy.types.Context) -> bool: - return context.active_object and context.active_object.type == 'MESH' + armature = get_selected_armature(context) + return armature is not None and is_valid_armature(armature) and get_all_meshes(context) def execute(self, context: bpy.types.Context) -> set: print("Starting viseme creation...") - mesh = context.active_object + mesh = bpy.data.objects.get(context.scene.selected_mesh) if not mesh or not common.has_shapekeys(mesh): self.report({'ERROR'}, t('AutoVisemeButton.error.noShapekeys')) return {'CANCELLED'} + # Remove existing VRC shape keys + self.remove_existing_vrc_shapekeys(mesh) + shape_a = context.scene.mouth_a shape_o = context.scene.mouth_o shape_ch = context.scene.mouth_ch @@ -73,7 +78,7 @@ class AutoVisemeButton(bpy.types.Operator): # Create new viseme new_key = mesh.shape_key_add(name=viseme_name, from_mix=False) - new_key.value = 1.0 + new_key.value = 0.0 # Mix shapes for shape_name, value in shape_mix: @@ -85,4 +90,9 @@ class AutoVisemeButton(bpy.types.Operator): print(f" Viseme {viseme_name} created successfully.") - + def remove_existing_vrc_shapekeys(self, mesh: bpy.types.Object) -> None: + vrc_prefixes = ['vrc.v_', 'vrc.blink_', 'vrc.lowerlid_'] + shape_keys = mesh.data.shape_keys.key_blocks + for key in reversed(shape_keys): + if any(key.name.startswith(prefix) for prefix in vrc_prefixes): + mesh.shape_key_remove(key) diff --git a/resources/translations/en_US.json b/resources/translations/en_US.json index ba218a3..8cdcb40 100644 --- a/resources/translations/en_US.json +++ b/resources/translations/en_US.json @@ -31,9 +31,6 @@ "Tools.tools_title.label": "Tools", "Tools.convert_to_resonite.label": "Convert to Resonite", "Tools.convert_to_resonite.desc": "Converts bone names on a model to names compatable with Resonite", - "Settings.label": "Settings", - "Settings.language.label": "Language", - "Settings.language.desc": "Select the language for the addon's UI", "VisemePanel.label": "Visemes", "VisemePanel.error.noMesh": "No mesh selected", "VisemePanel.error.noShapekeys": "Selected mesh has no shape keys", @@ -45,8 +42,11 @@ "AutoVisemeButton.desc": "Create visemes automatically, based on shape keys", "AutoVisemeButton.error.noShapekeys": "No shape keys found", "AutoVisemeButton.error.selectShapekeys": "Please Select shape keys", - "AutoVisemeButton.success": "Visemes created successfully" + "AutoVisemeButton.success": "Visemes created successfully", "Settings.translation_restart_popup.label": "Translation Update", + "Settings.label": "Settings", + "Settings.language.label": "Language", + "Settings.language.desc": "Select the language for the addon's UI", "Settings.translation_restart_popup.description": "Information about translation updates", "Settings.translation_restart_popup.message1": "Some translations may not apply", "Settings.translation_restart_popup.message2": "until you restart Blender.", @@ -56,7 +56,6 @@ "Importing.importer_search_term":"https://search.brave.com/search?q=blender+{extension}+importer+addon&source=web", "Importer.export_resonite.label":"Export to Resonite", "Importer.export_resonite.desc":"Export to Resonite as a GLTF. Make sure your model is to scale in blender, and import as meters in Resonite.", - "Importer.export_vrchat.label":"Export to VRChat", "Importer.export_vrchat.desc":"Export to VRChat, may also work for ChilloutVR. Is similar to Cats export." } diff --git a/resources/translations/ja_JP.json b/resources/translations/ja_JP.json index 130cf0f..0e9763e 100644 --- a/resources/translations/ja_JP.json +++ b/resources/translations/ja_JP.json @@ -31,9 +31,6 @@ "Tools.tools_title.label": "ツール", "Tools.convert_to_resonite.label": "Resoniteに変換", "Tools.convert_to_resonite.desc": "モデルのボーン名をResoniteと互換性のある名前に変換", - "Settings.label": "設定", - "Settings.language.label": "言語", - "Settings.language.desc": "アドオンのUIの言語を選択", "VisemePanel.label": "ビセーム", "VisemePanel.error.noMesh": "メッシュが選択されていません", "VisemePanel.error.noShapekeys": "選択されたメッシュにシェイプキーがありません", @@ -45,7 +42,9 @@ "AutoVisemeButton.desc": "シェイプキーに基づいて自動的にビセームを作成", "AutoVisemeButton.error.noShapekeys": "シェイプキーが見つかりません", "AutoVisemeButton.error.selectShapekeys": "シェイプキーを選択してください", - "AutoVisemeButton.success": "ビセームが正常に作成されました" + "AutoVisemeButton.success": "ビセームが正常に作成されました", + "Settings.label": "設定", + "Settings.language.label": "言語", "Settings.language.desc": "アドオンのUI言語を選択してください", "Settings.translation_restart_popup.label": "翻訳の更新", "Settings.translation_restart_popup.description": "翻訳の更新に関する情報", diff --git a/ui/viseme.py b/ui/viseme.py index 8706cf4..d0ea8e3 100644 --- a/ui/viseme.py +++ b/ui/viseme.py @@ -1,6 +1,7 @@ import bpy from ..core.register import register_wrap from ..functions.translations import t +from ..core.common import get_selected_armature @register_wrap class AvatarToolkitVisemePanel(bpy.types.Panel): @@ -14,24 +15,28 @@ class AvatarToolkitVisemePanel(bpy.types.Panel): def draw(self, context: bpy.types.Context) -> None: 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 + armature = get_selected_armature(context) + if armature: + layout.prop(context.scene, "selected_mesh", text="Select Mesh") - # Check if the mesh has shape keys - if mesh.data.shape_keys: - layout.prop_search(context.scene, "mouth_a", mesh.data.shape_keys, "key_blocks", text=t('VisemePanel.mouth_a.label')) - layout.prop_search(context.scene, "mouth_o", mesh.data.shape_keys, "key_blocks", text=t('VisemePanel.mouth_o.label')) - layout.prop_search(context.scene, "mouth_ch", mesh.data.shape_keys, "key_blocks", text=t('VisemePanel.mouth_ch.label')) + mesh = bpy.data.objects.get(context.scene.selected_mesh) + if mesh and mesh.type == 'MESH': + if mesh.data.shape_keys: + layout.prop_search(context.scene, "mouth_a", mesh.data.shape_keys, "key_blocks", text=t('VisemePanel.mouth_a.label')) + layout.prop_search(context.scene, "mouth_o", mesh.data.shape_keys, "key_blocks", text=t('VisemePanel.mouth_o.label')) + layout.prop_search(context.scene, "mouth_ch", mesh.data.shape_keys, "key_blocks", text=t('VisemePanel.mouth_ch.label')) - 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.noShapekeys'), icon='ERROR') + layout.label(text=t('VisemePanel.error.selectMesh'), icon='INFO') else: - layout.label(text=t('VisemePanel.error.noMesh'), icon='ERROR') + layout.label(text=t('VisemePanel.error.noArmature'), icon='ERROR') - # Always show some information or options layout.separator() layout.label(text=t('VisemePanel.info.selectMesh')) + + From 018a080a4748e4c8579b876c4bca2ff115c92192 Mon Sep 17 00:00:00 2001 From: Yusarina Date: Thu, 25 Jul 2024 01:45:21 +0100 Subject: [PATCH 6/6] Update common.py --- core/common.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/core/common.py b/core/common.py index 8463734..2dd93ab 100644 --- a/core/common.py +++ b/core/common.py @@ -122,9 +122,6 @@ def duplicatebone(b: bpy.types.EditBone) -> bpy.types.EditBone: def has_shapekeys(mesh_obj: Object) -> bool: return mesh_obj.data.shape_keys is not None -def has_shapekeys(mesh_obj: Object) -> bool: - return mesh_obj.data.shape_keys is not None - def sort_shape_keys(mesh: Object) -> None: print("Starting shape key sorting...") if not has_shapekeys(mesh):