diff --git a/__init__.py b/__init__.py index da7389e..05a6329 100644 --- a/__init__.py +++ b/__init__.py @@ -1,5 +1,3 @@ - - if "bpy" not in locals(): import bpy from . import ui @@ -7,30 +5,36 @@ if "bpy" not in locals(): from . import functions from .core import register from .core.register import __bl_ordered_classes - from .core.properties import register_properties + from .core import properties else: import importlib importlib.reload(ui) importlib.reload(core) importlib.reload(functions) - + importlib.reload(properties) def register(): print("Registering Avatar Toolkit") + # Register the addon properties + properties.register() # Order the classes before registration core.register.order_classes() register_properties() # Register the UI classes # Iterate over the classes to register and register them - for cls in __bl_ordered_classes: - print("registering" + str(cls)) - bpy.utils.register_class(cls) + core.register.register_properties() + for cls in core.register.__bl_ordered_classes: + print("registering " + str(cls)) + bpy.utils.register_class(cls) + def unregister(): print("Unregistering Avatar Toolkit") # Unregister the UI classes - + # Iterate over the classes to unregister in reverse order and unregister them - for cls in reversed(list(__bl_ordered_classes)): + for cls in reversed(core.register.__bl_ordered_classes): bpy.utils.unregister_class(cls) - print("unregistering" + str(cls)) + print("unregistering " + str(cls)) + core.register.unregister_properties() + properties.unregister() diff --git a/core/__init__.py b/core/__init__.py index d1f53d5..50c92e7 100644 --- a/core/__init__.py +++ b/core/__init__.py @@ -1,7 +1,6 @@ # core/__init__.py from .register import register_wrap -from . import pmx #to reload all things in this directory and import them properly - @989onan if "bpy" not in locals(): diff --git a/core/importer.py b/core/importer.py new file mode 100644 index 0000000..e29ca11 --- /dev/null +++ b/core/importer.py @@ -0,0 +1,17 @@ +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. + + +# 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)}") diff --git a/core/pmd/import_pmd.py b/core/pmd/import_pmd.py deleted file mode 100644 index d23f4a9..0000000 --- a/core/pmd/import_pmd.py +++ /dev/null @@ -1,224 +0,0 @@ -import bpy -import struct -import mathutils - -def read_pmd_header(file): - # Read PMD header information - magic = file.read(3) - if magic != b'Pmd': - raise ValueError("Invalid PMD file") - - version = struct.unpack(' None: + register_property((Scene, "language", bpy.props.EnumProperty( + name=t("Settings.language.label"), + description=t("Settings.language.desc"), + items=get_languages_list, + update=update_ui + ))) + register_class(SceneMatClass) #happy with how compressed this get_texture_node_list method is - @989onan @@ -65,23 +72,21 @@ def register_properties(): - Material.texture_atlas_albedo = EnumProperty(name="Albedo", description="The texture that will be used for the albedo map atlas", default=0, items=get_texture_node_list) - Material.texture_atlas_normal = EnumProperty(name="Normal", description="The texture that will be used for the normal map atlas", default=0, items=get_texture_node_list) - Material.texture_atlas_emission = EnumProperty(name="Emission", description="The texture that will be used for the emission map atlas", default=0, items=get_texture_node_list) - Material.texture_atlas_ambient_occlusion = EnumProperty(name="Ambient Occlusion", description="The texture that will be used for the ambient occlusion map atlas", default=0, items=get_texture_node_list) - Material.texture_atlas_height = EnumProperty(name="Height", description="The texture that will be used for the height map atlas", default=0, items=get_texture_node_list) - Material.texture_atlas_roughness = EnumProperty(name="Roughness", description="The texture that will be used for the roughness map atlas", default=0, items=get_texture_node_list) + register_property(Material, "texture_atlas_albedo", EnumProperty(name="Albedo", description="The texture that will be used for the albedo map atlas", default=0, items=get_texture_node_list)) + register_property(Material, "texture_atlas_normal", EnumProperty(name="Normal", description="The texture that will be used for the normal map atlas", default=0, items=get_texture_node_list)) + register_property(Material, "texture_atlas_emission", EnumProperty(name="Emission", description="The texture that will be used for the emission map atlas", default=0, items=get_texture_node_list)) + register_property(Material, "texture_atlas_ambient_occlusion", EnumProperty(name="Ambient Occlusion", description="The texture that will be used for the ambient occlusion map atlas", default=0, items=get_texture_node_list)) + register_property(Material, "texture_atlas_height", EnumProperty(name="Height", description="The texture that will be used for the height map atlas", default=0, items=get_texture_node_list)) + register_property(Material, "texture_atlas_roughness", EnumProperty(name="Roughness", description="The texture that will be used for the roughness map atlas", default=0, items=get_texture_node_list)) - Scene.texture_atlas_material_index = IntProperty(default=-1, get=(lambda self : -1), set=(lambda self,context : None)) + register_property(Scene, "texture_atlas_material_index", IntProperty(default=-1, get=(lambda self : -1), set=(lambda self,context : None))) - Scene.materials = CollectionProperty(type=SceneMatClass) + register_property(Scene, "materials", CollectionProperty(type=SceneMatClass)) - Scene.texture_atlas_Has_Mat_List_Shown = BoolProperty(default=False, get=material_list_bool.get_bool, set=material_list_bool.set_bool) + register_property(Scene, "texture_atlas_Has_Mat_List_Shown", BoolProperty(default=False, get=material_list_bool.get_bool, set=material_list_bool.set_bool)) - #Scene.texture_atlas_properties = PointerProperty(type=Texture_Atlas_PropertyGroup) - - - \ No newline at end of file +def unregister() -> None: + pass diff --git a/core/register.py b/core/register.py index 2c20729..396c5e1 100644 --- a/core/register.py +++ b/core/register.py @@ -5,6 +5,8 @@ import typing __bl_classes = [] # List to store the ordered classes for registration __bl_ordered_classes = [] +# List to store props to register +__bl_props = [] def register_wrap(cls): # Check if the class has a 'bl_rna' attribute (indicating it's a Blender class) @@ -13,6 +15,17 @@ def register_wrap(cls): __bl_classes.append(cls) return cls +# Register all properties +def register_property(prop): + __bl_props.append(prop) + +def register_properties(): + for prop in __bl_props: + setattr(prop[0], prop[1], prop[2]) + +def unregister_properties(): + for prop in reversed(__bl_props): + delattr(prop[0], prop[1]) #- @989onan had to add this from Cats. This is extremely important else you will be screamed at by register order issues! # Find order to register to solve dependencies diff --git a/functions/translations.py b/functions/translations.py new file mode 100644 index 0000000..4f84a1d --- /dev/null +++ b/functions/translations.py @@ -0,0 +1,64 @@ +import os +import json +import bpy +from bpy.app.translations import locale +from ..core.register import register_wrap +from typing import Dict, List, Tuple + +main_dir: str = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +resources_dir: str = os.path.join(main_dir, "resources") +translations_dir: str = os.path.join(resources_dir, "translations") + +dictionary: Dict[str, str] = dict() +languages: List[str] = [] +verbose: bool = True + +def load_translations() -> None: + global dictionary, languages + + dictionary = dict() + languages = ["auto"] + + language: str = bpy.context.preferences.view.language + + for i in os.listdir(translations_dir): + languages.append(i.split(".")[0]) + + translation_file: str = os.path.join(translations_dir, language + ".json") + if os.path.exists(translation_file): + with open(translation_file, 'r', encoding='utf-8') as file: + dictionary = json.load(file)["messages"] + else: + custom_language: str = language.split("_")[0] + custom_translation_file: str = os.path.join(translations_dir, custom_language + ".json") + if os.path.exists(custom_translation_file): + with open(custom_translation_file, 'r', encoding='utf-8') as file: + dictionary = json.load(file)["messages"] + else: + print(f"Translation file not found for language: {language}") + default_file: str = os.path.join(translations_dir, "en_US.json") + if os.path.exists(default_file): + with open(default_file, 'r', encoding='utf-8') as file: + dictionary = json.load(file)["messages"] + else: + print("Default translation file 'en_US.json' not found.") + +def t(phrase: str, *args, **kwargs) -> str: + output: str = dictionary.get(phrase) + if output is None: + if verbose: + print('Warning: Unknown phrase: ' + phrase) + return phrase + return output.format(*args, **kwargs) + +def get_languages_list(self, context) -> List[Tuple[str, str, str]]: + choices: List[Tuple[str, str, str]] = [] + for language in languages: + choices.append((language, language, language)) + return choices + +def update_ui(self, context) -> None: + load_translations() + bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1) + +load_translations() diff --git a/resources/translations/en_US.json b/resources/translations/en_US.json new file mode 100644 index 0000000..ed97a90 --- /dev/null +++ b/resources/translations/en_US.json @@ -0,0 +1,9 @@ +{ + "messages": { + "Settings.label": "Settings", + "Settings.language.label": "Language", + "Settings.language.desc": "Select the language for the addon's UI" + } + } + + \ No newline at end of file diff --git a/resources/translations/ja_JP.json b/resources/translations/ja_JP.json new file mode 100644 index 0000000..1c53c39 --- /dev/null +++ b/resources/translations/ja_JP.json @@ -0,0 +1,8 @@ +{ + "messages": { + "Settings.label": "Settings Ja Test", + "Settings.language.label": "Language Ja Test", + "Settings.language.desc": "Select the language for the addon's UI Ja Test" + } + } + \ No newline at end of file diff --git a/ui/__init__.py b/ui/__init__.py index cc0041c..7db3379 100644 --- a/ui/__init__.py +++ b/ui/__init__.py @@ -13,7 +13,3 @@ else: for module_name in [ basename(f)[:-3] for f in modules if isfile(f) and not f.endswith('__init__.py')]: print("reloading " +module_name) exec("importlib.reload("+module_name+")") - - - - diff --git a/ui/quick_access.py b/ui/quick_access.py index 1d93c25..9ceee62 100644 --- a/ui/quick_access.py +++ b/ui/quick_access.py @@ -5,6 +5,7 @@ from bpy.types import Context from ..core.import_pmx import import_pmx from ..core.import_pmd import import_pmd +from ..core.importer import import_fbx @register_wrap class AvatarToolkitQuickAccessPanel(bpy.types.Panel): @@ -46,12 +47,17 @@ class AVATAR_TOOLKIT_OT_import_menu(bpy.types.Operator): layout.label(text="Select Import Method") layout.operator("avatar_toolkit.import_pmx", text="Import PMX") layout.operator("avatar_toolkit.import_pmd", text="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" bl_label = "Export Menu" + @classmethod + def poll(cls, context): + return any(obj.type == 'MESH' for obj in context.scene.objects) + def execute(self, context: Context): return {'FINISHED'} @@ -63,6 +69,7 @@ class AVATAR_TOOLKIT_OT_export_menu(bpy.types.Operator): layout = self.layout layout.label(text="Select Export Method") layout.operator("avatar_toolkit.export_resonite", text="Export Resonite") + layout.operator("avatar_toolkit.export_fbx", text="Export FBX") @register_wrap class AVATAR_TOOLKIT_OT_import_pmx(bpy.types.Operator): @@ -93,3 +100,31 @@ class AVATAR_TOOLKIT_OT_import_pmd(bpy.types.Operator): 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' + bl_label = "Export FBX" + bl_description = "Export the model as FBX" + bl_options = {'REGISTER', 'UNDO', 'INTERNAL'} + + def execute(self, context): + bpy.ops.export_scene.fbx('INVOKE_DEFAULT') + return {'FINISHED'} + + diff --git a/ui/settings.py b/ui/settings.py new file mode 100644 index 0000000..bd298ed --- /dev/null +++ b/ui/settings.py @@ -0,0 +1,19 @@ +import bpy +from ..core.register import register_wrap +from .panel import AvatarToolkitPanel +from ..functions.translations import t + +@register_wrap +class AvatarToolkitSettingsPanel(bpy.types.Panel): + bl_label = t("Settings.label") + bl_idname = "OBJECT_PT_avatar_toolkit_settings" + bl_space_type = 'VIEW_3D' + bl_region_type = 'UI' + bl_category = "Avatar Toolkit" + bl_parent_id = "OBJECT_PT_avatar_toolkit" + + def draw(self, context): + layout = self.layout + props = context.scene + + layout.prop(props, "language")