From 909b1048826c312a5207ef1f3e5419d46aa9aa09 Mon Sep 17 00:00:00 2001 From: Yusarina Date: Fri, 27 Sep 2024 22:58:01 +0100 Subject: [PATCH 1/3] Move Join Meshes to Mesh Tools I see no point having two files here if we can just move join meshes into the mesh tools file. --- functions/join_meshes.py | 132 -------------------------------------- functions/mesh_tools.py | 133 ++++++++++++++++++++++++++++++++++++++- ui/mmd_options.py | 2 +- 3 files changed, 131 insertions(+), 136 deletions(-) delete mode 100644 functions/join_meshes.py diff --git a/functions/join_meshes.py b/functions/join_meshes.py deleted file mode 100644 index 09776ae..0000000 --- a/functions/join_meshes.py +++ /dev/null @@ -1,132 +0,0 @@ -import bpy -from typing import List, Optional, Set -from bpy.types import Operator, Context, Object -from ..core.register import register_wrap -from ..core.common import fix_uv_coordinates, get_selected_armature, is_valid_armature, select_current_armature, get_all_meshes, init_progress, update_progress, finish_progress -from ..functions.translations import t - -@register_wrap -class AvatarToolKit_OT_JoinAllMeshes(Operator): - bl_idname = "avatar_toolkit.join_all_meshes" - bl_label = t("Optimization.join_all_meshes.label") - bl_description = t("Optimization.join_all_meshes.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) - - def execute(self, context: Context) -> Set[str]: - try: - self.join_all_meshes(context) - return {'FINISHED'} - except Exception as e: - self.report({'ERROR'}, f"{t('Optimization.join_error')}: {str(e)}") - return {'CANCELLED'} - - def join_all_meshes(self, context: Context) -> None: - if not select_current_armature(context): - raise ValueError(t("Optimization.no_armature_selected")) - - armature = get_selected_armature(context) - bpy.ops.object.mode_set(mode='OBJECT') - bpy.ops.object.select_all(action='DESELECT') - - meshes: List[Object] = get_all_meshes(context) - if not meshes: - raise ValueError(t("Optimization.no_meshes_found")) - - init_progress(context, 5) # 5 steps in total - - update_progress(self, context, t("Optimization.selecting_meshes")) - for mesh in meshes: - mesh.select_set(True) - - if bpy.context.selected_objects: - bpy.context.view_layer.objects.active = bpy.context.selected_objects[0] - - update_progress(self, context, t("Optimization.joining_meshes")) - try: - bpy.ops.object.join() - except RuntimeError as e: - raise RuntimeError(f"{t('Optimization.join_operation_failed')}: {str(e)}") - - update_progress(self, context, t("Optimization.applying_transforms")) - try: - bpy.ops.object.transform_apply(location=True, rotation=True, scale=True) - except RuntimeError as e: - raise RuntimeError(f"{t('Optimization.transform_apply_failed')}: {str(e)}") - - update_progress(self, context, t("Optimization.fixing_uv_coordinates")) - fix_uv_coordinates(context) - - update_progress(self, context, t("Optimization.finalizing")) - bpy.ops.object.mode_set(mode='OBJECT') - bpy.ops.object.select_all(action='DESELECT') - self.report({'INFO'}, t("Optimization.meshes_joined")) - else: - raise ValueError(t("Optimization.no_mesh_selected")) - - context.view_layer.objects.active = armature - finish_progress(context) - -@register_wrap -class AvatarToolKit_OT_JoinSelectedMeshes(Operator): - bl_idname = "avatar_toolkit.join_selected_meshes" - bl_label = t("Optimization.join_selected_meshes.label") - bl_description = t("Optimization.join_selected_meshes.desc") - bl_options = {'REGISTER', 'UNDO'} - - @classmethod - def poll(cls, context: Context) -> bool: - return context.mode == 'OBJECT' and len([obj for obj in context.selected_objects if obj.type == 'MESH']) > 1 - - def execute(self, context: Context) -> Set[str]: - try: - self.join_selected_meshes(context) - return {'FINISHED'} - except Exception as e: - self.report({'ERROR'}, f"{t('Optimization.join_error')}: {str(e)}") - return {'CANCELLED'} - - def join_selected_meshes(self, context: Context) -> None: - selected_objects: List[Object] = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH'] - - if len(selected_objects) < 2: - raise ValueError(t("Optimization.select_at_least_two_meshes")) - - init_progress(context, 5) # 5 steps in total - - update_progress(self, context, t("Optimization.preparing_meshes")) - bpy.ops.object.mode_set(mode='OBJECT') - bpy.ops.object.select_all(action='DESELECT') - - update_progress(self, context, t("Optimization.selecting_meshes")) - for obj in selected_objects: - obj.select_set(True) - - if bpy.context.selected_objects: - bpy.context.view_layer.objects.active = bpy.context.selected_objects[0] - - update_progress(self, context, t("Optimization.joining_meshes")) - try: - bpy.ops.object.join() - except RuntimeError as e: - raise RuntimeError(f"{t('Optimization.join_operation_failed')}: {str(e)}") - - update_progress(self, context, t("Optimization.applying_transforms")) - try: - bpy.ops.object.transform_apply(location=True, rotation=True, scale=True) - except RuntimeError as e: - raise RuntimeError(f"{t('Optimization.transform_apply_failed')}: {str(e)}") - - update_progress(self, context, t("Optimization.fixing_uv_coordinates")) - fix_uv_coordinates(context) - bpy.ops.object.mode_set(mode='OBJECT') - bpy.ops.object.select_all(action='DESELECT') - self.report({'INFO'}, t("Optimization.selected_meshes_joined")) - else: - raise ValueError(t("Optimization.no_mesh_selected")) - - finish_progress(context) diff --git a/functions/mesh_tools.py b/functions/mesh_tools.py index 1ebd43f..426dc54 100644 --- a/functions/mesh_tools.py +++ b/functions/mesh_tools.py @@ -1,7 +1,8 @@ import numpy as np import bpy -from bpy.types import Context -from ..core.common import get_selected_armature, get_all_meshes, is_valid_armature, apply_shapekey_to_basis, has_shapekeys +from typing import List, Optional, Set +from bpy.types import Operator, Context, Object +from ..core.common import fix_uv_coordinates, get_selected_armature, get_all_meshes, is_valid_armature, apply_shapekey_to_basis, has_shapekeys, select_current_armature, init_progress, update_progress, finish_progress from ..functions.translations import t from ..core.register import register_wrap @@ -76,4 +77,130 @@ class AvatarToolkit_OT_ApplyShapeKey(bpy.types.Operator): return {'FINISHED'} else: self.report({'ERROR'}, t("Tools.apply_shape_key.error")) - return {'FINISHED'} \ No newline at end of file + return {'FINISHED'} + +@register_wrap +class AvatarToolKit_OT_JoinAllMeshes(Operator): + bl_idname = "avatar_toolkit.join_all_meshes" + bl_label = t("Optimization.join_all_meshes.label") + bl_description = t("Optimization.join_all_meshes.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) + + def execute(self, context: Context) -> Set[str]: + try: + self.join_all_meshes(context) + return {'FINISHED'} + except Exception as e: + self.report({'ERROR'}, f"{t('Optimization.join_error')}: {str(e)}") + return {'CANCELLED'} + + def join_all_meshes(self, context: Context) -> None: + if not select_current_armature(context): + raise ValueError(t("Optimization.no_armature_selected")) + + armature = get_selected_armature(context) + bpy.ops.object.mode_set(mode='OBJECT') + bpy.ops.object.select_all(action='DESELECT') + + meshes: List[Object] = get_all_meshes(context) + if not meshes: + raise ValueError(t("Optimization.no_meshes_found")) + + init_progress(context, 5) # 5 steps in total + + update_progress(self, context, t("Optimization.selecting_meshes")) + for mesh in meshes: + mesh.select_set(True) + + if bpy.context.selected_objects: + bpy.context.view_layer.objects.active = bpy.context.selected_objects[0] + + update_progress(self, context, t("Optimization.joining_meshes")) + try: + bpy.ops.object.join() + except RuntimeError as e: + raise RuntimeError(f"{t('Optimization.join_operation_failed')}: {str(e)}") + + update_progress(self, context, t("Optimization.applying_transforms")) + try: + bpy.ops.object.transform_apply(location=True, rotation=True, scale=True) + except RuntimeError as e: + raise RuntimeError(f"{t('Optimization.transform_apply_failed')}: {str(e)}") + + update_progress(self, context, t("Optimization.fixing_uv_coordinates")) + fix_uv_coordinates(context) + + update_progress(self, context, t("Optimization.finalizing")) + bpy.ops.object.mode_set(mode='OBJECT') + bpy.ops.object.select_all(action='DESELECT') + self.report({'INFO'}, t("Optimization.meshes_joined")) + else: + raise ValueError(t("Optimization.no_mesh_selected")) + + context.view_layer.objects.active = armature + finish_progress(context) + +@register_wrap +class AvatarToolKit_OT_JoinSelectedMeshes(Operator): + bl_idname = "avatar_toolkit.join_selected_meshes" + bl_label = t("Optimization.join_selected_meshes.label") + bl_description = t("Optimization.join_selected_meshes.desc") + bl_options = {'REGISTER', 'UNDO'} + + @classmethod + def poll(cls, context: Context) -> bool: + return context.mode == 'OBJECT' and len([obj for obj in context.selected_objects if obj.type == 'MESH']) > 1 + + def execute(self, context: Context) -> Set[str]: + try: + self.join_selected_meshes(context) + return {'FINISHED'} + except Exception as e: + self.report({'ERROR'}, f"{t('Optimization.join_error')}: {str(e)}") + return {'CANCELLED'} + + def join_selected_meshes(self, context: Context) -> None: + selected_objects: List[Object] = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH'] + + if len(selected_objects) < 2: + raise ValueError(t("Optimization.select_at_least_two_meshes")) + + init_progress(context, 5) # 5 steps in total + + update_progress(self, context, t("Optimization.preparing_meshes")) + bpy.ops.object.mode_set(mode='OBJECT') + bpy.ops.object.select_all(action='DESELECT') + + update_progress(self, context, t("Optimization.selecting_meshes")) + for obj in selected_objects: + obj.select_set(True) + + if bpy.context.selected_objects: + bpy.context.view_layer.objects.active = bpy.context.selected_objects[0] + + update_progress(self, context, t("Optimization.joining_meshes")) + try: + bpy.ops.object.join() + except RuntimeError as e: + raise RuntimeError(f"{t('Optimization.join_operation_failed')}: {str(e)}") + + update_progress(self, context, t("Optimization.applying_transforms")) + try: + bpy.ops.object.transform_apply(location=True, rotation=True, scale=True) + except RuntimeError as e: + raise RuntimeError(f"{t('Optimization.transform_apply_failed')}: {str(e)}") + + update_progress(self, context, t("Optimization.fixing_uv_coordinates")) + fix_uv_coordinates(context) + bpy.ops.object.mode_set(mode='OBJECT') + bpy.ops.object.select_all(action='DESELECT') + self.report({'INFO'}, t("Optimization.selected_meshes_joined")) + else: + raise ValueError(t("Optimization.no_mesh_selected")) + + finish_progress(context) \ No newline at end of file diff --git a/ui/mmd_options.py b/ui/mmd_options.py index 1b609b1..d3e1eb1 100644 --- a/ui/mmd_options.py +++ b/ui/mmd_options.py @@ -3,7 +3,7 @@ from ..core.register import register_wrap from .panel import AvatarToolKit_PT_AvatarToolkitPanel, CATEGORY_NAME from ..functions.translations import t from ..functions.mmd_functions import * -from ..functions.join_meshes import AvatarToolKit_OT_JoinAllMeshes +from ..functions.mesh_tools import AvatarToolKit_OT_JoinAllMeshes from ..functions.combine_materials import AvatarToolKit_OT_CombineMaterials from ..functions.additional_tools import AvatarToolKit_OT_ApplyTransforms From 503ba52b9bedc1cbd619001ad4d1dd0ee9c8999b Mon Sep 17 00:00:00 2001 From: Yusarina Date: Fri, 27 Sep 2024 22:59:45 +0100 Subject: [PATCH 2/3] Move Separate By to Additional Tools Move Separate BY to Additional tools, makes no sense having these smaller functions in it's own file. --- functions/additional_tools.py | 41 ++++++++++++++++++++++++++++++++ functions/seperate_by.py | 44 ----------------------------------- ui/tools.py | 3 +-- 3 files changed, 42 insertions(+), 46 deletions(-) delete mode 100644 functions/seperate_by.py diff --git a/functions/additional_tools.py b/functions/additional_tools.py index 241cac1..0979207 100644 --- a/functions/additional_tools.py +++ b/functions/additional_tools.py @@ -119,3 +119,44 @@ class AvatarToolKit_OT_DeleteBoneConstraints(Operator): self.report({'INFO'}, t("Tools.delete_bone_constraints.success").format(constraints_removed=constraints_removed)) return {'FINISHED'} + +@register_wrap +class AvatarToolKit_OT_SeparateByMaterials(Operator): + bl_idname = "avatar_toolkit.separate_by_materials" + bl_label = t("Tools.separate_by_materials.label") + bl_description = t("Tools.separate_by_materials.desc") + bl_options = {'REGISTER', 'UNDO'} + + @classmethod + def poll(cls, context: Context) -> bool: + return context.active_object and context.active_object.type == 'MESH' + + def execute(self, context: Context) -> set[str]: + obj = context.active_object + bpy.ops.object.mode_set(mode='EDIT') + bpy.ops.mesh.select_all(action='SELECT') + bpy.ops.mesh.separate(type='MATERIAL') + bpy.ops.object.mode_set(mode='OBJECT') + self.report({'INFO'}, t("Tools.separate_by_materials.success")) + return {'FINISHED'} + +@register_wrap +class AvatarToolKit_OT_SeparateByLooseParts(Operator): + bl_idname = "avatar_toolkit.separate_by_loose_parts" + bl_label = t("Tools.separate_by_loose_parts.label") + bl_description = t("Tools.separate_by_loose_parts.desc") + bl_options = {'REGISTER', 'UNDO'} + + @classmethod + def poll(cls, context: Context) -> bool: + return context.active_object and context.active_object.type == 'MESH' + + def execute(self, context: Context) -> set[str]: + obj = context.active_object + bpy.ops.object.mode_set(mode='EDIT') + bpy.ops.mesh.select_all(action='SELECT') + bpy.ops.mesh.separate(type='LOOSE') + bpy.ops.object.mode_set(mode='OBJECT') + self.report({'INFO'}, t("Tools.separate_by_loose_parts.success")) + return {'FINISHED'} + diff --git a/functions/seperate_by.py b/functions/seperate_by.py deleted file mode 100644 index 8902f8c..0000000 --- a/functions/seperate_by.py +++ /dev/null @@ -1,44 +0,0 @@ -import bpy -from bpy.types import Context, Operator -from ..core.register import register_wrap -from ..functions.translations import t - -@register_wrap -class AvatarToolKit_OT_SeparateByMaterials(Operator): - bl_idname = "avatar_toolkit.separate_by_materials" - bl_label = t("Tools.separate_by_materials.label") - bl_description = t("Tools.separate_by_materials.desc") - bl_options = {'REGISTER', 'UNDO'} - - @classmethod - def poll(cls, context: Context) -> bool: - return context.active_object and context.active_object.type == 'MESH' - - def execute(self, context: Context) -> set[str]: - obj = context.active_object - bpy.ops.object.mode_set(mode='EDIT') - bpy.ops.mesh.select_all(action='SELECT') - bpy.ops.mesh.separate(type='MATERIAL') - bpy.ops.object.mode_set(mode='OBJECT') - self.report({'INFO'}, t("Tools.separate_by_materials.success")) - return {'FINISHED'} - -@register_wrap -class AvatarToolKit_OT_SeparateByLooseParts(Operator): - bl_idname = "avatar_toolkit.separate_by_loose_parts" - bl_label = t("Tools.separate_by_loose_parts.label") - bl_description = t("Tools.separate_by_loose_parts.desc") - bl_options = {'REGISTER', 'UNDO'} - - @classmethod - def poll(cls, context: Context) -> bool: - return context.active_object and context.active_object.type == 'MESH' - - def execute(self, context: Context) -> set[str]: - obj = context.active_object - bpy.ops.object.mode_set(mode='EDIT') - bpy.ops.mesh.select_all(action='SELECT') - bpy.ops.mesh.separate(type='LOOSE') - bpy.ops.object.mode_set(mode='OBJECT') - self.report({'INFO'}, t("Tools.separate_by_loose_parts.success")) - return {'FINISHED'} diff --git a/ui/tools.py b/ui/tools.py index e2c0eff..973c46f 100644 --- a/ui/tools.py +++ b/ui/tools.py @@ -7,8 +7,7 @@ from ..functions.resonite_functions import AvatarToolKit_OT_ConvertToResonite from ..functions.translations import t from ..core.common import get_selected_armature from ..functions.mesh_tools import AvatarToolkit_OT_RemoveUnusedShapekeys -from ..functions.seperate_by import AvatarToolKit_OT_SeparateByMaterials, AvatarToolKit_OT_SeparateByLooseParts -from ..functions.additional_tools import AvatarToolKit_OT_ApplyTransforms, AvatarToolKit_OT_ConnectBones, AvatarToolKit_OT_DeleteBoneConstraints +from ..functions.additional_tools import AvatarToolKit_OT_ApplyTransforms, AvatarToolKit_OT_ConnectBones, AvatarToolKit_OT_DeleteBoneConstraints, AvatarToolKit_OT_SeparateByMaterials, AvatarToolKit_OT_SeparateByLooseParts from ..functions.armature_modifying import AvatarToolkit_OT_RemoveZeroWeightBones, AvatarToolkit_OT_MergeBonesToActive, AvatarToolkit_OT_MergeBonesToParents from ..functions.rigify_functions import AvatarToolKit_OT_ConvertRigifyToUnity From bb92e5053082aa61db33f3d081dd3cf9ae1d9f50 Mon Sep 17 00:00:00 2001 From: Yusarina Date: Fri, 27 Sep 2024 23:00:16 +0100 Subject: [PATCH 3/3] Small Fix --- ui/optimization.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/optimization.py b/ui/optimization.py index 3e19c9f..657ecc9 100644 --- a/ui/optimization.py +++ b/ui/optimization.py @@ -4,7 +4,7 @@ from .panel import AvatarToolKit_PT_AvatarToolkitPanel, CATEGORY_NAME from ..functions.translations import t from ..functions.remove_doubles_safely import AvatarToolKit_OT_RemoveDoublesSafely, AvatarToolKit_OT_RemoveDoublesSafelyAdvanced from ..core.common import get_selected_armature -from ..functions.join_meshes import AvatarToolKit_OT_JoinAllMeshes, AvatarToolKit_OT_JoinSelectedMeshes +from ..functions.mesh_tools import AvatarToolKit_OT_JoinAllMeshes, AvatarToolKit_OT_JoinSelectedMeshes from ..functions.combine_materials import AvatarToolKit_OT_CombineMaterials @register_wrap