From 06c7cff4b7a7823a8006a9d1824ededadfa5d354 Mon Sep 17 00:00:00 2001 From: Yusarina Date: Mon, 22 Jul 2024 23:13:10 +0100 Subject: [PATCH 1/6] Basic Start of Armature Selection --- core/common.py | 12 ++++++++++ core/properties.py | 10 +++++++++ functions/combine_materials.py | 9 ++++---- functions/join_meshes.py | 18 +++++++-------- functions/remove_doubles_safely.py | 35 +++++++----------------------- functions/resonite_functions.py | 16 ++++++-------- ui/optimization.py | 34 +++++++++++++++-------------- ui/quick_access.py | 6 +++-- ui/tools.py | 20 +++++++++++------ 9 files changed, 86 insertions(+), 74 deletions(-) diff --git a/core/common.py b/core/common.py index 6e08f93..4ad2f9c 100644 --- a/core/common.py +++ b/core/common.py @@ -57,3 +57,15 @@ 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 get_armatures(self, context): + return [(obj.name, obj.name, "") for obj in bpy.data.objects if obj.type == 'ARMATURE'] + +def get_selected_armature(context): + if context.scene.selected_armature: + return bpy.data.objects.get(context.scene.selected_armature) + return None + +def set_selected_armature(context, armature): + context.scene.selected_armature = armature.name if armature else "" + diff --git a/core/properties.py b/core/properties.py index 9841212..801356e 100644 --- a/core/properties.py +++ b/core/properties.py @@ -1,6 +1,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 def register(): default_language = get_preference("language", 0) @@ -15,9 +16,18 @@ def register(): bpy.types.Scene.avatar_toolkit_language_changed = bpy.props.BoolProperty(default=False) + bpy.types.Scene.selected_armature = bpy.props.EnumProperty( + items=get_armatures, + name="Selected Armature", + description="The currently selected armature for Avatar Toolkit operations" + ) + def unregister(): if hasattr(bpy.types.Scene, "avatar_toolkit_language"): del bpy.types.Scene.avatar_toolkit_language if hasattr(bpy.types.Scene, "avatar_toolkit_language_changed"): del bpy.types.Scene.avatar_toolkit_language_changed + + if hasattr(bpy.types.Scene, "selected_armature"): + del bpy.types.Scene.selected_armature diff --git a/functions/combine_materials.py b/functions/combine_materials.py index d2fa56a..5a6b0af 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 +from ..core.common import clean_material_names, get_selected_armature from ..core.register import register_wrap from ..functions.translations import t @@ -65,17 +65,19 @@ class CombineMaterials(Operator): @classmethod def poll(cls, context: Context) -> bool: - return context.active_object is not None + return context.active_object is not None and get_selected_armature(context) is not None def execute(self, context: Context) -> set: bpy.ops.object.mode_set(mode='OBJECT') - armature: Optional[Object] = next((obj for obj in bpy.data.objects if obj.type == 'ARMATURE'), None) + 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] if not meshes: + self.report({'WARNING'}, "No meshes found for the selected armature") return {'CANCELLED'} bpy.ops.object.mode_set(mode='OBJECT') @@ -125,4 +127,3 @@ class CombineMaterials(Operator): for obj in bpy.data.objects: if obj.type == 'MESH': clean_material_names(obj) - diff --git a/functions/join_meshes.py b/functions/join_meshes.py index 195870e..495c037 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 +from ..core.common import fix_uv_coordinates, get_selected_armature from ..functions.translations import t @register_wrap @@ -14,21 +14,22 @@ class JoinAllMeshes(Operator): @classmethod def poll(cls, context: Context) -> bool: - return context.mode == 'OBJECT' + return context.mode == 'OBJECT' and get_selected_armature(context) is not None def execute(self, context: Context) -> set: self.join_all_meshes(context) return {'FINISHED'} def join_all_meshes(self, context: Context) -> None: - if not bpy.data.objects: - self.report({'INFO'}, "No objects in the scene") + armature = get_selected_armature(context) + if not armature: + self.report({'WARNING'}, "No armature selected") return 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'] + 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] for mesh in meshes: mesh.select_set(True) @@ -52,7 +53,7 @@ class JoinSelectedMeshes(Operator): @classmethod def poll(cls, context: Context) -> bool: - return context.mode == 'OBJECT' + return context.mode == 'OBJECT' and len([obj for obj in context.selected_objects if obj.type == 'MESH']) > 1 def execute(self, context: Context) -> set: self.join_selected_meshes(context) @@ -61,8 +62,8 @@ class JoinSelectedMeshes(Operator): 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 not selected_objects: - self.report({'WARNING'}, "No mesh objects selected") + if len(selected_objects) < 2: + self.report({'WARNING'}, "Please select at least two mesh objects") return bpy.ops.object.mode_set(mode='OBJECT') @@ -81,4 +82,3 @@ class JoinSelectedMeshes(Operator): self.report({'INFO'}, "Selected meshes joined successfully") else: self.report({'WARNING'}, "No mesh objects selected") - diff --git a/functions/remove_doubles_safely.py b/functions/remove_doubles_safely.py index 99850ae..8d0e2c5 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_armature +from ..core.common import get_selected_armature class meshEntry(TypedDict): @@ -23,20 +23,19 @@ class RemoveDoublesSafely(Operator): @classmethod def poll(cls, context: Context) -> bool: - return context.mode == 'OBJECT' + return context.mode == 'OBJECT' and get_selected_armature(context) is not None def execute(self, context: Context) -> set: - if not bpy.data.objects: - self.report({'INFO'}, "No objects in the scene") - return + armature = get_selected_armature(context) + if not armature: + self.report({'WARNING'}, "No armature selected") + return {'CANCELLED'} bpy.ops.object.mode_set(mode='OBJECT') bpy.ops.object.select_all(action='DESELECT') - objects: List[Object] = get_armature(context).children if get_armature(context) else context.view_layer.objects + objects: List[Object] = [obj for obj in armature.children if obj.type == 'MESH'] - meshes: List[Object] = [obj for obj in objects if obj.type == 'MESH'] - - for mesh in meshes: + for mesh in objects: if mesh.data.name not in [stored_object["mesh"].data.name for stored_object in self.objects_to_do]: mesh_shapekeys = {"mesh":mesh,"shapekeys":[]} mesh_data: bpy.types.Mesh = mesh.data @@ -45,12 +44,10 @@ class RemoveDoublesSafely(Operator): for shape in mesh_data.shape_keys.key_blocks: mesh_shapekeys["shapekeys"].append(shape.name) self.objects_to_do.append(mesh_shapekeys) - return {'FINISHED'} def invoke(self, context: Context, event: bpy.types.Event) -> set: - self.execute(context) context.window_manager.modal_handler_add(self) return {'RUNNING_MODAL'} @@ -62,7 +59,6 @@ class RemoveDoublesSafely(Operator): mesh_data: bpy.types.Mesh = mesh["mesh"].data bpy.ops.object.mode_set(mode='EDIT') - bpy.ops.object.mode_set(mode='OBJECT') for index, point in enumerate(mesh["mesh"].active_shape_key.points): if point.co.xyz != mesh_data.shape_keys.key_blocks[0].points[index].co.xyz: @@ -70,18 +66,10 @@ class RemoveDoublesSafely(Operator): print("shapekey has a moved vertex at index \""+str(index)+"\", excluding from double merging!") bpy.ops.object.mode_set(mode='EDIT') - - - bpy.ops.object.mode_set(mode='OBJECT') mesh["mesh"].select_set(False) - def modal(self, context: Context, event: bpy.types.Event) -> set: - - - - if len(self.objects_to_do) > 0: mesh = self.objects_to_do[0] mesh_data: bpy.types.Mesh = mesh["mesh"].data @@ -131,10 +119,3 @@ class RemoveDoublesSafely(Operator): return {'FINISHED'} return {'RUNNING_MODAL'} - - - - - - - diff --git a/functions/resonite_functions.py b/functions/resonite_functions.py index 74a80be..c3a049f 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_armature, simplify_bonename +from ..core.common import get_selected_armature, simplify_bonename from ..functions.translations import t @register_wrap @@ -16,12 +16,13 @@ class ConvertToResonite(Operator): @classmethod def poll(cls, context: Context) -> bool: - if not get_armature(context): - return False - return True + return get_selected_armature(context) is not None def execute(self, context: Context) -> set: - armature = get_armature(context) + armature = get_selected_armature(context) + if not armature: + self.report({'WARNING'}, "No armature selected") + return {'CANCELLED'} translate_bone_fails = 0 untranslated_bones = set() @@ -89,8 +90,6 @@ class ConvertToResonite(Operator): 'thumb_3_r': "thumb3.R" } - - context.view_layer.objects.active = armature bpy.ops.object.mode_set(mode='EDIT') @@ -111,5 +110,4 @@ class ConvertToResonite(Operator): else: self.report({'INFO'}, "Successfully translated all bones to humanoid names") - - return {'FINISHED'} \ No newline at end of file + return {'FINISHED'} diff --git a/ui/optimization.py b/ui/optimization.py index bcad49c..1726e22 100644 --- a/ui/optimization.py +++ b/ui/optimization.py @@ -2,6 +2,7 @@ import bpy from ..core.register import register_wrap from .panel import AvatarToolkitPanel from ..functions.translations import t +from ..core.common import get_selected_armature @register_wrap class AvatarToolkitOptimizationPanel(bpy.types.Panel): @@ -14,20 +15,21 @@ class AvatarToolkitOptimizationPanel(bpy.types.Panel): def draw(self, context): layout = self.layout - layout.label(text=t("Optimization.options.label")) + armature = get_selected_armature(context) - row = layout.row() - row.scale_y = 1.2 - row.operator("avatar_toolkit.combine_materials", text=t("Optimization.combine_materials.label")) - - - layout.separator(factor=0.5) - - row = layout.row(align=True) - row.scale_y = 1.2 - row.operator("avatar_toolkit.join_all_meshes", text=t("Optimization.join_all_meshes.label")) - row.operator("avatar_toolkit.join_selected_meshes", text=t("Optimization.join_selected_meshes.label")) - row.operator("avatar_toolkit.remove_doubles_safely", text="Remove Doubles Safely") - - # Add optimization options here - + if armature: + layout.label(text=t("Optimization.options.label")) + + row = layout.row() + row.scale_y = 1.2 + row.operator("avatar_toolkit.combine_materials", text=t("Optimization.combine_materials.label")) + + layout.separator(factor=0.5) + + row = layout.row(align=True) + row.scale_y = 1.2 + row.operator("avatar_toolkit.join_all_meshes", text=t("Optimization.join_all_meshes.label")) + row.operator("avatar_toolkit.join_selected_meshes", text=t("Optimization.join_selected_meshes.label")) + row.operator("avatar_toolkit.remove_doubles_safely", text="Remove Doubles Safely") + else: + layout.label(text="Please select an armature in Quick Access") diff --git a/ui/quick_access.py b/ui/quick_access.py index 81b9bee..e221085 100644 --- a/ui/quick_access.py +++ b/ui/quick_access.py @@ -7,6 +7,7 @@ from ..functions.translations import t from ..core.import_pmx import import_pmx from ..core.import_pmd import import_pmd from ..core.importer import import_fbx +from ..core.common import get_selected_armature, set_selected_armature @register_wrap class AvatarToolkitQuickAccessPanel(bpy.types.Panel): @@ -21,6 +22,9 @@ 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() row.label(text=t("Quick_Access.import_export.label"), icon='IMPORT') @@ -129,5 +133,3 @@ class AVATAR_TOOLKIT_OT_export_fbx(bpy.types.Operator): def execute(self, context): bpy.ops.export_scene.fbx('INVOKE_DEFAULT') return {'FINISHED'} - - diff --git a/ui/tools.py b/ui/tools.py index d25a4be..0e37f96 100644 --- a/ui/tools.py +++ b/ui/tools.py @@ -3,6 +3,7 @@ from ..core.register import register_wrap from .panel import AvatarToolkitPanel from bpy.types import Context from ..functions.translations import t +from ..core.common import get_selected_armature @register_wrap class AvatarToolkitToolsPanel(bpy.types.Panel): @@ -15,11 +16,16 @@ class AvatarToolkitToolsPanel(bpy.types.Panel): def draw(self, context: Context): layout = self.layout - layout.label(text=t("Tools.tools_title.label")) - layout.separator(factor=0.5) + armature = get_selected_armature(context) + + if armature: + layout.label(text=t("Tools.tools_title.label")) + layout.separator(factor=0.5) - row = layout.row(align=True) - row.scale_y = 1.5 - row.operator("avatar_toolkit.convert_to_resonite", text=t("Tools.convert_to_resonite.label")) - row = layout.row(align=True) - row.operator("avatar_toolkit.remove_doubles_safely", text="Remove Doubles Safely") + row = layout.row(align=True) + row.scale_y = 1.5 + row.operator("avatar_toolkit.convert_to_resonite", text=t("Tools.convert_to_resonite.label")) + row = layout.row(align=True) + row.operator("avatar_toolkit.remove_doubles_safely", text="Remove Doubles Safely") + else: + layout.label(text="Please select an armature in Quick Access") From 76046f7c6d99b5aef70d4636055bb26b53bbb0e8 Mon Sep 17 00:00:00 2001 From: Yusarina Date: Wed, 24 Jul 2024 00:27:14 +0100 Subject: [PATCH 2/6] 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() From a8d7cd303289de2d98c0366cedecaeca31a56bad Mon Sep 17 00:00:00 2001 From: Yusarina Date: Wed, 24 Jul 2024 00:52:04 +0100 Subject: [PATCH 3/6] Typing --- core/common.py | 15 +++++++-------- functions/combine_materials.py | 6 +++--- functions/join_meshes.py | 7 ++++--- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/core/common.py b/core/common.py index 1d0d1b9..589e2b4 100644 --- a/core/common.py +++ b/core/common.py @@ -2,7 +2,7 @@ import bpy import numpy as np from .dictionaries import bone_names -from typing import List, Optional +from typing import List, Optional, Tuple from bpy.types import Object, ShapeKey, Mesh, Context from functools import lru_cache @@ -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,17 +58,17 @@ 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 get_armatures(self, context): +def get_armatures(self, context: Context) -> List[Tuple[str, str, str]]: return [(obj.name, obj.name, "") for obj in bpy.data.objects if obj.type == 'ARMATURE'] -def get_selected_armature(context): +def get_selected_armature(context: Context) -> Optional[Object]: if 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): +def set_selected_armature(context: Context, armature: Optional[Object]) -> None: context.scene.selected_armature = armature.name if armature else "" def is_valid_armature(armature: Object) -> bool: @@ -78,7 +78,7 @@ def is_valid_armature(armature: Object) -> bool: return False return True -def select_current_armature(context): +def select_current_armature(context: Context) -> bool: armature = get_selected_armature(context) if armature: bpy.ops.object.select_all(action='DESELECT') @@ -92,4 +92,3 @@ def get_all_meshes(context: Context) -> List[Object]: 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 c9aabfc..473e881 100644 --- a/functions/combine_materials.py +++ b/functions/combine_materials.py @@ -1,6 +1,6 @@ import bpy import re -from typing import List, Tuple, Optional +from typing import List, Tuple, Optional, Set from bpy.types import Material, Operator, Context, Object from ..core.common import clean_material_names, get_selected_armature, is_valid_armature, get_all_meshes from ..core.register import register_wrap @@ -68,7 +68,7 @@ class CombineMaterials(Operator): armature = get_selected_armature(context) return armature is not None and is_valid_armature(armature) - def execute(self, context: Context) -> set: + def execute(self, context: Context) -> Set[str]: armature = get_selected_armature(context) if not armature: self.report({'WARNING'}, "No armature selected") @@ -90,7 +90,7 @@ class CombineMaterials(Operator): return {'FINISHED'} def consolidate_materials(self, meshes: List[Object]) -> None: - mat_mapping: dict = {} + mat_mapping: Dict[str, Material] = {} num_combined: int = 0 for mesh in meshes: for slot in mesh.material_slots: diff --git a/functions/join_meshes.py b/functions/join_meshes.py index 2eb8c12..9fbe3a1 100644 --- a/functions/join_meshes.py +++ b/functions/join_meshes.py @@ -1,5 +1,5 @@ import bpy -from typing import List, Optional +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 @@ -17,7 +17,7 @@ class JoinAllMeshes(Operator): armature = get_selected_armature(context) return armature is not None and is_valid_armature(armature) - def execute(self, context: Context) -> set: + def execute(self, context: Context) -> Set[str]: self.join_all_meshes(context) return {'FINISHED'} @@ -58,7 +58,7 @@ class JoinSelectedMeshes(Operator): 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: + def execute(self, context: Context) -> Set[str]: self.join_selected_meshes(context) return {'FINISHED'} @@ -85,3 +85,4 @@ class JoinSelectedMeshes(Operator): self.report({'INFO'}, "Selected meshes joined successfully") else: self.report({'WARNING'}, "No mesh objects selected") + From 7401ba78d518719b551ec45e1cc15fb70164996a Mon Sep 17 00:00:00 2001 From: 989onan Date: Wed, 24 Jul 2024 18:20:28 -0400 Subject: [PATCH 4/6] Adds a bunch of import methods Yes I did do this code all myself and they have been looked over and modified since proposed to other addons. The commented MMD animation importer is stashed as a comment for now till an MMD animation importer is properly created. --- core/common.py | 15 +++ core/dictionaries.py | 104 ++++++++++---------- core/import_dictionary.py | 45 +++++++++ core/register.py | 2 +- functions/import_anything.py | 186 +++++++++++++++++++++++++++++++++++ 5 files changed, 299 insertions(+), 53 deletions(-) create mode 100644 core/import_dictionary.py create mode 100644 functions/import_anything.py diff --git a/core/common.py b/core/common.py index 6e08f93..6d8b316 100644 --- a/core/common.py +++ b/core/common.py @@ -1,6 +1,10 @@ import bpy import numpy as np from .dictionaries import bone_names +import threading +import time +import webbrowser +import typing from typing import List, Optional from bpy.types import Object, ShapeKey, Mesh, Context @@ -57,3 +61,14 @@ 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 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() + +def open_web_after_delay(delay, url): + print("opening browser in "+str(delay)+" seconds.") + time.sleep(delay) + + webbrowser.open_new_tab(url) \ No newline at end of file diff --git a/core/dictionaries.py b/core/dictionaries.py index cafc84a..64c6125 100644 --- a/core/dictionaries.py +++ b/core/dictionaries.py @@ -5,85 +5,85 @@ # Note from @989onan: Please make sure to make your names are lowercase in this array. I banged my head metaphorically till I figured that out... # Taken from Tuxedo/Cats bone_names = { - "right_shoulder": ["rightshoulder", "shoulderr", "rshoulder"], - "right_arm": ["rightarm", "armr", "rarm", "upperarmr", "rupperarm", "rightupperarm", "upperarmright", "uparmr", "ruparm"], - "right_elbow": ["rightelbow", "elbowr", "relbow", "lowerarmr", "rightlowerarm", "lowerarmr","rlowerarm", "lowerarmright", "lowarmr", "rlowarm", "forearmr","rforearm"], - "right_wrist": ["rightwrist", "wristr", "rwrist", "handr", "righthand", "rhand"], + "right_shoulder": ["rightshoulder", "shoulderr", "rshoulder", "valvebipedbip01rclavicle"], + "right_arm": ["rightarm", "armr", "rarm", "upperarmr", "rupperarm", "rightupperarm", "uparmr", "ruparm", "valvebipedbip01rupperarm"], + "right_elbow": ["rightelbow", "elbowr", "relbow", "lowerarmr", "rightlowerarm", "lowerarmr","rlowerarm", "lowarmr", "rlowarm", "forearmr","rforearm", "valvebipedbip01rforearm"], + "right_wrist": ["rightwrist", "wristr", "rwrist", "handr", "righthand", "rhand", "valvebipedbip01rhand"], #hand l fingers "pinkie_0_r": ["littlefinger0r","pinkie0r","rpinkie0","pinkiemetacarpalr"], - "pinkie_1_r": ["littlefinger1r","pinkie1r","rpinkie1","pinkieproximalr"], - "pinkie_2_r": ["littlefinger2r","pinkie2r","rpinkie2","pinkieintermediater"], - "pinkie_3_r": ["littlefinger3r","pinkie3r","rpinkie3","pinkiedistalr"], + "pinkie_1_r": ["littlefinger1r","pinkie1r","rpinkie1","pinkieproximalr", "valvebipedbip01rfinger4"], + "pinkie_2_r": ["littlefinger2r","pinkie2r","rpinkie2","pinkieintermediater", "valvebipedbip01rfinger41"], + "pinkie_3_r": ["littlefinger3r","pinkie3r","rpinkie3","pinkiedistalr", "valvebipedbip01rfinger42"], "ring_0_r": ["ringfinger0r","ring0r","rring0","ringmetacarpalr"], - "ring_1_r": ["ringfinger1r","ring1r","rring1","ringproximalr"], - "ring_2_r": ["ringfinger2r","ring2r","rring2","ringintermediater"], - "ring_3_r": ["ringfinger3r","ring3r","rring3","ringdistalr"], + "ring_1_r": ["ringfinger1r","ring1r","rring1","ringproximalr", "valvebipedbip01rfinger3"], + "ring_2_r": ["ringfinger2r","ring2r","rring2","ringintermediater", "valvebipedbip01rfinger31"], + "ring_3_r": ["ringfinger3r","ring3r","rring3","ringdistalr", "valvebipedbip01rfinger32"], "middle_0_r": ["middlefinger0r","middle0r","rmiddle0","middlemetacarpalr"], - "middle_1_r": ["middlefinger1r","middle1r","rmiddle1","middleproximalr"], - "middle_2_r": ["middlefinger2r","middle2r","rmiddle2","middleintermediater"], - "middle_3_r": ["middlefinger3r","middle3r","rmiddle3","middledistalr"], + "middle_1_r": ["middlefinger1r","middle1r","rmiddle1","middleproximalr", "valvebipedbip01rfinger2"], + "middle_2_r": ["middlefinger2r","middle2r","rmiddle2","middleintermediater", "valvebipedbip01rfinger21"], + "middle_3_r": ["middlefinger3r","middle3r","rmiddle3","middledistalr", "valvebipedbip01rfinger22"], "index_0_r": ["indexfinger0r","index0r","rindex0","indexmetacarpalr"], - "index_1_r": ["indexfinger1r","index1r","rindex1","indexproximalr"], - "index_2_r": ["indexfinger2r","index2r","rindex2","indexintermediater"], - "index_3_r": ["indexfinger3r","index3r","rindex3","indexdistalr"], + "index_1_r": ["indexfinger1r","index1r","rindex1","indexproximalr", "valvebipedbip01rfinger1"], + "index_2_r": ["indexfinger2r","index2r","rindex2","indexintermediater", "valvebipedbip01rfinger11"], + "index_3_r": ["indexfinger3r","index3r","rindex3","indexdistalr", "valvebipedbip01rfinger12"], "thumb_0_r": ["thumb0r","rthumb0","thumbmetacarpalr"], - "thumb_1_r": ['thumb1r',"rthumb1","thumbproximalr"], - "thumb_2_r": ['thumb2r',"rthumb2","thumbintermediater"], - "thumb_3_r": ['thumb3r',"rthumb3","thumbdistalr"], + "thumb_1_r": ['thumb1r',"rthumb1","thumbproximalr", "valvebipedbip01rfinger0"], + "thumb_2_r": ['thumb2r',"rthumb2","thumbintermediater", "valvebipedbip01rfinger01"], + "thumb_3_r": ['thumb3r',"rthumb3","thumbdistalr", "valvebipedbip01rfinger02"], - "right_leg": ["rightleg", "legr", "rleg", "upperlegr", "rupperleg", "thighr", "rightupperleg", "upperlegright", "uplegr", "rupleg"], - "right_knee": ["rightknee", "kneer", "rknee", "lowerlegr", "calfr", "rlowerleg", "rcalf", "rightlowerleg", "lowerlegright", "lowlegr", "rlowleg"], - "right_ankle": ["rightankle", "ankler", "rankle", "footright", "footr", "rfoot", "rightfoot", "rightfeet", "feetright", "rfeet", "feetr"], - "right_toe": ["righttoe", "toeright", "toer", "rtoe", "toesr", "rtoes"], + "right_leg": ["rightleg", "legr", "rleg", "upperlegr", "rupperleg", "thighr", "rightupperleg", "uplegr", "rupleg", "valvebipedbip01rthigh"], + "right_knee": ["rightknee", "kneer", "rknee", "lowerlegr", "calfr", "rlowerleg", "rcalf", "rightlowerleg", "lowlegr", "rlowleg", "valvebipedbip01rcalf"], + "right_ankle": ["rightankle", "ankler", "rankle", "rightfoot", "footr", "rfoot", "rightfoot", "rightfeet", "feetright", "rfeet", "feetr", "valvebipedbip01rfoot"], + "right_toe": ["righttoe", "toeright", "toer", "rtoe", "toesr", "rtoes", "valvebipedbip01rtoe0"], - "left_shoulder": ["leftshoulder", "shoulderl", "lshoulder"], - "left_arm": ["leftarm", "arml", "rarm", "upperarml", "lupperarm", "leftupperarm", "upperarmleft", "uparml", "luparm"], - "left_elbow": ["leftelbow", "elbowl", "lelbow", "lowerarml", "leftlowerarm", "lowerarmleft", "lowerarml", "llowerarm", "lowarml", "llowarm", "forearml","lforearm"], - "left_wrist": ["leftwrist", "wristl", "lwrist", "handl", "lefthand", "lhand"], + "left_shoulder": ["leftshoulder", "shoulderl", "lshoulder", "valvebipedbip01lclavicle"], + "left_arm": ["leftarm", "arml", "rarm", "upperarml", "lupperarm", "leftupperarm", "uparml", "luparm", "valvebipedbip01lupperarm"], + "left_elbow": ["leftelbow", "elbowl", "lelbow", "lowerarml", "leftlowerarm", "lowerarml", "llowerarm", "lowarml", "llowarm", "forearml","lforearm", "valvebipedbip01lforearm"], + "left_wrist": ["leftwrist", "wristl", "lwrist", "handl", "lefthand", "lhand", "valvebipedbip01lhand"], #hand l fingers "pinkie_0_l": ["pinkiefinger0l","pinkie0l","lpinkie0","pinkiemetacarpall"], - "pinkie_1_l": ["littlefinger1l","pinkie1l","lpinkie1","pinkieproximall"], - "pinkie_2_l": ["littlefinger2l","pinkie2l","lpinkie2","pinkieintermediatel"], - "pinkie_3_l": ["littlefinger3l","pinkie3l","lpinkie3","pinkiedistall"], + "pinkie_1_l": ["littlefinger1l","pinkie1l","lpinkie1","pinkieproximall", "valvebipedbip01lfinger4"], + "pinkie_2_l": ["littlefinger2l","pinkie2l","lpinkie2","pinkieintermediatel", "valvebipedbip01lfinger41"], + "pinkie_3_l": ["littlefinger3l","pinkie3l","lpinkie3","pinkiedistall", "valvebipedbip01lfinger42"], "ring_0_l": ["ringfinger0l","ring0l","lring0","ringmetacarpall"], - "ring_1_l": ["ringfinger1l","ring1l","lring1","ringproximall"], - "ring_2_l": ["ringfinger2l","ring2l","lring2","ringintermediatel"], - "ring_3_l": ["ringfinger3l","ring3l","lring3","ringdistall"], + "ring_1_l": ["ringfinger1l","ring1l","lring1","ringproximall", "valvebipedbip01lfinger3"], + "ring_2_l": ["ringfinger2l","ring2l","lring2","ringintermediatel", "valvebipedbip01lfinger31"], + "ring_3_l": ["ringfinger3l","ring3l","lring3","ringdistall", "valvebipedbip01lfinger32"], "middle_0_l": ["middlefinger0l","middle_0l","lmiddle0","middlemetacarpall"], - "middle_1_l": ["middlefinger1l","middle_1l","lmiddle1","middleproximall"], - "middle_2_l": ["middlefinger2l","middle_2l","lmiddle2","middleintermediatel"], - "middle_3_l": ["middlefinger3l","middle_3l","lmiddle3","middledistall"], + "middle_1_l": ["middlefinger1l","middle_1l","lmiddle1","middleproximall", "valvebipedbip01lfinger2"], + "middle_2_l": ["middlefinger2l","middle_2l","lmiddle2","middleintermediatel", "valvebipedbip01lfinger21"], + "middle_3_l": ["middlefinger3l","middle_3l","lmiddle3","middledistall", "valvebipedbip01lfinger22"], "index_0_l": ["indexfinger0l","index0l","lindex0","indexmetacarpall"], - "index_1_l": ["indexfinger1l","index1l","lindex1","indexproximall"], - "index_2_l": ["indexfinger2l","index2l","lindex2","indexintermediatel"], - "index_3_l": ["indexfinger3l","index3l","lindex3","indexdistall"], + "index_1_l": ["indexfinger1l","index1l","lindex1","indexproximall", "valvebipedbip01lfinger1"], + "index_2_l": ["indexfinger2l","index2l","lindex2","indexintermediatel", "valvebipedbip01lfinger11"], + "index_3_l": ["indexfinger3l","index3l","lindex3","indexdistall", "valvebipedbip01lfinger12"], "thumb_0_l": ["thumb0l","lthumb0","thumbmetacarpall"], - "thumb_1_l": ['thumb1l',"lthumb1","thumbproximall"], - "thumb_2_l": ['thumb2l',"lthumb2","thumbintermediatel"], - "thumb_3_l": ['thumb3l',"lthumb3","thumbdistall"], + "thumb_1_l": ['thumb1l',"lthumb1","thumbproximall", "valvebipedbip01lfinger0"], + "thumb_2_l": ['thumb2l',"lthumb2","thumbintermediatel", "valvebipedbip01lfinger01"], + "thumb_3_l": ['thumb3l',"lthumb3","thumbdistall", "valvebipedbip01lfinger02"], - "left_leg": ["leftleg", "legl", "lleg", "upperlegl", "lupperleg", "thighl", "leftupperleg", "upperlegleft", "uplegl", "lupleg"], - "left_knee": ["leftknee", "kneel", "lknee", "lowerlegl", "llowerleg", "calfl", "lcalf", "leftlowerleg", "lowerlegleft", 'lowlegl', 'llowleg'], - "left_ankle": ["leftankle", "anklel", "rankle", "footleft", "footl", "lfoot", "leftfoot", "leftfeet", "feetleft", "lfeet", "feetl"], - "left_toe": ["lefttoe", "toeleft", "toel", "ltoe", "toesl", "ltoes"], + "left_leg": ["leftleg", "legl", "lleg", "upperlegl", "lupperleg", "thighl", "leftupperleg", "uplegl", "lupleg", "valvebipedbip01lthigh"], + "left_knee": ["leftknee", "kneel", "lknee", "lowerlegl", "llowerleg", "calfl", "lcalf", "leftlowerleg", 'lowlegl', 'llowleg', "valvebipedbip01lcalf"], + "left_ankle": ["leftankle", "anklel", "rankle", "leftfoot", "footl", "lfoot", "leftfoot", "leftfeet", "feetleft", "lfeet", "feetl", "valvebipedbip01lfoot"], + "left_toe": ["lefttoe", "toeleft", "toel", "ltoe", "toesl", "ltoes", "valvebipedbip01ltoe0"], - "hips": ["pelvis", "hips", "hip"], - "spine": ["torso", "spine"], - "chest": ["chest"], - "upper_chest": ["upperchest", "chestupper"], - "neck": ["neck"], - "head": ["head", "cabeza"], + "hips": ["pelvis", "hips", "valvebipedbip01pelvis"], + "spine": ["torso", "spine", "valvebipedbip01spine"], + "chest": ["chest", "valvebipedbip01spine1"], + "upper_chest": ["upperchest", "valvebipedbip01spine4"], + "neck": ["neck", "valvebipedbip01neck1"], + "head": ["head", "valvebipedbip01head1"], "left_eye": ["eyeleft", "lefteye", "eyel", "leye"], "right_eye": ["eyeright", "righteye", "eyer", "reye"], } diff --git a/core/import_dictionary.py b/core/import_dictionary.py new file mode 100644 index 0000000..023038f --- /dev/null +++ b/core/import_dictionary.py @@ -0,0 +1,45 @@ +import importlib.util +import bpy +import os +import typing + +if importlib.util.find_spec("io_scene_valvesource") is not None: + #from .....scripts.addons.io_scene_valvesource.import_smd import SmdImporter #<- use this to check if your IDE is working properly. idfk + from io_scene_valvesource.import_smd import SmdImporter #ignore IDE bitching this is fine, trust me, also above comment should be okay to an IDE usually if set up right. ^_^ - @989onan + +def import_multi_files(method = None, directory: typing.Optional[str] = None, files: list[dict[str,str]] = None, filepath: typing.Optional[str] = ""): + if not files: + method(directory, filepath) + else: + for file in files: + fullpath = os.path.join(directory,os.path.basename(file["name"])) + print("run method!") + method(directory, fullpath) +#each import should map to a type. even in the case that multiple methods should import together, or have the same import method. Make sure the lambdas match so they get grouped together +#In the case of a file importer that takes only one file argument and each one needs individual import, use above method. (example of it in use is ".dae" format) +import_types: dict[str, typing.Callable[[str, list[dict[str,str]], str], None]] = { + "fbx": (lambda directory, files, filepath : bpy.ops.import_scene.fbx(files=files, directory=directory, filepath=filepath,automatic_bone_orientation=False,use_prepost_rot=False,use_anim=False)), + "smd": (lambda directory, files, filepath : eval("bpy."+SmdImporter.bl_idname+".(files=files, directory=directory, filepath=filepath)")), + "dmx": (lambda directory, files, filepath: eval("bpy."+SmdImporter.bl_idname+".(files=files, directory=directory, filepath=filepath)")), + "gltf": (lambda directory, files, filepath : bpy.ops.import_scene.gltf(files=files, filepath=filepath)), + "glb": (lambda directory, files, filepath : bpy.ops.import_scene.gltf(files=files, filepath=filepath)), + "qc": (lambda directory, files, filepath : eval("bpy."+SmdImporter.bl_idname+".(files=files, directory=directory, filepath=filepath)")), + "obj": (lambda directory, files, filepath : bpy.ops.wm.obj_import(files=files, directory=directory, filepath=filepath)), + "dae": (lambda directory, files, filepath : import_multi_files(directory=directory, files=files, filepath=filepath, method = (lambda directory, filepath: bpy.ops.wm.collada_import(filepath=filepath, auto_connect = True, find_chains = True, fix_orientation = True)))), + "3ds": (lambda directory, files, filepath : bpy.ops.import_scene.max3ds(files=files, directory=directory, filepath=filepath)), + "stl": (lambda directory, files, filepath : bpy.ops.import_mesh.stl(files=files, directory=directory, filepath=filepath)), + "mtl": (lambda directory, files, filepath : bpy.ops.wm.obj_import(files=files, directory=directory, filepath=filepath)), + "x3d": (lambda directory, files, filepath : bpy.ops.import_scene.x3d(files=files, directory=directory, filepath=filepath)), + "wrl": (lambda directory, files, filepath : bpy.ops.import_scene.x3d(files=files, directory=directory, filepath=filepath)), + "vmd": (lambda directory, files, filepath : import_multi_files(directory=directory, files=files, filepath=filepath, method = (lambda directory, filepath: bpy.ops.tuxedo.import_mmd_animation(directory=directory, filepath=filepath)))), + "pmx": (lambda directory, files, filepath : bpy.ops.mmd_tools.import_model(files=files, directory=directory, filepath=filepath)), + "pmd": (lambda directory, files, filepath : bpy.ops.mmd_tools.import_model(files=files, directory=directory, filepath=filepath)), +} + +def concat_imports_filter(imports): + names = "" + for importer in imports.keys(): + names = names+"*."+importer+";" + return names + +imports = concat_imports_filter(import_types) \ No newline at end of file diff --git a/core/register.py b/core/register.py index 5745870..ac6211d 100644 --- a/core/register.py +++ b/core/register.py @@ -49,7 +49,7 @@ def toposort(deps_dict): unsorted.append(value) deps_dict = {value : deps_dict[value] - sorted_values for value in unsorted} - sort_order(sorted_list) #to sort by 'bl_order' so we can choose how things may appear in the ui + #sort_order(sorted_list) #to sort by 'bl_order' so we can choose how things may appear in the ui return sorted_list diff --git a/functions/import_anything.py b/functions/import_anything.py new file mode 100644 index 0000000..4b8bcbf --- /dev/null +++ b/functions/import_anything.py @@ -0,0 +1,186 @@ +import bpy +from bpy.types import Operator, ImportHelper +from ..core.register import register_wrap +from ..core.import_dictionary import imports, import_types +from ..functions.translations import t +import pathlib +import os +from ..core import common +from ..core.dictionaries import bone_names + +@register_wrap +class ImportAnyModel(Operator, ImportHelper): + bl_idname = 'avatar_toolkit.import_any_model' + bl_label = t('Tools.import_any_model.label') + bl_description = t('Tools.import_any_model.desc') + bl_options = {'REGISTER', 'UNDO'} + files: bpy.props.CollectionProperty(type=bpy.types.OperatorFileListElement, options={'HIDDEN', 'SKIP_SAVE'}) + + filter_glob: bpy.props.StringProperty(default = imports, options={'HIDDEN','SKIP_SAVE'}) + directory: bpy.props.StringProperty(maxlen=1024, subtype='FILE_PATH', options={'HIDDEN', 'SKIP_SAVE'}) + + + #since I wrote this myself, a bit more efficent than cats. mostly - @989onan + def execute(self, context: bpy.types.Context): + file_grouping_dict: dict[str, list[dict[str,str]]] = dict()#group our files so our importers can import them together. in the case of OBJ+MTL and others that need grouped files, this is extremely important. + + #check if we are importing multiple files + is_multi = False + try: + for file in self.files: + pass + is_multi = True + except Exception as e: + is_multi = False + print(e) + + + #put the files together into lists of same importers + + if(is_multi): + for file in self.files: + fullpath = os.path.join(self.directory,os.path.basename(file.name)) + name = pathlib.Path(fullpath).suffix.replace(".","") + #this makes sure our imports that should be grouped stay together. + #basically the method checks for if the first value has a lambda with the same bytecode as another lambda, then it will use that value's key (ex:"obj"<->"mtl" or "fbx"), keeping same importers together + try: + name2 = next(key for key,value in import_types.items() if value.__code__.co_code == import_types[name].__code__.co_code) + print(name +" is the same importer as "+name2+", grouping.") + name = name2 + except Exception as e: + print("error when trying to find a value of the same value in the kinds of importers. May just be an import type that's a singlet:") + print(e) + if name not in file_grouping_dict: file_grouping_dict[name] = [] + file_grouping_dict[name].append({"name": os.path.basename(file.name)}) #emulate passing a list of files. + + else: + fullpath: str = os.path.join(os.path.dirname(self.filepath),os.path.basename(self.filepath)) + name = pathlib.Path(fullpath).suffix.replace(".","") + if name not in file_grouping_dict: file_grouping_dict[name] = [] + file_grouping_dict[name].append({"name": fullpath}) #emulate passing a list of files. + + #import the files together to make sure things like obj import together. This is important + for file_group_name,files in file_grouping_dict.items(): + try: + if(self.directory): + print(files) + import_types[file_group_name](self.directory,files,self.filepath) + else: + import_types[file_group_name]("",files,self.filepath) #give an empty directory, works just fine for 90% + except AttributeError as e: + print("Warning, you may not have the required importer!") + + common.open_web_after_delay_multi_threaded(delay=12, url=t('Importing.importer_search_term').format(extension = file_group_name)) + + self.report({'ERROR'},t('Importing.need_importer').format(extension = file_group_name)) + + print("importer error was:") + print(e) + + return {'FINISHED'} + + + +#This needs to be done with our own MMD importer: +""" +#stolen from cats. Oh wait I made this code riiiiiiight - @989onan +@register_wrap +class ImportMMDAnimation(bpy.types.Operator, ImportHelper): + bl_idname = 'avatar_toolkit.import_mmd_animation' + bl_label = t('Importer.mmd_anim_importer.label') + bl_description = t('Importer.mmd_anim_importer.desc') + bl_options = {'INTERNAL', 'UNDO'} + + filter_glob: bpy.props.StringProperty( + default="*.vmd", + options={'HIDDEN'} + ) + files: bpy.props.CollectionProperty(type=bpy.types.OperatorFileListElement, options={'HIDDEN', 'SKIP_SAVE'}) + directory: bpy.props.StringProperty(maxlen=1024, subtype='FILE_PATH', options={'HIDDEN', 'SKIP_SAVE'}) + filepath: bpy.props.StringProperty() + + @classmethod + def poll(cls, context): + if common.get_armature(context) is None: + return False + return True + + def execute(self, context): + + # Make sure that the first layer is visible + if hasattr(context.scene, 'layers'): + context.scene.layers[0] = True + + filename, extension = os.path.splitext(self.filepath) + + if(extension == ".vmd"): + + #A dictionary to change the current model to MMD importer compatable temporarily + bonedict = { + "chest":"UpperBody", + "neck":"Neck", + "head":"Head", + "hips":"Center", + "spine":"LowerBody", + + "right_wrist":"Wrist_R", + "right_elbow":"Elbow_R", + "right_arm":"Arm_R", + "right_shoulder":"Shoulder_R", + "right_leg":"Leg_R", + "right_knee":"Knee_R", + "right_ankle":"Ankle_R", + "right_toe":"Toe_R", + + + "left_wrist":"Wrist_L", + "left_elbow":"Elbow_L", + "left_arm":"Arm_L", + "left_shoulder":"Shoulder_L", + "left_leg":"Leg_L", + "left_knee":"Knee_L", + "left_ankle":"Ankle_L", + "left_toe":"Toe_L" + + } + + armature = common.get_armature(context) + common.unselect_all() + common.Set_Mode(context, 'OBJECT') + common.unselect_all() + common.set_active(armature) + + orig_names = dict() + reverse_bone_lookup = dict() + for (preferred_name, name_list) in bone_names.items(): + for name in name_list: + reverse_bone_lookup[name] = preferred_name + + + for bone in armature.data.bones: + if common.simplify_bonename(bone.name) in reverse_bone_lookup and reverse_bone_lookup[common.simplify_bonename(bone.name)] in bonedict: + orig_names[bonedict[reverse_bone_lookup[common.simplify_bonename(bone.name)]]] = bone.name + bone.name = bonedict[reverse_bone_lookup[common.simplify_bonename(bone.name)]] + try: + bpy.ops.mmd_tools.import_vmd(filepath=self.filepath,bone_mapper='RENAMED_BONES',use_underscore=True, dictionary='INTERNAL') + except AttributeError as e: + print("importer error was:") + print(e) + print(t('Importing.importer_search_term')) + common.open_web_after_delay_multi_threaded(delay=12, url=t('Importing.importer_search_term').format(extension = "MMD")) + self.report({'ERROR'},t('Importing.need_importer').format(extension = "MMD")) + + return {'CANCELLED'} + + #iterate through bones and put them back, therefore blender API will change the animation to be correct. + #this is because renaming bones fixes the animation targets in the data model. + for bone in armature.data.bones: + if common.simplify_bonename(bone.name) in orig_names: + bone.name = orig_names[common.simplify_bonename(bone.name)] + + common.unselect_all() + common.Set_Mode(context, 'OBJECT') + common.unselect_all() + common.set_active(armature) + + return {'FINISHED'} """ \ No newline at end of file From bad2c69ae01d334cfff7c6a38f0f348f54de4492 Mon Sep 17 00:00:00 2001 From: 989onan Date: Wed, 24 Jul 2024 18:54:02 -0400 Subject: [PATCH 5/6] add some translation keys --- resources/translations/en_US.json | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/resources/translations/en_US.json b/resources/translations/en_US.json index e5a05f3..8faa25b 100644 --- a/resources/translations/en_US.json +++ b/resources/translations/en_US.json @@ -36,7 +36,16 @@ "Settings.translation_restart_popup.label": "Translation Update", "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." + "Settings.translation_restart_popup.message2": "until you restart Blender.", + "Importing.need_importer":"You do not have the required importer for the {extension} type! Opening web browser for importer search term...", + "Importer.mmd_anim_importer.label":"MMD Animation", + "Importer.mmd_anim_importer.desc":"Import a MMD Animation (.vmd)", + "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." } } \ No newline at end of file From 65ddea16e877957887700576d65527ea2c0e83d5 Mon Sep 17 00:00:00 2001 From: 989onan Date: Wed, 24 Jul 2024 19:27:46 -0400 Subject: [PATCH 6/6] Fix problems - fix errors - add import to UI --- core/import_dictionary.py | 45 ----------------------- core/importer.py | 60 ++++++++++++++++++++++++------- core/preferences.json | 3 ++ functions/import_anything.py | 6 ++-- ui/quick_access.py | 69 ++---------------------------------- 5 files changed, 56 insertions(+), 127 deletions(-) delete mode 100644 core/import_dictionary.py create mode 100644 core/preferences.json diff --git a/core/import_dictionary.py b/core/import_dictionary.py deleted file mode 100644 index 023038f..0000000 --- a/core/import_dictionary.py +++ /dev/null @@ -1,45 +0,0 @@ -import importlib.util -import bpy -import os -import typing - -if importlib.util.find_spec("io_scene_valvesource") is not None: - #from .....scripts.addons.io_scene_valvesource.import_smd import SmdImporter #<- use this to check if your IDE is working properly. idfk - from io_scene_valvesource.import_smd import SmdImporter #ignore IDE bitching this is fine, trust me, also above comment should be okay to an IDE usually if set up right. ^_^ - @989onan - -def import_multi_files(method = None, directory: typing.Optional[str] = None, files: list[dict[str,str]] = None, filepath: typing.Optional[str] = ""): - if not files: - method(directory, filepath) - else: - for file in files: - fullpath = os.path.join(directory,os.path.basename(file["name"])) - print("run method!") - method(directory, fullpath) -#each import should map to a type. even in the case that multiple methods should import together, or have the same import method. Make sure the lambdas match so they get grouped together -#In the case of a file importer that takes only one file argument and each one needs individual import, use above method. (example of it in use is ".dae" format) -import_types: dict[str, typing.Callable[[str, list[dict[str,str]], str], None]] = { - "fbx": (lambda directory, files, filepath : bpy.ops.import_scene.fbx(files=files, directory=directory, filepath=filepath,automatic_bone_orientation=False,use_prepost_rot=False,use_anim=False)), - "smd": (lambda directory, files, filepath : eval("bpy."+SmdImporter.bl_idname+".(files=files, directory=directory, filepath=filepath)")), - "dmx": (lambda directory, files, filepath: eval("bpy."+SmdImporter.bl_idname+".(files=files, directory=directory, filepath=filepath)")), - "gltf": (lambda directory, files, filepath : bpy.ops.import_scene.gltf(files=files, filepath=filepath)), - "glb": (lambda directory, files, filepath : bpy.ops.import_scene.gltf(files=files, filepath=filepath)), - "qc": (lambda directory, files, filepath : eval("bpy."+SmdImporter.bl_idname+".(files=files, directory=directory, filepath=filepath)")), - "obj": (lambda directory, files, filepath : bpy.ops.wm.obj_import(files=files, directory=directory, filepath=filepath)), - "dae": (lambda directory, files, filepath : import_multi_files(directory=directory, files=files, filepath=filepath, method = (lambda directory, filepath: bpy.ops.wm.collada_import(filepath=filepath, auto_connect = True, find_chains = True, fix_orientation = True)))), - "3ds": (lambda directory, files, filepath : bpy.ops.import_scene.max3ds(files=files, directory=directory, filepath=filepath)), - "stl": (lambda directory, files, filepath : bpy.ops.import_mesh.stl(files=files, directory=directory, filepath=filepath)), - "mtl": (lambda directory, files, filepath : bpy.ops.wm.obj_import(files=files, directory=directory, filepath=filepath)), - "x3d": (lambda directory, files, filepath : bpy.ops.import_scene.x3d(files=files, directory=directory, filepath=filepath)), - "wrl": (lambda directory, files, filepath : bpy.ops.import_scene.x3d(files=files, directory=directory, filepath=filepath)), - "vmd": (lambda directory, files, filepath : import_multi_files(directory=directory, files=files, filepath=filepath, method = (lambda directory, filepath: bpy.ops.tuxedo.import_mmd_animation(directory=directory, filepath=filepath)))), - "pmx": (lambda directory, files, filepath : bpy.ops.mmd_tools.import_model(files=files, directory=directory, filepath=filepath)), - "pmd": (lambda directory, files, filepath : bpy.ops.mmd_tools.import_model(files=files, directory=directory, filepath=filepath)), -} - -def concat_imports_filter(imports): - names = "" - for importer in imports.keys(): - names = names+"*."+importer+";" - return names - -imports = concat_imports_filter(import_types) \ No newline at end of file diff --git a/core/importer.py b/core/importer.py index e29ca11..9e86387 100644 --- a/core/importer.py +++ b/core/importer.py @@ -1,17 +1,53 @@ import bpy # Importers which don't need much code should be added here, however if a importer needs alot of code -# Like the PMX and PMD importers, they should be added to their own files. +# Like the PMX and PMD importers, they should be added to their own files and referenced in the import_types str->lambda dictionary. +#See below comments on how the system works. - @989onan -# FBX Importer settings borrowed form Cat's Blender Plugin -def import_fbx(filepath): - try: - bpy.ops.import_scene.fbx( - filepath=filepath, - automatic_bone_orientation=False, - use_prepost_rot=False, - use_anim=False - ) - except (TypeError, ValueError) as e: - print(f"Error importing FBX: {str(e)}") +import importlib.util +import os +import typing +from .import_pmx import import_pmx +from .import_pmd import import_pmd + +if importlib.util.find_spec("io_scene_valvesource") is not None: + #from .....scripts.addons.io_scene_valvesource.import_smd import SmdImporter #<- use this to check if your IDE is working properly. idfk + from io_scene_valvesource.import_smd import SmdImporter #ignore IDE bitching this is fine, trust me, also above comment should be okay to an IDE usually if set up right. ^_^ - @989onan + +def import_multi_files(method = None, directory: typing.Optional[str] = None, files: list[dict[str,str]] = None, filepath: typing.Optional[str] = ""): + if not files: + method(directory, filepath) + else: + for file in files: + fullpath = os.path.join(directory,os.path.basename(file["name"])) + print("run method!") + method(directory, fullpath) +#each import should map to a type. even in the case that multiple methods should import together, or have the same import method. Make sure the lambdas match so they get grouped together +#In the case of a file importer that takes only one file argument and each one needs individual import, use above method. (example of it in use is ".dae" format) +import_types: dict[str, typing.Callable[[str, list[dict[str,str]], str], None]] = { + "fbx": (lambda directory, files, filepath : bpy.ops.import_scene.fbx(files=files, directory=directory, filepath=filepath,automatic_bone_orientation=False,use_prepost_rot=False,use_anim=False)), + "smd": (lambda directory, files, filepath : eval("bpy."+SmdImporter.bl_idname+".(files=files, directory=directory, filepath=filepath)")), + "dmx": (lambda directory, files, filepath: eval("bpy."+SmdImporter.bl_idname+".(files=files, directory=directory, filepath=filepath)")), + "gltf": (lambda directory, files, filepath : bpy.ops.import_scene.gltf(files=files, filepath=filepath)), + "glb": (lambda directory, files, filepath : bpy.ops.import_scene.gltf(files=files, filepath=filepath)), + "qc": (lambda directory, files, filepath : eval("bpy."+SmdImporter.bl_idname+".(files=files, directory=directory, filepath=filepath)")), + "obj": (lambda directory, files, filepath : bpy.ops.wm.obj_import(files=files, directory=directory, filepath=filepath)), + "dae": (lambda directory, files, filepath : import_multi_files(directory=directory, files=files, filepath=filepath, method = (lambda directory, filepath: bpy.ops.wm.collada_import(filepath=filepath, auto_connect = True, find_chains = True, fix_orientation = True)))), + "3ds": (lambda directory, files, filepath : bpy.ops.import_scene.max3ds(files=files, directory=directory, filepath=filepath)), + "stl": (lambda directory, files, filepath : bpy.ops.import_mesh.stl(files=files, directory=directory, filepath=filepath)), + "mtl": (lambda directory, files, filepath : bpy.ops.wm.obj_import(files=files, directory=directory, filepath=filepath)), + "x3d": (lambda directory, files, filepath : bpy.ops.import_scene.x3d(files=files, directory=directory, filepath=filepath)), + "wrl": (lambda directory, files, filepath : bpy.ops.import_scene.x3d(files=files, directory=directory, filepath=filepath)), + "vmd": (lambda directory, files, filepath : import_multi_files(directory=directory, files=files, filepath=filepath, method = (lambda directory, filepath: bpy.ops.tuxedo.import_mmd_animation(directory=directory, filepath=filepath)))), + "pmx": (lambda directory, files, filepath : import_pmx(filepath)), + "pmd": (lambda directory, files, filepath : import_pmd(filepath)), +} + +def concat_imports_filter(imports): + names = "" + for importer in imports.keys(): + names = names+"*."+importer+";" + return names + +imports = concat_imports_filter(import_types) \ No newline at end of file diff --git a/core/preferences.json b/core/preferences.json new file mode 100644 index 0000000..b0ca7bf --- /dev/null +++ b/core/preferences.json @@ -0,0 +1,3 @@ +{ + "language": 0 +} \ No newline at end of file diff --git a/functions/import_anything.py b/functions/import_anything.py index 4b8bcbf..f4a5921 100644 --- a/functions/import_anything.py +++ b/functions/import_anything.py @@ -1,12 +1,12 @@ import bpy -from bpy.types import Operator, ImportHelper +from bpy.types import Operator +from bpy_extras.io_utils import ImportHelper from ..core.register import register_wrap -from ..core.import_dictionary import imports, import_types +from ..core.importer import imports, import_types from ..functions.translations import t import pathlib import os from ..core import common -from ..core.dictionaries import bone_names @register_wrap class ImportAnyModel(Operator, ImportHelper): diff --git a/ui/quick_access.py b/ui/quick_access.py index 81b9bee..802af88 100644 --- a/ui/quick_access.py +++ b/ui/quick_access.py @@ -6,7 +6,7 @@ from ..functions.translations import t from ..core.import_pmx import import_pmx from ..core.import_pmd import import_pmd -from ..core.importer import import_fbx +from ..functions.import_anything import ImportAnyModel @register_wrap class AvatarToolkitQuickAccessPanel(bpy.types.Panel): @@ -28,29 +28,9 @@ class AvatarToolkitQuickAccessPanel(bpy.types.Panel): row = layout.row(align=True) row.scale_y = 1.5 - row.operator("avatar_toolkit.import_menu", text=t("Quick_Access.import")) + row.operator(ImportAnyModel.bl_idname, text=t("Quick_Access.import")) row.operator("avatar_toolkit.export_menu", text=t("Quick_Access.export")) -@register_wrap -class AVATAR_TOOLKIT_OT_import_menu(bpy.types.Operator): - bl_idname = "avatar_toolkit.import_menu" - bl_label = t("Quick_Access.import_menu.label") - bl_description = t("Quick_Access.import_menu.desc") - - def execute(self, context: Context): - return {'FINISHED'} - - def invoke(self, context: Context, event): - wm = context.window_manager - return wm.invoke_popup(self, width=200) - - def draw(self, context: Context): - layout = self.layout - layout.label(text="Select Import Method") - layout.operator("avatar_toolkit.import_pmx", text=t("Quick_Access.import_pmx")) - layout.operator("avatar_toolkit.import_pmd", text=t("Quick_Access.import_pmd")) - layout.operator("avatar_toolkit.import_fbx", text="Import FBX") - @register_wrap class AVATAR_TOOLKIT_OT_export_menu(bpy.types.Operator): bl_idname = "avatar_toolkit.export_menu" @@ -74,51 +54,6 @@ class AVATAR_TOOLKIT_OT_export_menu(bpy.types.Operator): layout.operator("avatar_toolkit.export_resonite", text=t("Quick_Access.select_export_resonite.label")) layout.operator("avatar_toolkit.export_fbx", text="Export FBX") -@register_wrap -class AVATAR_TOOLKIT_OT_import_pmx(bpy.types.Operator): - bl_idname = "avatar_toolkit.import_pmx" - bl_label = t("Quick_Access.import_pmx") - - filepath: bpy.props.StringProperty(subtype="FILE_PATH") - - def execute(self, context: Context): - import_pmx(self.filepath) - return {'FINISHED'} - - def invoke(self, context: Context, event): - context.window_manager.fileselect_add(self) - return {'RUNNING_MODAL'} - -@register_wrap -class AVATAR_TOOLKIT_OT_import_pmd(bpy.types.Operator): - bl_idname = "avatar_toolkit.import_pmd" - bl_label = t("Quick_Access.import_pmd") - - filepath: bpy.props.StringProperty(subtype="FILE_PATH") - - def execute(self, context: Context): - import_pmd(self.filepath) - return {'FINISHED'} - - def invoke(self, context: Context, event): - context.window_manager.fileselect_add(self) - return {'RUNNING_MODAL'} - -@register_wrap -class AVATAR_TOOLKIT_OT_import_fbx(bpy.types.Operator): - bl_idname = "avatar_toolkit.import_fbx" - bl_label = "Import FBX" - - filepath: bpy.props.StringProperty(subtype="FILE_PATH") - - def execute(self, context): - import_fbx(self.filepath) - return {'FINISHED'} - - def invoke(self, context, event): - context.window_manager.fileselect_add(self) - return {'RUNNING_MODAL'} - @register_wrap class AVATAR_TOOLKIT_OT_export_fbx(bpy.types.Operator): bl_idname = 'avatar_toolkit.export_fbx'