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
+36 -19
View File
@@ -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)]
+23 -10
View File
@@ -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
del bpy.types.Scene.shape_intensity
+23 -16
View File
@@ -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
+5 -1
View File
@@ -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"
}
}
+5 -1
View File
@@ -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": "ビセームを作成するメッシュを選択してください"
}
}
+20 -15
View File
@@ -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'))