From 76046f7c6d99b5aef70d4636055bb26b53bbb0e8 Mon Sep 17 00:00:00 2001 From: Yusarina Date: Wed, 24 Jul 2024 00:27:14 +0100 Subject: [PATCH] Armature Selection Improvements. - Added a check to make sure Armature is valid. - Added a helper to select the current armature selected in armature selection. - Added a helper to get all meshes. - Updated all current functions to work with the system. --- core/common.py | 26 +++++++++++++++++++++- functions/combine_materials.py | 35 ++++++++++++++++-------------- functions/join_meshes.py | 13 ++++++----- functions/remove_doubles_safely.py | 11 +++++----- functions/resonite_functions.py | 5 +++-- ui/quick_access.py | 1 - 6 files changed, 61 insertions(+), 30 deletions(-) diff --git a/core/common.py b/core/common.py index 4ad2f9c..1d0d1b9 100644 --- a/core/common.py +++ b/core/common.py @@ -63,9 +63,33 @@ def get_armatures(self, context): def get_selected_armature(context): if context.scene.selected_armature: - return bpy.data.objects.get(context.scene.selected_armature) + armature = bpy.data.objects.get(context.scene.selected_armature) + if is_valid_armature(armature): + return armature return None def set_selected_armature(context, armature): context.scene.selected_armature = armature.name if armature else "" +def is_valid_armature(armature: Object) -> bool: + if not armature or armature.type != 'ARMATURE': + return False + if not armature.data or not armature.data.bones: + return False + return True + +def select_current_armature(context): + armature = get_selected_armature(context) + if armature: + bpy.ops.object.select_all(action='DESELECT') + armature.select_set(True) + context.view_layer.objects.active = armature + return True + return False + +def get_all_meshes(context: Context) -> List[Object]: + armature = get_selected_armature(context) + if armature and is_valid_armature(armature): + return [obj for obj in bpy.data.objects if obj.type == 'MESH' and obj.parent == armature] + return [] + diff --git a/functions/combine_materials.py b/functions/combine_materials.py index 5a6b0af..c9aabfc 100644 --- a/functions/combine_materials.py +++ b/functions/combine_materials.py @@ -2,7 +2,7 @@ import bpy import re from typing import List, Tuple, Optional from bpy.types import Material, Operator, Context, Object -from ..core.common import clean_material_names, get_selected_armature +from ..core.common import clean_material_names, get_selected_armature, is_valid_armature, get_all_meshes from ..core.register import register_wrap from ..functions.translations import t @@ -65,51 +65,54 @@ class CombineMaterials(Operator): @classmethod def poll(cls, context: Context) -> bool: - return context.active_object is not None and get_selected_armature(context) is not None + armature = get_selected_armature(context) + return armature is not None and is_valid_armature(armature) def execute(self, context: Context) -> set: - bpy.ops.object.mode_set(mode='OBJECT') - armature = get_selected_armature(context) if not armature: self.report({'WARNING'}, "No armature selected") return {'CANCELLED'} - meshes: List[Object] = [obj for obj in bpy.data.objects if obj.type == 'MESH' and 'Armature' in obj.modifiers and obj.modifiers['Armature'].object == armature] + context.view_layer.objects.active = armature + bpy.ops.object.mode_set(mode='OBJECT') + + meshes = get_all_meshes(context) if not meshes: self.report({'WARNING'}, "No meshes found for the selected armature") return {'CANCELLED'} - bpy.ops.object.mode_set(mode='OBJECT') self.consolidate_materials(meshes) self.remove_unused_materials() self.cleanmatslots() self.clean_material_names() - bpy.ops.object.mode_set(mode='OBJECT') - bpy.context.view_layer.objects.active = armature return {'FINISHED'} - def consolidate_materials(self, objects: List[Object]) -> None: + def consolidate_materials(self, meshes: List[Object]) -> None: mat_mapping: dict = {} num_combined: int = 0 - for ob in objects: - for slot in ob.material_slots: + for mesh in meshes: + for slot in mesh.material_slots: mat: Optional[Material] = slot.material if mat: base_name: str = get_base_name(mat.name) if base_name in mat_mapping: base_mat: Material = mat_mapping[base_name] - if materials_match(base_mat, mat): - consolidate_textures(base_mat, mat) - num_combined += 1 - slot.material = base_mat + try: + if materials_match(base_mat, mat): + consolidate_textures(base_mat, mat) + num_combined += 1 + slot.material = base_mat + except AttributeError: + # Skip this material if there's an attribute mismatch + continue else: mat_mapping[base_name] = mat report_consolidated(self, num_combined) - + def remove_unused_materials(self) -> None: for mat in bpy.data.materials: if not any(obj for obj in bpy.data.objects if obj.material_slots and mat.name in obj.material_slots): diff --git a/functions/join_meshes.py b/functions/join_meshes.py index 495c037..2eb8c12 100644 --- a/functions/join_meshes.py +++ b/functions/join_meshes.py @@ -2,7 +2,7 @@ import bpy from typing import List, Optional from bpy.types import Operator, Context, Object from ..core.register import register_wrap -from ..core.common import fix_uv_coordinates, get_selected_armature +from ..core.common import fix_uv_coordinates, get_selected_armature, is_valid_armature, select_current_armature, get_all_meshes from ..functions.translations import t @register_wrap @@ -14,22 +14,23 @@ class JoinAllMeshes(Operator): @classmethod def poll(cls, context: Context) -> bool: - return context.mode == 'OBJECT' and get_selected_armature(context) is not None + armature = get_selected_armature(context) + return armature is not None and is_valid_armature(armature) def execute(self, context: Context) -> set: self.join_all_meshes(context) return {'FINISHED'} def join_all_meshes(self, context: Context) -> None: - armature = get_selected_armature(context) - if not armature: + if not select_current_armature(context): self.report({'WARNING'}, "No armature selected") return + armature = get_selected_armature(context) bpy.ops.object.mode_set(mode='OBJECT') bpy.ops.object.select_all(action='DESELECT') - meshes: List[Object] = [obj for obj in bpy.data.objects if obj.type == 'MESH' and 'Armature' in obj.modifiers and obj.modifiers['Armature'].object == armature] + meshes: List[Object] = get_all_meshes(context) for mesh in meshes: mesh.select_set(True) @@ -44,6 +45,8 @@ class JoinAllMeshes(Operator): else: self.report({'WARNING'}, "No mesh objects selected") + context.view_layer.objects.active = armature + @register_wrap class JoinSelectedMeshes(Operator): bl_idname = "avatar_toolkit.join_selected_meshes" diff --git a/functions/remove_doubles_safely.py b/functions/remove_doubles_safely.py index 8d0e2c5..1631b68 100644 --- a/functions/remove_doubles_safely.py +++ b/functions/remove_doubles_safely.py @@ -5,7 +5,7 @@ import re from typing import List, Tuple, Optional, TypedDict from bpy.types import Material, Operator, Context, Object from ..core.register import register_wrap -from ..core.common import get_selected_armature +from ..core.common import get_selected_armature, is_valid_armature, select_current_armature, get_all_meshes class meshEntry(TypedDict): @@ -23,17 +23,18 @@ class RemoveDoublesSafely(Operator): @classmethod def poll(cls, context: Context) -> bool: - return context.mode == 'OBJECT' and get_selected_armature(context) is not None + armature = get_selected_armature(context) + return armature is not None and is_valid_armature(armature) def execute(self, context: Context) -> set: - armature = get_selected_armature(context) - if not armature: + if not select_current_armature(context): self.report({'WARNING'}, "No armature selected") return {'CANCELLED'} + armature = get_selected_armature(context) bpy.ops.object.mode_set(mode='OBJECT') bpy.ops.object.select_all(action='DESELECT') - objects: List[Object] = [obj for obj in armature.children if obj.type == 'MESH'] + objects: List[Object] = get_all_meshes(context) for mesh in objects: if mesh.data.name not in [stored_object["mesh"].data.name for stored_object in self.objects_to_do]: diff --git a/functions/resonite_functions.py b/functions/resonite_functions.py index c3a049f..2b6a005 100644 --- a/functions/resonite_functions.py +++ b/functions/resonite_functions.py @@ -4,7 +4,7 @@ from typing import List, Optional import re from bpy.types import Operator, Context, Object from ..core.dictionaries import bone_names -from ..core.common import get_selected_armature, simplify_bonename +from ..core.common import get_selected_armature, simplify_bonename, is_valid_armature from ..functions.translations import t @register_wrap @@ -16,7 +16,8 @@ class ConvertToResonite(Operator): @classmethod def poll(cls, context: Context) -> bool: - return get_selected_armature(context) is not None + armature = get_selected_armature(context) + return armature is not None and is_valid_armature(armature) def execute(self, context: Context) -> set: armature = get_selected_armature(context) diff --git a/ui/quick_access.py b/ui/quick_access.py index e221085..6de9021 100644 --- a/ui/quick_access.py +++ b/ui/quick_access.py @@ -22,7 +22,6 @@ class AvatarToolkitQuickAccessPanel(bpy.types.Panel): layout = self.layout layout.label(text=t("Quick_Access.options")) - # Add Armature Selection layout.prop(context.scene, "selected_armature", text="Select Armature") row = layout.row()