From 9ec186b1cfc8791473b52947eb7f4dda473a83e7 Mon Sep 17 00:00:00 2001 From: 989onan Date: Sun, 7 Jul 2024 16:19:52 -0400 Subject: [PATCH 01/17] Added a modal for remove doubles - made remove doubles a blender modal. this way the code can run over multiple frames. - Since remove doubles is async now, the user gets feedback on which shapekey and mesh is being worked on - this does not remove doubles correctly yet, but is very close to ready --- .gitignore | 1 + .vscode/settings.json | 11 +++ functions/remove_doubles_safely.py | 103 +++++++++++++++++++++++++++++ ui/optimization.py | 1 + ui/tools.py | 4 +- 5 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 functions/remove_doubles_safely.py diff --git a/.gitignore b/.gitignore index fd20fdd..5aea8c5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.pyc +.vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json index e69de29..96df99e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -0,0 +1,11 @@ +{ + "python.analysis.extraPaths": [ + "D:\\SteamLibrary\\steamapps\\common\\Blender\\4.0\\scripts\\addons", + "C:\\Users\\Onan\\AppData\\Roaming\\Blender Foundation\\Blender\\4.0\\scripts\\addons",//C:/Users/Onan/AppData/Roaming/Blender Foundation/Blender/4.0/scripts/addons + "D:\\blender stuff\\blendercodestuff\\4.0" + ], + "python.analysis.diagnosticSeverityOverrides": { + "reportInvalidTypeForm": "none" + }, + "python.REPL.enableREPLSmartSend": false, +} \ No newline at end of file diff --git a/functions/remove_doubles_safely.py b/functions/remove_doubles_safely.py new file mode 100644 index 0000000..e962df6 --- /dev/null +++ b/functions/remove_doubles_safely.py @@ -0,0 +1,103 @@ +from ast import Dict +from itertools import count +import bpy +import re +from typing import List, Tuple, Optional, TypedDict +from bpy.types import Material, Operator, Context, Object +from ..core.register import register_wrap + + +class meshEntry(TypedDict): + mesh: bpy.types.Object + shapekeys: list[str] + +@register_wrap +class RemoveDoublesSafely(Operator): + bl_idname = "avatar_toolkit.remove_doubles_safely" + bl_label = "Remove Doubles Safely" + bl_description = "Remove Doubles on all meshes, making sure to not fuse things like mouths together." + bl_options = {'REGISTER', 'UNDO'} + objects_to_do: list[meshEntry] = [] + + @classmethod + def poll(cls, context: Context) -> bool: + return context.mode == 'OBJECT' + + def execute(self, context: Context) -> set: + if not bpy.data.objects: + self.report({'INFO'}, "No objects in the scene") + return + + bpy.ops.object.mode_set(mode='OBJECT') + bpy.ops.object.select_all(action='DESELECT') + + meshes: List[Object] = [obj for obj in context.view_layer.objects if obj.type == 'MESH'] + + for mesh in meshes: + 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 + shape: bpy.types.ShapeKey = None + if mesh_data.shape_keys: + 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'} + + def modify_mesh(self, context: Context, mesh: meshEntry): + mesh["mesh"].select_set(True) + context.view_layer.objects.active = mesh["mesh"] + 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 + if len(mesh['shapekeys']) > 0: + shapekeyname: str = mesh['shapekeys'].pop(0) + + target_shapekey: int = mesh_data.shape_keys.key_blocks.find(shapekeyname) + mesh["mesh"].active_shape_key_index = target_shapekey + print("doing shapekey \""+shapekeyname+"\" on mesh \""+mesh['mesh'].name+"\".") + self.modify_mesh(context, mesh) + + elif not (mesh_data.shape_keys): + print("doing mesh with no shapekeys named \""+mesh['mesh'].name+"\".") + self.modify_mesh(context, mesh) + self.objects_to_do.pop(0) + else: + self.objects_to_do.pop(0) + else: + return {'FINISHED'} + + return {'RUNNING_MODAL'} + + + + + + + diff --git a/ui/optimization.py b/ui/optimization.py index 2f6e69a..0de9bae 100644 --- a/ui/optimization.py +++ b/ui/optimization.py @@ -25,6 +25,7 @@ class AvatarToolkitOptimizationPanel(bpy.types.Panel): row.scale_y = 1.2 row.operator("avatar_toolkit.join_all_meshes", text="Join All Meshes") row.operator("avatar_toolkit.join_selected_meshes", text="Join Selected Meshes") + row.operator("avatar_toolkit.remove_doubles_safely", text="Remove Doubles Safely") # Add optimization options here diff --git a/ui/tools.py b/ui/tools.py index 408a95d..2c2ea05 100644 --- a/ui/tools.py +++ b/ui/tools.py @@ -19,4 +19,6 @@ class AvatarToolkitToolsPanel(bpy.types.Panel): row = layout.row(align=True) row.scale_y = 1.5 - row.operator("avatar_toolkit.convert_to_resonite", text="Translate to Resonite") \ No newline at end of file + row.operator("avatar_toolkit.convert_to_resonite", text="Translate to Resonite") + row = layout.row(align=True) + row.operator("avatar_toolkit.remove_doubles_safely", text="Remove Doubles Safely") \ No newline at end of file From 12e651f68c974f3dc96163820719270fa0fd872b Mon Sep 17 00:00:00 2001 From: 989onan Date: Sun, 7 Jul 2024 17:49:03 -0400 Subject: [PATCH 02/17] Finished merge doubles this now does doubles asyncronously --- .vscode/settings.json | 6 ++--- functions/remove_doubles_safely.py | 41 +++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 96df99e..e685f8d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,8 +1,8 @@ { "python.analysis.extraPaths": [ - "D:\\SteamLibrary\\steamapps\\common\\Blender\\4.0\\scripts\\addons", - "C:\\Users\\Onan\\AppData\\Roaming\\Blender Foundation\\Blender\\4.0\\scripts\\addons",//C:/Users/Onan/AppData/Roaming/Blender Foundation/Blender/4.0/scripts/addons - "D:\\blender stuff\\blendercodestuff\\4.0" + "D:\\SteamLibrary\\steamapps\\common\\Blender\\4.3\\scripts\\addons", + "C:\\Users\\Onan\\AppData\\Roaming\\Blender Foundation\\Blender\\4.3\\extensions\\user_default\\",//C:/Users/Onan/AppData/Roaming/Blender Foundation/Blender/4.0/scripts/addons + "D:\\blender stuff\\blendercodestuff\\4.3" ], "python.analysis.diagnosticSeverityOverrides": { "reportInvalidTypeForm": "none" diff --git a/functions/remove_doubles_safely.py b/functions/remove_doubles_safely.py index e962df6..1263e2d 100644 --- a/functions/remove_doubles_safely.py +++ b/functions/remove_doubles_safely.py @@ -18,6 +18,7 @@ class RemoveDoublesSafely(Operator): bl_description = "Remove Doubles on all meshes, making sure to not fuse things like mouths together." bl_options = {'REGISTER', 'UNDO'} objects_to_do: list[meshEntry] = [] + merge_distance: bpy.props.FloatProperty(default=0.0001) @classmethod def poll(cls, context: Context) -> bool: @@ -55,11 +56,17 @@ class RemoveDoublesSafely(Operator): def modify_mesh(self, context: Context, mesh: meshEntry): mesh["mesh"].select_set(True) context.view_layer.objects.active = mesh["mesh"] + context.view_layer.objects.active = mesh["mesh"] + 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: + mesh_data.vertices[index].select = True + print("shapekey has a moved vertex at index \""+str(index)+"\", excluding from double merging!") + bpy.ops.object.mode_set(mode='EDIT') @@ -86,10 +93,38 @@ class RemoveDoublesSafely(Operator): elif not (mesh_data.shape_keys): print("doing mesh with no shapekeys named \""+mesh['mesh'].name+"\".") - self.modify_mesh(context, mesh) + mesh["mesh"].select_set(True) + context.view_layer.objects.active = mesh["mesh"] + bpy.ops.object.mode_set(mode='EDIT') + mesh_data.vertices.foreach_set("select",[False]*len(mesh_data.vertices)) + + bpy.ops.mesh.select_all(action="INVERT") + bpy.ops.mesh.remove_doubles(threshold=self.merge_distance,use_unselected=False) + + bpy.ops.object.mode_set(mode='OBJECT') + mesh["mesh"].select_set(False) self.objects_to_do.pop(0) else: + mesh["mesh"].select_set(True) + context.view_layer.objects.active = mesh["mesh"] + bpy.ops.object.mode_set(mode='EDIT') + + bpy.ops.mesh.select_all(action="INVERT") + bpy.ops.mesh.remove_doubles(threshold=self.merge_distance,use_unselected=False) + + bpy.ops.object.mode_set(mode='OBJECT') + mesh["mesh"].select_set(False) + self.objects_to_do.pop(0) + if len(self.objects_to_do) > 0: + mesh = self.objects_to_do[0] + mesh["mesh"].select_set(True) + context.view_layer.objects.active = mesh["mesh"] + bpy.ops.object.mode_set(mode='EDIT') + mesh_data.vertices.foreach_set("select",[False]*len(mesh_data.vertices)) + bpy.ops.object.mode_set(mode='OBJECT') + mesh["mesh"].select_set(False) + else: return {'FINISHED'} From 88061d2ad5f4176f6864609c8bfab07f0b0c6183 Mon Sep 17 00:00:00 2001 From: 989onan Date: Sun, 7 Jul 2024 18:42:57 -0400 Subject: [PATCH 03/17] Update remove_doubles_safely.py use the get armature method properly --- functions/remove_doubles_safely.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/functions/remove_doubles_safely.py b/functions/remove_doubles_safely.py index 1263e2d..99850ae 100644 --- a/functions/remove_doubles_safely.py +++ b/functions/remove_doubles_safely.py @@ -5,6 +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 class meshEntry(TypedDict): @@ -31,8 +32,9 @@ class RemoveDoublesSafely(Operator): 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 - meshes: List[Object] = [obj for obj in context.view_layer.objects if obj.type == 'MESH'] + meshes: List[Object] = [obj for obj in objects if obj.type == 'MESH'] for mesh in meshes: if mesh.data.name not in [stored_object["mesh"].data.name for stored_object in self.objects_to_do]: From bfdbac8412313a834e124ff40695ab1d7c8dffe1 Mon Sep 17 00:00:00 2001 From: 989onan Date: Sun, 7 Jul 2024 19:11:50 -0400 Subject: [PATCH 04/17] start work on texture atlas structures start of something big --- .vscode/settings.json | 11 +++++++++++ core/properties.py | 25 +++++++++++++++++++++++++ functions/texture_atlas.py | 23 +++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 core/properties.py create mode 100644 functions/texture_atlas.py diff --git a/.vscode/settings.json b/.vscode/settings.json index e69de29..e685f8d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -0,0 +1,11 @@ +{ + "python.analysis.extraPaths": [ + "D:\\SteamLibrary\\steamapps\\common\\Blender\\4.3\\scripts\\addons", + "C:\\Users\\Onan\\AppData\\Roaming\\Blender Foundation\\Blender\\4.3\\extensions\\user_default\\",//C:/Users/Onan/AppData/Roaming/Blender Foundation/Blender/4.0/scripts/addons + "D:\\blender stuff\\blendercodestuff\\4.3" + ], + "python.analysis.diagnosticSeverityOverrides": { + "reportInvalidTypeForm": "none" + }, + "python.REPL.enableREPLSmartSend": false, +} \ No newline at end of file diff --git a/core/properties.py b/core/properties.py new file mode 100644 index 0000000..530a53b --- /dev/null +++ b/core/properties.py @@ -0,0 +1,25 @@ +from bpy.types import Scene, PropertyGroup, Object, Material, TextureNode +from bpy.props import BoolProperty, EnumProperty, FloatProperty, IntProperty, CollectionProperty, StringProperty, FloatVectorProperty, PointerProperty +from bpy.utils import register_class + + + +def register_properties(): + class Material_Texture_Atlas_PropertyGroup(PropertyGroup): + normal: PointerProperty(type=TextureNode) + albedo: PointerProperty(type=TextureNode) + emission: PointerProperty(type=TextureNode) + ambient_occlusion: PointerProperty(type=TextureNode) + height: PointerProperty(type=TextureNode) + + + register_class(Material_Texture_Atlas_PropertyGroup) + Material.texture_atlas = PointerProperty(type=Material_Texture_Atlas_PropertyGroup) + + class Texture_Atlas_PropertyGroup(PropertyGroup): + materials: CollectionProperty(type=Material) + + Scene.texture_atlas_properties = PointerProperty(type=Texture_Atlas_PropertyGroup) + + + \ No newline at end of file diff --git a/functions/texture_atlas.py b/functions/texture_atlas.py new file mode 100644 index 0000000..e303838 --- /dev/null +++ b/functions/texture_atlas.py @@ -0,0 +1,23 @@ +import bpy +from typing import List, Optional +from bpy.types import Operator, Context, Object, TextureNode +from ..core.register import register_wrap +from ..core.common import get_armature, simplify_bonename + +@register_wrap +class Atlas_Textures(Operator): + bl_idname = "avatar_toolkit.atlas_textures" + bl_label = "Atlas Textures" + bl_description = """Combines materials and their textures to optimize the model. +Although this combines materials, it may not reduce your VRAM usage. Other tools +like Tuxedo can vastly reduce your VRAM usage as well as many other optimizations, +rather than just duct taping the textures together like material combiner and this tool. +""" + bl_options = {'REGISTER', 'UNDO'} + + def execute(self, context: Context) -> set: + + + return {'FINISHED'} + + From 23b4656859ef46dc9a3d23a3a67e572c08e382fb Mon Sep 17 00:00:00 2001 From: 989onan Date: Sun, 7 Jul 2024 22:36:10 -0400 Subject: [PATCH 05/17] Added part of Texture Atlas UI -added dynamic list for texture atlas ui - ui isn't the most intuitive but it will do for now --- __init__.py | 2 ++ core/properties.py | 38 +++++++++++++++-------- ui/atlas_materials.py | 71 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 13 deletions(-) create mode 100644 ui/atlas_materials.py diff --git a/__init__.py b/__init__.py index 4f93020..da7389e 100644 --- a/__init__.py +++ b/__init__.py @@ -7,6 +7,7 @@ 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 else: import importlib importlib.reload(ui) @@ -18,6 +19,7 @@ def register(): print("Registering Avatar Toolkit") # Order the classes before registration core.register.order_classes() + register_properties() # Register the UI classes # Iterate over the classes to register and register them diff --git a/core/properties.py b/core/properties.py index 530a53b..2773ca4 100644 --- a/core/properties.py +++ b/core/properties.py @@ -1,25 +1,37 @@ -from bpy.types import Scene, PropertyGroup, Object, Material, TextureNode +import bpy +from bpy.types import Scene, PropertyGroup, Object, Material, TextureNode, Context from bpy.props import BoolProperty, EnumProperty, FloatProperty, IntProperty, CollectionProperty, StringProperty, FloatVectorProperty, PointerProperty from bpy.utils import register_class def register_properties(): - class Material_Texture_Atlas_PropertyGroup(PropertyGroup): - normal: PointerProperty(type=TextureNode) - albedo: PointerProperty(type=TextureNode) - emission: PointerProperty(type=TextureNode) - ambient_occlusion: PointerProperty(type=TextureNode) - height: PointerProperty(type=TextureNode) + + #happy with how compressed this get_texture_node_list method is - @989onan + def get_texture_node_list(self: Material, context: Context) -> list[set[3]]: + if self.use_nodes: + Object.Enum = [(i.name+"_image",(i.image.name if i.image else "node with no image..."),i.name,index+1) for index,i in enumerate(self.node_tree.nodes) if i.bl_idname == "ShaderNodeTexImage"] + if not len(Object.Enum): + Object.Enum = [("ERROR", "THIS MATERIAL HAS NO IMAGES!", "ERROR", 0)] + else: + Object.Enum = [("ERROR", "THIS MATERIAL DOES NOT USE NODES!", "ERROR", 0)] + Object.Enum.append(("None", "None", "None", 0)) + return Object.Enum + + 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_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_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) + + Scene.texture_atlas_material_index = IntProperty()#default=-1, get=(lambda self : -1), set=(lambda self,context : None) - register_class(Material_Texture_Atlas_PropertyGroup) - Material.texture_atlas = PointerProperty(type=Material_Texture_Atlas_PropertyGroup) + #class Texture_Atlas_PropertyGroup(PropertyGroup): + # materials: CollectionProperty(type=Material) + #register_class(Texture_Atlas_PropertyGroup) - class Texture_Atlas_PropertyGroup(PropertyGroup): - materials: CollectionProperty(type=Material) - - Scene.texture_atlas_properties = PointerProperty(type=Texture_Atlas_PropertyGroup) + #Scene.texture_atlas_properties = PointerProperty(type=Texture_Atlas_PropertyGroup) \ No newline at end of file diff --git a/ui/atlas_materials.py b/ui/atlas_materials.py new file mode 100644 index 0000000..78735a5 --- /dev/null +++ b/ui/atlas_materials.py @@ -0,0 +1,71 @@ +from bpy.types import UIList, Panel, UILayout, Object,Context,MaterialSlot +import bpy +from ..core.register import register_wrap +from .panel import AvatarToolkitPanel + +@register_wrap +class MaterialTextureAtlasProperties(UIList): + bl_label = "Texture Atlas Material List Material" + bl_idname = "Material_UL_avatar_toolkit_texture_atlas_mat_list_mat" + bl_space_type = 'VIEW_3D' + bl_region_type = 'UI' + + + def draw_item(self, context: Context, layout: UILayout, data: bpy.types.Object, item:MaterialSlot, icon, active_data, active_propname, index): + + if item.material: + box = layout.box() + col = box.row() + col.label(text="Material: \""+item.material.name+"\"") + if data.active_material_index == index: + col = box.row() + col.prop(item.material, "texture_atlas_albedo") + col = box.row() + col.prop(item.material, "texture_atlas_normal") + col = box.row() + col.prop(item.material, "texture_atlas_emission") + col = box.row() + col.prop(item.material, "texture_atlas_ambient_occlusion") + col = box.row() + col.prop(item.material, "texture_atlas_height") + else: + box = layout.box() + col = box.row() + col.label(text="Empty Material Slot.") + +@register_wrap +class MaterialListPanel(UIList): + bl_label = "Texture Atlas Material List" + bl_idname = "Material_UL_avatar_toolkit_texture_atlas_mat_list" + bl_space_type = 'VIEW_3D' + bl_region_type = 'UI' + + def draw_item(self, context: Context, layout: UILayout, data, item:Object, icon, active_data, active_propname, index): + custom_icon = "OBJECT_DATAMODE" + box = layout.box() + row = box.row() + row.label(text=item.name, icon = custom_icon) + if context.scene.texture_atlas_material_index == index: + row = box.row() + box = row.box() + + box.template_list("Material_UL_avatar_toolkit_texture_atlas_mat_list_mat", "The_Texture_Atlas_List_mat_"+item.name, item, "material_slots", item, "active_material_index") + + +@register_wrap +class TextureAtlasPanel(Panel): + bl_label = "Texture Atlasing" + bl_idname = "OBJECT_PT_avatar_toolkit_texture_atlas" + bl_space_type = 'VIEW_3D' + bl_region_type = 'UI' + bl_category = "Avatar Toolkit" + bl_parent_id = "OBJECT_PT_avatar_toolkit" + + def draw(self, context: Context): + layout = self.layout + row = layout.row() + boxoutter = row.box() + row = boxoutter.row() + row.label(text=MaterialListPanel.bl_label) + row = boxoutter.row() + row.template_list("Material_UL_avatar_toolkit_texture_atlas_mat_list", "The_Texture_Atlas_List", context.scene, "objects", context.scene, "texture_atlas_material_index") From e875f9192a638634aa8107f4b0a4aa2b221537ea Mon Sep 17 00:00:00 2001 From: 989onan Date: Sun, 14 Jul 2024 15:36:01 -0400 Subject: [PATCH 06/17] Fixed the UI to be much better - ui for materials is now a list with no duplicates - auto detects that materials have changed and prompts the user to reload - due to context limitations in code, user is needed to reload the materials, but the ui is made so the user is forced to reload the materials to see them - later on, we should prevent user from atlasing if the material list is not up to date. --- core/properties.py | 51 ++++++++++++++++++++--- ui/atlas_materials.py | 97 +++++++++++++++++++++++++------------------ 2 files changed, 103 insertions(+), 45 deletions(-) diff --git a/core/properties.py b/core/properties.py index 2773ca4..abe3e17 100644 --- a/core/properties.py +++ b/core/properties.py @@ -1,12 +1,50 @@ import bpy -from bpy.types import Scene, PropertyGroup, Object, Material, TextureNode, Context +from bpy.types import Scene, PropertyGroup, Object, Material, TextureNode, Context, SceneObjects from bpy.props import BoolProperty, EnumProperty, FloatProperty, IntProperty, CollectionProperty, StringProperty, FloatVectorProperty, PointerProperty from bpy.utils import register_class +class material_list_bool: + old_list: list[Material] = [] + bool_material_list_expand: bool = False + + def set_bool(self, value: bool) -> None: + material_list_bool.bool_material_list_expand = value + if value == False: + material_list_bool.old_list = [] + + def get_bool(self) -> bool: + newlist: list[Material] = [] + for obj in bpy.context.scene.objects: + if len(obj.material_slots)>0: + for mat_slot in obj.material_slots: + if mat_slot.material: + if mat_slot.material not in newlist: + newlist.append(mat_slot.material) + + still_the_same: bool = True + for item in newlist: + if item not in material_list_bool.old_list: + still_the_same = False + break + for item in material_list_bool.old_list: + if item not in newlist: + still_the_same = False + break + + material_list_bool.bool_material_list_expand = still_the_same + + return material_list_bool.bool_material_list_expand + +class SceneMatClass(PropertyGroup): + mat: PointerProperty(type=Material) + + def register_properties(): + register_class(SceneMatClass) + #happy with how compressed this get_texture_node_list method is - @989onan def get_texture_node_list(self: Material, context: Context) -> list[set[3]]: if self.use_nodes: @@ -25,11 +63,14 @@ def register_properties(): 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) - Scene.texture_atlas_material_index = IntProperty()#default=-1, get=(lambda self : -1), set=(lambda self,context : None) + Scene.texture_atlas_material_index = IntProperty(default=-1, get=(lambda self : -1), set=(lambda self,context : None)) - #class Texture_Atlas_PropertyGroup(PropertyGroup): - # materials: CollectionProperty(type=Material) - #register_class(Texture_Atlas_PropertyGroup) + + + + 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) #Scene.texture_atlas_properties = PointerProperty(type=Texture_Atlas_PropertyGroup) diff --git a/ui/atlas_materials.py b/ui/atlas_materials.py index 78735a5..f0c8018 100644 --- a/ui/atlas_materials.py +++ b/ui/atlas_materials.py @@ -1,7 +1,38 @@ -from bpy.types import UIList, Panel, UILayout, Object,Context,MaterialSlot +from bpy.types import UIList, Panel, UILayout, Object, Context,Material, Operator import bpy from ..core.register import register_wrap from .panel import AvatarToolkitPanel +from ..core.properties import SceneMatClass, material_list_bool + + +@register_wrap +class ExpandSection_Materials(Operator): + bl_idname = 'avatar_toolkit.expand_section_materials' + bl_label = "" + bl_description = "" + + @classmethod + def poll(cls, context: Context) -> bool: + return True + + + def execute(self, context: Context) -> set: + + if not context.scene.texture_atlas_Has_Mat_List_Shown: + context.scene.materials.clear() + newlist: list[Material] = [] + for obj in bpy.context.scene.objects: + if len(obj.material_slots)>0: + for mat_slot in obj.material_slots: + if mat_slot.material: + if mat_slot.material not in newlist: + newlist.append(mat_slot.material) + newitem: SceneMatClass = context.scene.materials.add() + newitem.mat = mat_slot.material + material_list_bool.old_list = newlist + else: + context.scene.texture_atlas_Has_Mat_List_Shown = False + return {'FINISHED'} @register_wrap class MaterialTextureAtlasProperties(UIList): @@ -11,46 +42,25 @@ class MaterialTextureAtlasProperties(UIList): bl_region_type = 'UI' - def draw_item(self, context: Context, layout: UILayout, data: bpy.types.Object, item:MaterialSlot, icon, active_data, active_propname, index): + def draw_item(self , context: Context, layout: UILayout, data: bpy.types.Object, item:SceneMatClass, icon, active_data, active_propname, index): - if item.material: + if context.scene.texture_atlas_Has_Mat_List_Shown: box = layout.box() - col = box.row() - col.label(text="Material: \""+item.material.name+"\"") - if data.active_material_index == index: - col = box.row() - col.prop(item.material, "texture_atlas_albedo") - col = box.row() - col.prop(item.material, "texture_atlas_normal") - col = box.row() - col.prop(item.material, "texture_atlas_emission") - col = box.row() - col.prop(item.material, "texture_atlas_ambient_occlusion") - col = box.row() - col.prop(item.material, "texture_atlas_height") - else: - box = layout.box() - col = box.row() - col.label(text="Empty Material Slot.") - -@register_wrap -class MaterialListPanel(UIList): - bl_label = "Texture Atlas Material List" - bl_idname = "Material_UL_avatar_toolkit_texture_atlas_mat_list" - bl_space_type = 'VIEW_3D' - bl_region_type = 'UI' - - def draw_item(self, context: Context, layout: UILayout, data, item:Object, icon, active_data, active_propname, index): - custom_icon = "OBJECT_DATAMODE" - box = layout.box() - row = box.row() - row.label(text=item.name, icon = custom_icon) - if context.scene.texture_atlas_material_index == index: row = box.row() - box = row.box() + row.label(text=item.mat.name, icon = "MATERIAL") + col = box.row() + col.prop(item.mat, "texture_atlas_albedo") + col = box.row() + col.prop(item.mat, "texture_atlas_normal") + col = box.row() + col.prop(item.mat, "texture_atlas_emission") + col = box.row() + col.prop(item.mat, "texture_atlas_ambient_occlusion") + col = box.row() + col.prop(item.mat, "texture_atlas_height") - box.template_list("Material_UL_avatar_toolkit_texture_atlas_mat_list_mat", "The_Texture_Atlas_List_mat_"+item.name, item, "material_slots", item, "active_material_index") - + + @register_wrap class TextureAtlasPanel(Panel): @@ -65,7 +75,14 @@ class TextureAtlasPanel(Panel): layout = self.layout row = layout.row() boxoutter = row.box() + direction_icon = 'RIGHTARROW' if not context.scene.texture_atlas_Has_Mat_List_Shown else 'DOWNARROW_HLT' row = boxoutter.row() - row.label(text=MaterialListPanel.bl_label) - row = boxoutter.row() - row.template_list("Material_UL_avatar_toolkit_texture_atlas_mat_list", "The_Texture_Atlas_List", context.scene, "objects", context.scene, "texture_atlas_material_index") + row.operator(ExpandSection_Materials.bl_idname, text=("Reload Texture Atlas Material List" if not context.scene.texture_atlas_Has_Mat_List_Shown else "Loaded Texture Atlas Material List"), icon=direction_icon) + if context.scene.texture_atlas_Has_Mat_List_Shown: + + #get_texture_node_list(bpy.context) + + row = boxoutter.row() + row.template_list(MaterialTextureAtlasProperties.bl_idname, 'material_list', context.scene, 'materials', + context.scene, 'texture_atlas_material_index', rows=12, type='DEFAULT') + \ No newline at end of file From 942e7e286800fb6a50d40c343141d9a104fa38b7 Mon Sep 17 00:00:00 2001 From: 989onan Date: Sun, 14 Jul 2024 23:55:20 -0400 Subject: [PATCH 07/17] Got images working - does not do UVs yet - is able to pack images using a split algorithm. I think I broke the size finding though for the output canvas. - does not combine materials after packing --- core/packer/rectangle_packer.py | 151 +++++++++++++++++++++++++++ core/properties.py | 42 +++++--- core/register.py | 2 +- functions/atlas_materials.py | 178 ++++++++++++++++++++++++++++++++ functions/texture_atlas.py | 2 +- ui/atlas_materials.py | 6 +- 6 files changed, 360 insertions(+), 21 deletions(-) create mode 100644 core/packer/rectangle_packer.py create mode 100644 functions/atlas_materials.py diff --git a/core/packer/rectangle_packer.py b/core/packer/rectangle_packer.py new file mode 100644 index 0000000..b8b8b0a --- /dev/null +++ b/core/packer/rectangle_packer.py @@ -0,0 +1,151 @@ + +# thank you https://stackoverflow.com/a/71432759 +from __future__ import annotations + + +from typing import Optional +from bpy.types import Image + + +# Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016 Jake Gordon and contributors +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +class Rectangle_Obj: + x: int = 0 + y: int = 0 + w: int = 0 + h: int = 0 + down: Rectangle_Obj = None + used: bool = False + right: Rectangle_Obj = None + + def __init__(self, x:int, y:int, w:int, h:int, down=None, used =False, right=None): + self.x = x + self.y = y + self.w = w + self.h = h + self.down = down + self.used = used + self.right = right + + def split(self, w, h) -> Rectangle_Obj: + self.used = True + self.down = Rectangle_Obj(x=self.x, y=self.y + h, w=self.w, h=self.h - h) + self.right = Rectangle_Obj(x=self.x + w, y=self.y, w=self.w - w, h=h) + return self + + def find(self, w, h) -> Optional[Rectangle_Obj]: + if self.used: + return self.right.find(w, h) or self.down.find(w, h) + elif (w <= self.w) and (h <= self.h): + return self + return None + +class MaterialImageList: + albedo: Image + normal: Image + emission: Image + ambient_occlusion: Image + height: Image + fit: Rectangle_Obj + + def __init__(self): + pass + + x: int = 0 + y: int = 0 + w: int = 0 + h: int = 0 + + + + + +class BinPacker(object): + root: Rectangle_Obj + bin: list[MaterialImageList] = [] + def __init__(self, structure: list[MaterialImageList]): + self.root = None + self.bin = structure + + def fit(self): + structure = self.bin + structure_len = len(self.bin) + w: int = 0 + h: int = 0 + if structure_len > 0: + w = structure[0].w + h = structure[0].h + self.root = Rectangle_Obj(x=0, y=0, w=w, h=h) + for img in structure: + w = img.w + h = img.h + node = self.root.find(w, h) + if node: + img.fit = node.split(w, h) + else: + img.fit = self.grow_node(w, h) + return structure + + def grow_node(self, w, h) -> Optional[Rectangle_Obj]: + can_grow_right = (h <= self.root.h) + can_grow_down = (w <= self.root.w) + + should_grow_right = can_grow_right and (self.root.h >= (self.root.w + w)) + should_grow_down = can_grow_down and (self.root.w >= (self.root.h + h)) + + if should_grow_right: + return self.grow_right(w, h) + elif should_grow_down: + return self.grow_down(w, h) + elif can_grow_right: + return self.grow_right(w, h) + elif can_grow_down: + return self.grow_down(w, h) + return None + + def grow_right(self, w, h) -> Optional[Rectangle_Obj]: + self.root = Rectangle_Obj( + used=True, + x=0, + y=0, + w=self.root.w + w, + h=self.root.h, + down=self.root, + right=Rectangle_Obj(x=self.root.w, y=0, w=w, h=self.root.h)) + node = self.root.find(w, h) + if node: + return node.split(w, h) + return None + + def grow_down(self, w, h) -> Optional[Rectangle_Obj]: + self.root = Rectangle_Obj( + used=True, + x=0, + y=0, + w=self.root.w, + h=self.root.h + h, + down=Rectangle_Obj(x=0, y=self.root.h, w=self.root.w, h=h), + right=self.root + ) + node = self.root.find(w, h) + if node: + return node.split(w, h) + return None \ No newline at end of file diff --git a/core/properties.py b/core/properties.py index abe3e17..5cb1141 100644 --- a/core/properties.py +++ b/core/properties.py @@ -6,13 +6,18 @@ from bpy.utils import register_class class material_list_bool: - old_list: list[Material] = [] - bool_material_list_expand: bool = False + #For the love that is holy do not ever touch these. If this was java I would make these private + #They should only be accessed via context.scene.texture_atlas_Has_Mat_List_Shown + #This is so we know if the materials are up to date. messing with these variables directly will make the thing blow up. + + #The only exception to this is the ExpandSection_Materials operator which populates this with new data once the materials have changed and need reloading. + old_list: dict[str,list[Material]] = {} + bool_material_list_expand: dict[str,bool] = {} def set_bool(self, value: bool) -> None: - material_list_bool.bool_material_list_expand = value + material_list_bool.bool_material_list_expand[bpy.context.scene.name] = value if value == False: - material_list_bool.old_list = [] + material_list_bool.old_list[bpy.context.scene.name] = [] def get_bool(self) -> bool: newlist: list[Material] = [] @@ -24,18 +29,20 @@ class material_list_bool: newlist.append(mat_slot.material) still_the_same: bool = True - for item in newlist: - if item not in material_list_bool.old_list: - still_the_same = False - break - for item in material_list_bool.old_list: - if item not in newlist: - still_the_same = False - break - - material_list_bool.bool_material_list_expand = still_the_same + if bpy.context.scene.name in material_list_bool.old_list: + for item in newlist: + if item not in material_list_bool.old_list[bpy.context.scene.name]: + still_the_same = False + break + for item in material_list_bool.old_list[bpy.context.scene.name]: + if item not in newlist: + still_the_same = False + break + else: + still_the_same = False + material_list_bool.bool_material_list_expand[bpy.context.scene.name] = still_the_same - return material_list_bool.bool_material_list_expand + return material_list_bool.bool_material_list_expand[bpy.context.scene.name] class SceneMatClass(PropertyGroup): mat: PointerProperty(type=Material) @@ -48,7 +55,7 @@ def register_properties(): #happy with how compressed this get_texture_node_list method is - @989onan def get_texture_node_list(self: Material, context: Context) -> list[set[3]]: if self.use_nodes: - Object.Enum = [(i.name+"_image",(i.image.name if i.image else "node with no image..."),i.name,index+1) for index,i in enumerate(self.node_tree.nodes) if i.bl_idname == "ShaderNodeTexImage"] + Object.Enum = [((i.image.name if i.image else i.name+"_image"),(i.image.name if i.image else "node with no image..."),(i.image.name if i.image else i.name),index+1) for index,i in enumerate(self.node_tree.nodes) if i.bl_idname == "ShaderNodeTexImage"] if not len(Object.Enum): Object.Enum = [("ERROR", "THIS MATERIAL HAS NO IMAGES!", "ERROR", 0)] else: @@ -57,8 +64,9 @@ def register_properties(): return Object.Enum - 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_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) diff --git a/core/register.py b/core/register.py index 0d91a8a..2c20729 100644 --- a/core/register.py +++ b/core/register.py @@ -32,7 +32,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/atlas_materials.py b/functions/atlas_materials.py new file mode 100644 index 0000000..39f3aa3 --- /dev/null +++ b/functions/atlas_materials.py @@ -0,0 +1,178 @@ +from pathlib import Path +import bpy +import re +import os +from typing import List, Tuple, Optional +from bpy.types import Material, Operator, Context, Object, Image +from ..core.register import register_wrap +from ..core.properties import material_list_bool, SceneMatClass +from ..core.packer.rectangle_packer import MaterialImageList, BinPacker + + + + + +def scale_images_to_largest(images:list[Image]) -> set: + print([image.name for image in images]) + x: int=0 + y: int=0 + for image in images: + x = max(x,image.size[0]) + y = max(y,image.size[1]) + print(x,y) + + for image in images: + image.scale(width=int(x), height=int(y)) + + return x,y + +def MaterialImageList_to_Image_list(classitem: MaterialImageList) -> list[Image]: + list_of_images: list[Image] = [] + + list_of_images.append(classitem.albedo) + list_of_images.append(classitem.normal) + list_of_images.append(classitem.emission) + list_of_images.append(classitem.ambient_occlusion) + list_of_images.append(classitem.height) + + return list_of_images + + +def get_material_images_from_scene(context: Context) -> list[MaterialImageList]: + mat: SceneMatClass = None + material_image_list: list[MaterialImageList] = [] + for mat in context.scene.materials: + new_mat_image_item: MaterialImageList = MaterialImageList() + try: + new_mat_image_item.albedo = bpy.data.images[mat.mat.texture_atlas_albedo] + except Exception as e: + name: str = mat.mat.name+"_albedo_replacement" + if name in bpy.data.images: + bpy.data.images.remove(image=bpy.data.images[name],do_unlink=True) + new_mat_image_item.albedo = bpy.data.images.new(name=name,width=32,height=32, alpha=True) + try: + new_mat_image_item.normal = bpy.data.images[mat.mat.texture_atlas_normal] + except Exception: + name: str = mat.mat.name+"_normal_replacement" + if name in bpy.data.images: + bpy.data.images.remove(image=bpy.data.images[name],do_unlink=True) + new_mat_image_item.normal = bpy.data.images.new(name=name,width=32,height=32, alpha=True) + try: + new_mat_image_item.emission = bpy.data.images[mat.mat.texture_atlas_emission] + except Exception: + name: str = mat.mat.name+"_emission_replacement" + if name in bpy.data.images: + bpy.data.images.remove(image=bpy.data.images[name],do_unlink=True) + new_mat_image_item.emission = bpy.data.images.new(name=name,width=32,height=32, alpha=True) + + try: + new_mat_image_item.ambient_occlusion = bpy.data.images[mat.mat.texture_atlas_ambient_occlusion] + except Exception: + name: str = mat.mat.name+"_ambient_occlusion_replacement" + if name in bpy.data.images: + bpy.data.images.remove(image=bpy.data.images[name],do_unlink=True) + new_mat_image_item.ambient_occlusion = bpy.data.images.new(name=name,width=32,height=32, alpha=True) + try: + new_mat_image_item.height = bpy.data.images[mat.mat.texture_atlas_height] + except Exception: + name: str = mat.mat.name+"_height_replacement" + if name in bpy.data.images: + bpy.data.images.remove(image=bpy.data.images[name],do_unlink=True) + new_mat_image_item.height = bpy.data.images.new(name=name,width=32,height=32, alpha=True) + material_image_list.append(new_mat_image_item) + return material_image_list + + +def prep_images_in_scene(context: Context) -> list[MaterialImageList]: + preped_images: list[MaterialImageList] = get_material_images_from_scene(context) + for MaterialImageClass in preped_images: + ImageList: list[Image] = MaterialImageList_to_Image_list(MaterialImageClass) + + MaterialImageClass.w, MaterialImageClass.h = scale_images_to_largest(ImageList) + + + + return preped_images + + + + + + + + + + + + + +@register_wrap +class Atlas_Materials(Operator): + + bl_idname = "avatar_toolkit.atlas_materials" + bl_label = "Atlas Materials" + bl_description = "Atlas materials to optimize the model" + bl_options = {'REGISTER', 'UNDO'} + + @classmethod + def poll(cls, context: Context) -> bool: + return context.scene.texture_atlas_Has_Mat_List_Shown + + def execute(self, context: Context) -> set: + try: + mat_images: list[MaterialImageList] = prep_images_in_scene(context) + + packer: BinPacker = BinPacker(mat_images) + + mat_images = packer.fit() + + + size: list[int] = [max([matimg.fit.w + matimg.albedo.size[0] for matimg in mat_images]), + max([matimg.fit.h + matimg.albedo.size[1] for matimg in mat_images])] + print([matimg.fit.w + matimg.albedo.size[1] for matimg in mat_images]) + + for type in ["albedo","normal", "emission","ambient_occlusion","height"]: + new_image_name: str= "Atlas_"+type+"_"+bpy.context.scene.name+"_"+Path(bpy.data.filepath).stem + + print("Processing "+type+" atlas image") + + if new_image_name in bpy.data.images: + bpy.data.images.remove(bpy.data.images[new_image_name]) + + canvas: Image = bpy.data.images.new(name=new_image_name, width=int(size[0]),height=int(size[1]), alpha=True) + c_w = canvas.size[0] + c_h = canvas.size[1] + canvas_pixels: list[float] = list(canvas.pixels[:]) + for mat in mat_images: + x: int = int(mat.fit.x) + y: int = int(mat.fit.y) + w: int = int(mat.albedo.size[0]) + h: int = int(mat.albedo.size[1]) + + image_var: Image = eval("mat."+type) + + image_pixels: list[float] = list(image_var.pixels[:]) + + print("writing image \""+image_var.name+"\" to canvas.") + print("x: \""+str(x)+"\" "+"y: \""+str(y)+"\" "+"w: \""+str(w)+"\" "+"h: \""+str(h)+"\" ") + for k in range(0,h): + for i in range(0, w): + for channel in range(0,4): + canvas_pixels[ + int((((k+y)*c_w) + + + (i+x))*4) + +int(channel) + ] = image_pixels[ + int(( + (k*w) + +i)*4) + +int(channel)] + + canvas.pixels[:] = canvas_pixels[:] + canvas.save(filepath=os.path.join(os.path.dirname(bpy.data.filepath),new_image_name+".png")) + return {"FINISHED"} + except Exception as e: + raise e + return {"FINISHED"} + \ No newline at end of file diff --git a/functions/texture_atlas.py b/functions/texture_atlas.py index e303838..90a4aec 100644 --- a/functions/texture_atlas.py +++ b/functions/texture_atlas.py @@ -16,8 +16,8 @@ rather than just duct taping the textures together like material combiner and th bl_options = {'REGISTER', 'UNDO'} def execute(self, context: Context) -> set: - + return {'FINISHED'} diff --git a/ui/atlas_materials.py b/ui/atlas_materials.py index f0c8018..4a9b245 100644 --- a/ui/atlas_materials.py +++ b/ui/atlas_materials.py @@ -3,6 +3,7 @@ import bpy from ..core.register import register_wrap from .panel import AvatarToolkitPanel from ..core.properties import SceneMatClass, material_list_bool +from ..functions.atlas_materials import Atlas_Materials @register_wrap @@ -29,7 +30,7 @@ class ExpandSection_Materials(Operator): newlist.append(mat_slot.material) newitem: SceneMatClass = context.scene.materials.add() newitem.mat = mat_slot.material - material_list_bool.old_list = newlist + material_list_bool.old_list[context.scene.name] = newlist else: context.scene.texture_atlas_Has_Mat_List_Shown = False return {'FINISHED'} @@ -85,4 +86,5 @@ class TextureAtlasPanel(Panel): row = boxoutter.row() row.template_list(MaterialTextureAtlasProperties.bl_idname, 'material_list', context.scene, 'materials', context.scene, 'texture_atlas_material_index', rows=12, type='DEFAULT') - \ No newline at end of file + row = layout.row() + row.operator(Atlas_Materials.bl_idname, text="Atlas Materials!") \ No newline at end of file From 5a3cc5a087291178a65ef58f10a0e100e46c8cad Mon Sep 17 00:00:00 2001 From: 989onan Date: Mon, 15 Jul 2024 01:51:59 -0400 Subject: [PATCH 08/17] Finally works This is a good first start to material combining It may need a few tweaks from here, but for now it should be good --- core/packer/rectangle_packer.py | 4 +- core/properties.py | 1 + functions/atlas_materials.py | 126 ++++++++++++++++++++++++++++---- ui/atlas_materials.py | 2 + 4 files changed, 117 insertions(+), 16 deletions(-) diff --git a/core/packer/rectangle_packer.py b/core/packer/rectangle_packer.py index b8b8b0a..fc147ce 100644 --- a/core/packer/rectangle_packer.py +++ b/core/packer/rectangle_packer.py @@ -4,7 +4,7 @@ from __future__ import annotations from typing import Optional -from bpy.types import Image +from bpy.types import Image, Material # Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016 Jake Gordon and contributors @@ -64,7 +64,9 @@ class MaterialImageList: emission: Image ambient_occlusion: Image height: Image + roughness: Image fit: Rectangle_Obj + material: Material def __init__(self): pass diff --git a/core/properties.py b/core/properties.py index 5cb1141..9a15d2f 100644 --- a/core/properties.py +++ b/core/properties.py @@ -70,6 +70,7 @@ def register_properties(): 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) Scene.texture_atlas_material_index = IntProperty(default=-1, get=(lambda self : -1), set=(lambda self,context : None)) diff --git a/functions/atlas_materials.py b/functions/atlas_materials.py index 39f3aa3..60a871d 100644 --- a/functions/atlas_materials.py +++ b/functions/atlas_materials.py @@ -1,9 +1,12 @@ from pathlib import Path + +import numpy import bpy import re import os from typing import List, Tuple, Optional -from bpy.types import Material, Operator, Context, Object, Image +from mathutils import Vector +from bpy.types import Material, Operator, Context, Object, Image, Mesh, MeshUVLoopLayer, Float2AttributeValue, ShaderNodeTexImage, ShaderNodeBsdfPrincipled, ShaderNodeNormalMap from ..core.register import register_wrap from ..core.properties import material_list_bool, SceneMatClass from ..core.packer.rectangle_packer import MaterialImageList, BinPacker @@ -34,6 +37,7 @@ def MaterialImageList_to_Image_list(classitem: MaterialImageList) -> list[Image] list_of_images.append(classitem.emission) list_of_images.append(classitem.ambient_occlusion) list_of_images.append(classitem.height) + list_of_images.append(classitem.roughness) return list_of_images @@ -64,6 +68,7 @@ def get_material_images_from_scene(context: Context) -> list[MaterialImageList]: if name in bpy.data.images: bpy.data.images.remove(image=bpy.data.images[name],do_unlink=True) new_mat_image_item.emission = bpy.data.images.new(name=name,width=32,height=32, alpha=True) + new_mat_image_item.emission.pixels[:] = numpy.tile(numpy.array([0.0,0.0,0.0,1.0]), 32*32) try: new_mat_image_item.ambient_occlusion = bpy.data.images[mat.mat.texture_atlas_ambient_occlusion] @@ -72,6 +77,7 @@ def get_material_images_from_scene(context: Context) -> list[MaterialImageList]: if name in bpy.data.images: bpy.data.images.remove(image=bpy.data.images[name],do_unlink=True) new_mat_image_item.ambient_occlusion = bpy.data.images.new(name=name,width=32,height=32, alpha=True) + new_mat_image_item.ambient_occlusion.pixels[:] = numpy.tile(numpy.array([1.0,1.0,1.0,1.0]), 32*32) try: new_mat_image_item.height = bpy.data.images[mat.mat.texture_atlas_height] except Exception: @@ -79,6 +85,17 @@ def get_material_images_from_scene(context: Context) -> list[MaterialImageList]: if name in bpy.data.images: bpy.data.images.remove(image=bpy.data.images[name],do_unlink=True) new_mat_image_item.height = bpy.data.images.new(name=name,width=32,height=32, alpha=True) + new_mat_image_item.height.pixels[:] = numpy.tile(numpy.array([0.5,0.5,0.5,1.0]), 32*32) + + try: + new_mat_image_item.roughness = bpy.data.images[mat.mat.texture_atlas_roughness] + except Exception: + name: str = mat.mat.name+"_roughness_replacement" + if name in bpy.data.images: + bpy.data.images.remove(image=bpy.data.images[name],do_unlink=True) + new_mat_image_item.roughness = bpy.data.images.new(name=name,width=32,height=32, alpha=True) + new_mat_image_item.roughness.pixels[:] = numpy.tile(numpy.array([1.0,1.0,1.0,0.0]), 32*32) + new_mat_image_item.material = mat.mat material_image_list.append(new_mat_image_item) return material_image_list @@ -95,17 +112,6 @@ def prep_images_in_scene(context: Context) -> list[MaterialImageList]: return preped_images - - - - - - - - - - - @register_wrap class Atlas_Materials(Operator): @@ -131,8 +137,30 @@ class Atlas_Materials(Operator): max([matimg.fit.h + matimg.albedo.size[1] for matimg in mat_images])] print([matimg.fit.w + matimg.albedo.size[1] for matimg in mat_images]) - for type in ["albedo","normal", "emission","ambient_occlusion","height"]: - new_image_name: str= "Atlas_"+type+"_"+bpy.context.scene.name+"_"+Path(bpy.data.filepath).stem + atlased_mat: MaterialImageList = MaterialImageList() + + for mat in mat_images: + x: int = int(mat.fit.x) + y: int = int(mat.fit.y) + w: int = int(mat.albedo.size[0]) + h: int = int(mat.albedo.size[1]) + + for obj in bpy.data.objects: + mesh: Mesh = obj.data + + + for layer in mesh.polygons: + if obj.material_slots[layer.material_index].material: + if obj.material_slots[layer.material_index].material == mat.material: + for loop_idx in layer.loop_indices: + layer_loops: MeshUVLoopLayer + for layer_loops in mesh.uv_layers: + uv_item: Float2AttributeValue = layer_loops.uv[loop_idx] + uv_item.vector.x = (uv_item.vector.x*(w/size[0]))+(x/size[0]) + uv_item.vector.y = (uv_item.vector.y*(h/size[1]))+(y/size[1]) + + for type in ["albedo","normal", "emission","ambient_occlusion","height", "roughness"]: + new_image_name: str= "Atlas_"+type+"_"+context.scene.name+"_"+Path(bpy.data.filepath).stem print("Processing "+type+" atlas image") @@ -141,7 +169,7 @@ class Atlas_Materials(Operator): canvas: Image = bpy.data.images.new(name=new_image_name, width=int(size[0]),height=int(size[1]), alpha=True) c_w = canvas.size[0] - c_h = canvas.size[1] + #c_h = canvas.size[1] canvas_pixels: list[float] = list(canvas.pixels[:]) for mat in mat_images: x: int = int(mat.fit.x) @@ -171,6 +199,74 @@ class Atlas_Materials(Operator): canvas.pixels[:] = canvas_pixels[:] canvas.save(filepath=os.path.join(os.path.dirname(bpy.data.filepath),new_image_name+".png")) + exec("atlased_mat."+type+" = canvas") + + + atlased_mat.material = bpy.data.materials.new(name="Atlas_Final_"+bpy.context.scene.name+"_"+Path(bpy.data.filepath).stem) + atlased_mat.material.use_nodes = True + atlased_mat.material.node_tree.nodes.clear() + + + #I am sorry for the amount of nodes I'm instanciating here and their values. + #This is so that the nodes look pretty in the UI, which I think looks kinda nice. - @989onan + principled_node: ShaderNodeBsdfPrincipled = atlased_mat.material.node_tree.nodes.new(type="ShaderNodeBsdfPrincipled") + principled_node.location.x = 7.29706335067749 + principled_node.location.y = 298.918212890625 + + output_node: ShaderNodeTexImage = atlased_mat.material.node_tree.nodes.new(type="ShaderNodeOutputMaterial") + output_node.location.x = 297.29705810546875 + output_node.location.y = 298.918212890625 + + albedo_node: ShaderNodeTexImage = atlased_mat.material.node_tree.nodes.new(type="ShaderNodeTexImage") + albedo_node.location.x = -588.6177978515625 + albedo_node.location.y = 414.1948547363281 + albedo_node.image = atlased_mat.albedo + + emission_node: ShaderNodeTexImage = atlased_mat.material.node_tree.nodes.new(type="ShaderNodeTexImage") + emission_node.location.x = -588.6177978515625 + emission_node.location.y = -173.9259033203125 + emission_node.image = atlased_mat.emission + + normal_node: ShaderNodeTexImage = atlased_mat.material.node_tree.nodes.new(type="ShaderNodeTexImage") + normal_node.location.x = -941.4189453125 + normal_node.location.y = -20.8391780853271 + normal_node.image = atlased_mat.normal + + normal_map_node: ShaderNodeNormalMap = atlased_mat.material.node_tree.nodes.new(type="ShaderNodeNormalMap") + normal_map_node.location.x = -545.550537109375 + normal_map_node.location.y = -0.7543716430664062 + + roughness_node: ShaderNodeTexImage = atlased_mat.material.node_tree.nodes.new(type="ShaderNodeTexImage") + roughness_node.location.x = -592.1703491210938 + roughness_node.location.y = 206.74075317382812 + roughness_node.image = atlased_mat.roughness + + ambient_occlusion_node: ShaderNodeTexImage = atlased_mat.material.node_tree.nodes.new(type="ShaderNodeTexImage") + ambient_occlusion_node.location.x = -906.4371337890625 + ambient_occlusion_node.location.y = -389.9602355957031 + ambient_occlusion_node.image = atlased_mat.ambient_occlusion + + height_node: ShaderNodeTexImage = atlased_mat.material.node_tree.nodes.new(type="ShaderNodeTexImage") + height_node.location.x = -1222.383056640625 + height_node.location.y = -375.48406982421875 + height_node.image = atlased_mat.height + + atlased_mat.material.node_tree.links.new(principled_node.inputs["Base Color"], albedo_node.outputs["Color"]) + atlased_mat.material.node_tree.links.new(principled_node.inputs["Metallic"], roughness_node.outputs["Alpha"]) + atlased_mat.material.node_tree.links.new(principled_node.inputs["Roughness"], roughness_node.outputs["Color"]) + atlased_mat.material.node_tree.links.new(principled_node.inputs["Alpha"], albedo_node.outputs["Alpha"]) + atlased_mat.material.node_tree.links.new(principled_node.inputs["Normal"], normal_map_node.outputs["Normal"]) + atlased_mat.material.node_tree.links.new(principled_node.inputs["Emission Color"], emission_node.outputs["Color"]) + atlased_mat.material.node_tree.links.new(output_node.inputs["Surface"], principled_node.outputs["BSDF"]) + atlased_mat.material.node_tree.links.new(normal_map_node.inputs["Color"], normal_node.outputs["Color"]) + + + for obj in context.scene.objects: + mesh: Mesh = obj.data + mesh.materials.clear() + + mesh.materials.append(atlased_mat.material) + return {"FINISHED"} except Exception as e: raise e diff --git a/ui/atlas_materials.py b/ui/atlas_materials.py index 4a9b245..0596846 100644 --- a/ui/atlas_materials.py +++ b/ui/atlas_materials.py @@ -59,6 +59,8 @@ class MaterialTextureAtlasProperties(UIList): col.prop(item.mat, "texture_atlas_ambient_occlusion") col = box.row() col.prop(item.mat, "texture_atlas_height") + col = box.row() + col.prop(item.mat, "texture_atlas_roughness") From 06c7cff4b7a7823a8006a9d1824ededadfa5d354 Mon Sep 17 00:00:00 2001 From: Yusarina Date: Mon, 22 Jul 2024 23:13:10 +0100 Subject: [PATCH 09/17] 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 10/17] 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 11/17] 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 ce7c6aa664a1756ae796d7f9ffcfadea0019a0a7 Mon Sep 17 00:00:00 2001 From: 989onan Date: Wed, 24 Jul 2024 17:41:17 -0400 Subject: [PATCH 12/17] Add digitgrade legs tool --- core/common.py | 11 ++++ functions/digitigrade_legs.py | 117 ++++++++++++++++++++++++++++++++++ functions/translations.py | 2 +- ui/tools.py | 4 +- 4 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 functions/digitigrade_legs.py diff --git a/core/common.py b/core/common.py index 6e08f93..2c7ce75 100644 --- a/core/common.py +++ b/core/common.py @@ -57,3 +57,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 duplicatebone(b: bpy.types.EditBone) -> bpy.types.EditBone: + arm = bpy.context.object.data + cb = arm.edit_bones.new(b.name) + + cb.head = b.head + cb.tail = b.tail + cb.matrix = b.matrix + cb.parent = b.parent + return cb \ No newline at end of file diff --git a/functions/digitigrade_legs.py b/functions/digitigrade_legs.py new file mode 100644 index 0000000..27ea3e5 --- /dev/null +++ b/functions/digitigrade_legs.py @@ -0,0 +1,117 @@ +import bpy +from ..core import common +from ..core import register_wrap +from .translations import t +import re + + +@register_wrap +class CreateDigitigradeLegs(bpy.types.Operator): + bl_idname = "avatar_toolkit.createdigitigradelegs" + bl_label = t('Tools.create_digitigrade_legs.label') + bl_description = t('Tools.create_digitigrade_legs.desc') + + @classmethod + def poll(cls, context): + if(context.active_object is None): + return False + if(context.selected_editable_bones is not None): + if(len(context.selected_editable_bones) == 2): + return True + return False + + def execute(self, context): + + for digi0 in context.selected_editable_bones: + digi1: bpy.types.EditBone = None + digi2: bpy.types.EditBone = None + digi3: bpy.types.EditBone = None + + try: + digi1 = digi0.children[0] + digi2 = digi1.children[0] + digi3 = digi2.children[0] + except: + print("bone format incorrect! Please select a chain of 4 continious bones!") #TODO: Show this to user. this is an error. + return {'CANCELLED'} + digi4 = None + try: + digi4 = digi3.children[0] + + except: + print("no toe bone. Continuing.") + digi0.select = True + digi1.select = True + digi2.select = True + digi3.select = True + if(digi4): + digi4.select = True + bpy.ops.armature.roll_clear() + bpy.ops.armature.select_all(action='DESELECT') + + #creating transform for upper leg + digi0.select = True + bpy.ops.transform.create_orientation(name="Toolkit_digi0", overwrite=True) + bpy.ops.armature.select_all(action='DESELECT') + + + #duplicate digi0 and assign it to thigh + thigh = common.duplicatebone(digi0) + bpy.ops.armature.select_all(action='DESELECT') + + #make digi2 parrallel to digi1 + digi2.align_orientation(digi0) + + #extrude thigh + thigh.select_tail = True + bpy.ops.armature.extrude_move(ARMATURE_OT_extrude={"forked":False},TRANSFORM_OT_translate=None) + #set new bone to calf varible + bpy.ops.armature.select_more() + calf = context.selected_bones[0] + bpy.ops.armature.select_all(action='DESELECT') + + #set calf end to digi2 end + calf.tail = digi2.tail + + #make copy of calf, flip it, and then align bone so that it's head is moved to match in align phase + flipedcalf = common.duplicatebone(calf) + bpy.ops.armature.select_all(action='DESELECT') + flipedcalf.select = True + bpy.ops.armature.switch_direction() + bpy.ops.armature.select_all(action='DESELECT') + flippeddigi1 = common.duplicatebone(digi1) + bpy.ops.armature.select_all(action='DESELECT') + flippeddigi1.select = True + bpy.ops.armature.switch_direction() + bpy.ops.armature.select_all(action='DESELECT') + + + + #align flipped calf to flipped middle leg to move the head + flipedcalf.align_orientation(flippeddigi1) + + flipedcalf.length = flippeddigi1.length + + #assign calf tail to flipped calf head so it moves calf's tail to be out at the perfect parallelagram + calf.head = flipedcalf.tail + + #delete helper bones + bpy.ops.armature.select_all(action='DESELECT') + flippeddigi1.select = True + bpy.ops.armature.delete() + bpy.ops.armature.select_all(action='DESELECT') + flipedcalf.select = True + bpy.ops.armature.delete() + bpy.ops.armature.select_all(action='DESELECT') + + + + #reparent the foot to the new calf so it will be part of the new foot IK chain + digi3.parent = calf + #Tada! It's done! now to rename the old 3 segments that make up the old part to noik so resonite doesn't try to select them + + digi0.name = re.compile(re.escape(""), re.IGNORECASE).sub("",digi0.name)+"" + digi1.name = re.compile(re.escape(""), re.IGNORECASE).sub("",digi1.name)+"" + digi2.name = re.compile(re.escape(""), re.IGNORECASE).sub("",digi2.name)+"" + #finally fully done! + return {'FINISHED'} \ No newline at end of file diff --git a/functions/translations.py b/functions/translations.py index 4f84a1d..a265c81 100644 --- a/functions/translations.py +++ b/functions/translations.py @@ -44,7 +44,7 @@ def load_translations() -> None: print("Default translation file 'en_US.json' not found.") def t(phrase: str, *args, **kwargs) -> str: - output: str = dictionary.get(phrase) + output: str = dictionary.get(phrase, None) if output is None: if verbose: print('Warning: Unknown phrase: ' + phrase) diff --git a/ui/tools.py b/ui/tools.py index 408a95d..0593bcf 100644 --- a/ui/tools.py +++ b/ui/tools.py @@ -2,6 +2,7 @@ import bpy from ..core.register import register_wrap from .panel import AvatarToolkitPanel from bpy.types import Context +from ..functions.digitigrade_legs import CreateDigitigradeLegs @register_wrap class AvatarToolkitToolsPanel(bpy.types.Panel): @@ -19,4 +20,5 @@ class AvatarToolkitToolsPanel(bpy.types.Panel): row = layout.row(align=True) row.scale_y = 1.5 - row.operator("avatar_toolkit.convert_to_resonite", text="Translate to Resonite") \ No newline at end of file + row.operator("avatar_toolkit.convert_to_resonite", text="Translate to Resonite") + row.operator(CreateDigitigradeLegs.bl_idname, text="Create Digitigrade Legs") \ No newline at end of file From 6d4b11585503475470f62c23a473eb6e66bc7117 Mon Sep 17 00:00:00 2001 From: 989onan Date: Wed, 24 Jul 2024 17:49:26 -0400 Subject: [PATCH 13/17] oops took changes from another branch, this should yeet --- core/packer/rectangle_packer.py | 153 ------------------ core/properties.py | 2 + functions/atlas_materials.py | 274 -------------------------------- functions/texture_atlas.py | 23 --- functions/translations.py | 2 +- 5 files changed, 3 insertions(+), 451 deletions(-) delete mode 100644 core/packer/rectangle_packer.py delete mode 100644 functions/atlas_materials.py delete mode 100644 functions/texture_atlas.py diff --git a/core/packer/rectangle_packer.py b/core/packer/rectangle_packer.py deleted file mode 100644 index fc147ce..0000000 --- a/core/packer/rectangle_packer.py +++ /dev/null @@ -1,153 +0,0 @@ - -# thank you https://stackoverflow.com/a/71432759 -from __future__ import annotations - - -from typing import Optional -from bpy.types import Image, Material - - -# Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016 Jake Gordon and contributors -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - -class Rectangle_Obj: - x: int = 0 - y: int = 0 - w: int = 0 - h: int = 0 - down: Rectangle_Obj = None - used: bool = False - right: Rectangle_Obj = None - - def __init__(self, x:int, y:int, w:int, h:int, down=None, used =False, right=None): - self.x = x - self.y = y - self.w = w - self.h = h - self.down = down - self.used = used - self.right = right - - def split(self, w, h) -> Rectangle_Obj: - self.used = True - self.down = Rectangle_Obj(x=self.x, y=self.y + h, w=self.w, h=self.h - h) - self.right = Rectangle_Obj(x=self.x + w, y=self.y, w=self.w - w, h=h) - return self - - def find(self, w, h) -> Optional[Rectangle_Obj]: - if self.used: - return self.right.find(w, h) or self.down.find(w, h) - elif (w <= self.w) and (h <= self.h): - return self - return None - -class MaterialImageList: - albedo: Image - normal: Image - emission: Image - ambient_occlusion: Image - height: Image - roughness: Image - fit: Rectangle_Obj - material: Material - - def __init__(self): - pass - - x: int = 0 - y: int = 0 - w: int = 0 - h: int = 0 - - - - - -class BinPacker(object): - root: Rectangle_Obj - bin: list[MaterialImageList] = [] - def __init__(self, structure: list[MaterialImageList]): - self.root = None - self.bin = structure - - def fit(self): - structure = self.bin - structure_len = len(self.bin) - w: int = 0 - h: int = 0 - if structure_len > 0: - w = structure[0].w - h = structure[0].h - self.root = Rectangle_Obj(x=0, y=0, w=w, h=h) - for img in structure: - w = img.w - h = img.h - node = self.root.find(w, h) - if node: - img.fit = node.split(w, h) - else: - img.fit = self.grow_node(w, h) - return structure - - def grow_node(self, w, h) -> Optional[Rectangle_Obj]: - can_grow_right = (h <= self.root.h) - can_grow_down = (w <= self.root.w) - - should_grow_right = can_grow_right and (self.root.h >= (self.root.w + w)) - should_grow_down = can_grow_down and (self.root.w >= (self.root.h + h)) - - if should_grow_right: - return self.grow_right(w, h) - elif should_grow_down: - return self.grow_down(w, h) - elif can_grow_right: - return self.grow_right(w, h) - elif can_grow_down: - return self.grow_down(w, h) - return None - - def grow_right(self, w, h) -> Optional[Rectangle_Obj]: - self.root = Rectangle_Obj( - used=True, - x=0, - y=0, - w=self.root.w + w, - h=self.root.h, - down=self.root, - right=Rectangle_Obj(x=self.root.w, y=0, w=w, h=self.root.h)) - node = self.root.find(w, h) - if node: - return node.split(w, h) - return None - - def grow_down(self, w, h) -> Optional[Rectangle_Obj]: - self.root = Rectangle_Obj( - used=True, - x=0, - y=0, - w=self.root.w, - h=self.root.h + h, - down=Rectangle_Obj(x=0, y=self.root.h, w=self.root.w, h=h), - right=self.root - ) - node = self.root.find(w, h) - if node: - return node.split(w, h) - return None \ No newline at end of file diff --git a/core/properties.py b/core/properties.py index f8fb79d..37da322 100644 --- a/core/properties.py +++ b/core/properties.py @@ -5,6 +5,8 @@ from typing import Tuple from bpy.types import Scene, PropertyGroup, Object, Material, TextureNode, Context, SceneObjects from bpy.props import BoolProperty, EnumProperty, FloatProperty, IntProperty, CollectionProperty, StringProperty, FloatVectorProperty, PointerProperty from bpy.utils import register_class +from ..functions.translations import t, get_languages_list, update_language +from ..core.addon_preferences import get_preference class material_list_bool: diff --git a/functions/atlas_materials.py b/functions/atlas_materials.py deleted file mode 100644 index 60a871d..0000000 --- a/functions/atlas_materials.py +++ /dev/null @@ -1,274 +0,0 @@ -from pathlib import Path - -import numpy -import bpy -import re -import os -from typing import List, Tuple, Optional -from mathutils import Vector -from bpy.types import Material, Operator, Context, Object, Image, Mesh, MeshUVLoopLayer, Float2AttributeValue, ShaderNodeTexImage, ShaderNodeBsdfPrincipled, ShaderNodeNormalMap -from ..core.register import register_wrap -from ..core.properties import material_list_bool, SceneMatClass -from ..core.packer.rectangle_packer import MaterialImageList, BinPacker - - - - - -def scale_images_to_largest(images:list[Image]) -> set: - print([image.name for image in images]) - x: int=0 - y: int=0 - for image in images: - x = max(x,image.size[0]) - y = max(y,image.size[1]) - print(x,y) - - for image in images: - image.scale(width=int(x), height=int(y)) - - return x,y - -def MaterialImageList_to_Image_list(classitem: MaterialImageList) -> list[Image]: - list_of_images: list[Image] = [] - - list_of_images.append(classitem.albedo) - list_of_images.append(classitem.normal) - list_of_images.append(classitem.emission) - list_of_images.append(classitem.ambient_occlusion) - list_of_images.append(classitem.height) - list_of_images.append(classitem.roughness) - - return list_of_images - - -def get_material_images_from_scene(context: Context) -> list[MaterialImageList]: - mat: SceneMatClass = None - material_image_list: list[MaterialImageList] = [] - for mat in context.scene.materials: - new_mat_image_item: MaterialImageList = MaterialImageList() - try: - new_mat_image_item.albedo = bpy.data.images[mat.mat.texture_atlas_albedo] - except Exception as e: - name: str = mat.mat.name+"_albedo_replacement" - if name in bpy.data.images: - bpy.data.images.remove(image=bpy.data.images[name],do_unlink=True) - new_mat_image_item.albedo = bpy.data.images.new(name=name,width=32,height=32, alpha=True) - try: - new_mat_image_item.normal = bpy.data.images[mat.mat.texture_atlas_normal] - except Exception: - name: str = mat.mat.name+"_normal_replacement" - if name in bpy.data.images: - bpy.data.images.remove(image=bpy.data.images[name],do_unlink=True) - new_mat_image_item.normal = bpy.data.images.new(name=name,width=32,height=32, alpha=True) - try: - new_mat_image_item.emission = bpy.data.images[mat.mat.texture_atlas_emission] - except Exception: - name: str = mat.mat.name+"_emission_replacement" - if name in bpy.data.images: - bpy.data.images.remove(image=bpy.data.images[name],do_unlink=True) - new_mat_image_item.emission = bpy.data.images.new(name=name,width=32,height=32, alpha=True) - new_mat_image_item.emission.pixels[:] = numpy.tile(numpy.array([0.0,0.0,0.0,1.0]), 32*32) - - try: - new_mat_image_item.ambient_occlusion = bpy.data.images[mat.mat.texture_atlas_ambient_occlusion] - except Exception: - name: str = mat.mat.name+"_ambient_occlusion_replacement" - if name in bpy.data.images: - bpy.data.images.remove(image=bpy.data.images[name],do_unlink=True) - new_mat_image_item.ambient_occlusion = bpy.data.images.new(name=name,width=32,height=32, alpha=True) - new_mat_image_item.ambient_occlusion.pixels[:] = numpy.tile(numpy.array([1.0,1.0,1.0,1.0]), 32*32) - try: - new_mat_image_item.height = bpy.data.images[mat.mat.texture_atlas_height] - except Exception: - name: str = mat.mat.name+"_height_replacement" - if name in bpy.data.images: - bpy.data.images.remove(image=bpy.data.images[name],do_unlink=True) - new_mat_image_item.height = bpy.data.images.new(name=name,width=32,height=32, alpha=True) - new_mat_image_item.height.pixels[:] = numpy.tile(numpy.array([0.5,0.5,0.5,1.0]), 32*32) - - try: - new_mat_image_item.roughness = bpy.data.images[mat.mat.texture_atlas_roughness] - except Exception: - name: str = mat.mat.name+"_roughness_replacement" - if name in bpy.data.images: - bpy.data.images.remove(image=bpy.data.images[name],do_unlink=True) - new_mat_image_item.roughness = bpy.data.images.new(name=name,width=32,height=32, alpha=True) - new_mat_image_item.roughness.pixels[:] = numpy.tile(numpy.array([1.0,1.0,1.0,0.0]), 32*32) - new_mat_image_item.material = mat.mat - material_image_list.append(new_mat_image_item) - return material_image_list - - -def prep_images_in_scene(context: Context) -> list[MaterialImageList]: - preped_images: list[MaterialImageList] = get_material_images_from_scene(context) - for MaterialImageClass in preped_images: - ImageList: list[Image] = MaterialImageList_to_Image_list(MaterialImageClass) - - MaterialImageClass.w, MaterialImageClass.h = scale_images_to_largest(ImageList) - - - - return preped_images - - -@register_wrap -class Atlas_Materials(Operator): - - bl_idname = "avatar_toolkit.atlas_materials" - bl_label = "Atlas Materials" - bl_description = "Atlas materials to optimize the model" - bl_options = {'REGISTER', 'UNDO'} - - @classmethod - def poll(cls, context: Context) -> bool: - return context.scene.texture_atlas_Has_Mat_List_Shown - - def execute(self, context: Context) -> set: - try: - mat_images: list[MaterialImageList] = prep_images_in_scene(context) - - packer: BinPacker = BinPacker(mat_images) - - mat_images = packer.fit() - - - size: list[int] = [max([matimg.fit.w + matimg.albedo.size[0] for matimg in mat_images]), - max([matimg.fit.h + matimg.albedo.size[1] for matimg in mat_images])] - print([matimg.fit.w + matimg.albedo.size[1] for matimg in mat_images]) - - atlased_mat: MaterialImageList = MaterialImageList() - - for mat in mat_images: - x: int = int(mat.fit.x) - y: int = int(mat.fit.y) - w: int = int(mat.albedo.size[0]) - h: int = int(mat.albedo.size[1]) - - for obj in bpy.data.objects: - mesh: Mesh = obj.data - - - for layer in mesh.polygons: - if obj.material_slots[layer.material_index].material: - if obj.material_slots[layer.material_index].material == mat.material: - for loop_idx in layer.loop_indices: - layer_loops: MeshUVLoopLayer - for layer_loops in mesh.uv_layers: - uv_item: Float2AttributeValue = layer_loops.uv[loop_idx] - uv_item.vector.x = (uv_item.vector.x*(w/size[0]))+(x/size[0]) - uv_item.vector.y = (uv_item.vector.y*(h/size[1]))+(y/size[1]) - - for type in ["albedo","normal", "emission","ambient_occlusion","height", "roughness"]: - new_image_name: str= "Atlas_"+type+"_"+context.scene.name+"_"+Path(bpy.data.filepath).stem - - print("Processing "+type+" atlas image") - - if new_image_name in bpy.data.images: - bpy.data.images.remove(bpy.data.images[new_image_name]) - - canvas: Image = bpy.data.images.new(name=new_image_name, width=int(size[0]),height=int(size[1]), alpha=True) - c_w = canvas.size[0] - #c_h = canvas.size[1] - canvas_pixels: list[float] = list(canvas.pixels[:]) - for mat in mat_images: - x: int = int(mat.fit.x) - y: int = int(mat.fit.y) - w: int = int(mat.albedo.size[0]) - h: int = int(mat.albedo.size[1]) - - image_var: Image = eval("mat."+type) - - image_pixels: list[float] = list(image_var.pixels[:]) - - print("writing image \""+image_var.name+"\" to canvas.") - print("x: \""+str(x)+"\" "+"y: \""+str(y)+"\" "+"w: \""+str(w)+"\" "+"h: \""+str(h)+"\" ") - for k in range(0,h): - for i in range(0, w): - for channel in range(0,4): - canvas_pixels[ - int((((k+y)*c_w) - + - (i+x))*4) - +int(channel) - ] = image_pixels[ - int(( - (k*w) - +i)*4) - +int(channel)] - - canvas.pixels[:] = canvas_pixels[:] - canvas.save(filepath=os.path.join(os.path.dirname(bpy.data.filepath),new_image_name+".png")) - exec("atlased_mat."+type+" = canvas") - - - atlased_mat.material = bpy.data.materials.new(name="Atlas_Final_"+bpy.context.scene.name+"_"+Path(bpy.data.filepath).stem) - atlased_mat.material.use_nodes = True - atlased_mat.material.node_tree.nodes.clear() - - - #I am sorry for the amount of nodes I'm instanciating here and their values. - #This is so that the nodes look pretty in the UI, which I think looks kinda nice. - @989onan - principled_node: ShaderNodeBsdfPrincipled = atlased_mat.material.node_tree.nodes.new(type="ShaderNodeBsdfPrincipled") - principled_node.location.x = 7.29706335067749 - principled_node.location.y = 298.918212890625 - - output_node: ShaderNodeTexImage = atlased_mat.material.node_tree.nodes.new(type="ShaderNodeOutputMaterial") - output_node.location.x = 297.29705810546875 - output_node.location.y = 298.918212890625 - - albedo_node: ShaderNodeTexImage = atlased_mat.material.node_tree.nodes.new(type="ShaderNodeTexImage") - albedo_node.location.x = -588.6177978515625 - albedo_node.location.y = 414.1948547363281 - albedo_node.image = atlased_mat.albedo - - emission_node: ShaderNodeTexImage = atlased_mat.material.node_tree.nodes.new(type="ShaderNodeTexImage") - emission_node.location.x = -588.6177978515625 - emission_node.location.y = -173.9259033203125 - emission_node.image = atlased_mat.emission - - normal_node: ShaderNodeTexImage = atlased_mat.material.node_tree.nodes.new(type="ShaderNodeTexImage") - normal_node.location.x = -941.4189453125 - normal_node.location.y = -20.8391780853271 - normal_node.image = atlased_mat.normal - - normal_map_node: ShaderNodeNormalMap = atlased_mat.material.node_tree.nodes.new(type="ShaderNodeNormalMap") - normal_map_node.location.x = -545.550537109375 - normal_map_node.location.y = -0.7543716430664062 - - roughness_node: ShaderNodeTexImage = atlased_mat.material.node_tree.nodes.new(type="ShaderNodeTexImage") - roughness_node.location.x = -592.1703491210938 - roughness_node.location.y = 206.74075317382812 - roughness_node.image = atlased_mat.roughness - - ambient_occlusion_node: ShaderNodeTexImage = atlased_mat.material.node_tree.nodes.new(type="ShaderNodeTexImage") - ambient_occlusion_node.location.x = -906.4371337890625 - ambient_occlusion_node.location.y = -389.9602355957031 - ambient_occlusion_node.image = atlased_mat.ambient_occlusion - - height_node: ShaderNodeTexImage = atlased_mat.material.node_tree.nodes.new(type="ShaderNodeTexImage") - height_node.location.x = -1222.383056640625 - height_node.location.y = -375.48406982421875 - height_node.image = atlased_mat.height - - atlased_mat.material.node_tree.links.new(principled_node.inputs["Base Color"], albedo_node.outputs["Color"]) - atlased_mat.material.node_tree.links.new(principled_node.inputs["Metallic"], roughness_node.outputs["Alpha"]) - atlased_mat.material.node_tree.links.new(principled_node.inputs["Roughness"], roughness_node.outputs["Color"]) - atlased_mat.material.node_tree.links.new(principled_node.inputs["Alpha"], albedo_node.outputs["Alpha"]) - atlased_mat.material.node_tree.links.new(principled_node.inputs["Normal"], normal_map_node.outputs["Normal"]) - atlased_mat.material.node_tree.links.new(principled_node.inputs["Emission Color"], emission_node.outputs["Color"]) - atlased_mat.material.node_tree.links.new(output_node.inputs["Surface"], principled_node.outputs["BSDF"]) - atlased_mat.material.node_tree.links.new(normal_map_node.inputs["Color"], normal_node.outputs["Color"]) - - - for obj in context.scene.objects: - mesh: Mesh = obj.data - mesh.materials.clear() - - mesh.materials.append(atlased_mat.material) - - return {"FINISHED"} - except Exception as e: - raise e - return {"FINISHED"} - \ No newline at end of file diff --git a/functions/texture_atlas.py b/functions/texture_atlas.py deleted file mode 100644 index 90a4aec..0000000 --- a/functions/texture_atlas.py +++ /dev/null @@ -1,23 +0,0 @@ -import bpy -from typing import List, Optional -from bpy.types import Operator, Context, Object, TextureNode -from ..core.register import register_wrap -from ..core.common import get_armature, simplify_bonename - -@register_wrap -class Atlas_Textures(Operator): - bl_idname = "avatar_toolkit.atlas_textures" - bl_label = "Atlas Textures" - bl_description = """Combines materials and their textures to optimize the model. -Although this combines materials, it may not reduce your VRAM usage. Other tools -like Tuxedo can vastly reduce your VRAM usage as well as many other optimizations, -rather than just duct taping the textures together like material combiner and this tool. -""" - bl_options = {'REGISTER', 'UNDO'} - - def execute(self, context: Context) -> set: - - - return {'FINISHED'} - - diff --git a/functions/translations.py b/functions/translations.py index a265c81..4f84a1d 100644 --- a/functions/translations.py +++ b/functions/translations.py @@ -44,7 +44,7 @@ def load_translations() -> None: print("Default translation file 'en_US.json' not found.") def t(phrase: str, *args, **kwargs) -> str: - output: str = dictionary.get(phrase, None) + output: str = dictionary.get(phrase) if output is None: if verbose: print('Warning: Unknown phrase: ' + phrase) From ce1cc796640a58e39cc519cd80538dad05c1ec6e Mon Sep 17 00:00:00 2001 From: 989onan Date: Wed, 24 Jul 2024 17:53:02 -0400 Subject: [PATCH 14/17] yuck oml fix! --- core/properties.py | 3 ++ functions/translations.py | 3 +- ui/atlas_materials.py | 92 --------------------------------------- 3 files changed, 5 insertions(+), 93 deletions(-) delete mode 100644 ui/atlas_materials.py diff --git a/core/properties.py b/core/properties.py index 7370b75..9841212 100644 --- a/core/properties.py +++ b/core/properties.py @@ -1,4 +1,6 @@ import bpy +from ..functions.translations import t, get_languages_list, update_language +from ..core.addon_preferences import get_preference def register(): default_language = get_preference("language", 0) @@ -12,6 +14,7 @@ def register(): ) bpy.types.Scene.avatar_toolkit_language_changed = bpy.props.BoolProperty(default=False) + def unregister(): if hasattr(bpy.types.Scene, "avatar_toolkit_language"): del bpy.types.Scene.avatar_toolkit_language diff --git a/functions/translations.py b/functions/translations.py index 225920c..9e2a742 100644 --- a/functions/translations.py +++ b/functions/translations.py @@ -63,9 +63,10 @@ def load_translations() -> bool: print(f"Loaded default translations: {dictionary}") # Debug print else: print("Default translation file 'en_US.json' not found.") + return dictionary != old_dictionary -def t(phrase: str, *args, **kwargs) -> str: +def t(phrase: str, default: str = None) -> str: output: str = dictionary.get(phrase) if output is None: if verbose: diff --git a/ui/atlas_materials.py b/ui/atlas_materials.py deleted file mode 100644 index 0596846..0000000 --- a/ui/atlas_materials.py +++ /dev/null @@ -1,92 +0,0 @@ -from bpy.types import UIList, Panel, UILayout, Object, Context,Material, Operator -import bpy -from ..core.register import register_wrap -from .panel import AvatarToolkitPanel -from ..core.properties import SceneMatClass, material_list_bool -from ..functions.atlas_materials import Atlas_Materials - - -@register_wrap -class ExpandSection_Materials(Operator): - bl_idname = 'avatar_toolkit.expand_section_materials' - bl_label = "" - bl_description = "" - - @classmethod - def poll(cls, context: Context) -> bool: - return True - - - def execute(self, context: Context) -> set: - - if not context.scene.texture_atlas_Has_Mat_List_Shown: - context.scene.materials.clear() - newlist: list[Material] = [] - for obj in bpy.context.scene.objects: - if len(obj.material_slots)>0: - for mat_slot in obj.material_slots: - if mat_slot.material: - if mat_slot.material not in newlist: - newlist.append(mat_slot.material) - newitem: SceneMatClass = context.scene.materials.add() - newitem.mat = mat_slot.material - material_list_bool.old_list[context.scene.name] = newlist - else: - context.scene.texture_atlas_Has_Mat_List_Shown = False - return {'FINISHED'} - -@register_wrap -class MaterialTextureAtlasProperties(UIList): - bl_label = "Texture Atlas Material List Material" - bl_idname = "Material_UL_avatar_toolkit_texture_atlas_mat_list_mat" - bl_space_type = 'VIEW_3D' - bl_region_type = 'UI' - - - def draw_item(self , context: Context, layout: UILayout, data: bpy.types.Object, item:SceneMatClass, icon, active_data, active_propname, index): - - if context.scene.texture_atlas_Has_Mat_List_Shown: - box = layout.box() - row = box.row() - row.label(text=item.mat.name, icon = "MATERIAL") - col = box.row() - col.prop(item.mat, "texture_atlas_albedo") - col = box.row() - col.prop(item.mat, "texture_atlas_normal") - col = box.row() - col.prop(item.mat, "texture_atlas_emission") - col = box.row() - col.prop(item.mat, "texture_atlas_ambient_occlusion") - col = box.row() - col.prop(item.mat, "texture_atlas_height") - col = box.row() - col.prop(item.mat, "texture_atlas_roughness") - - - - -@register_wrap -class TextureAtlasPanel(Panel): - bl_label = "Texture Atlasing" - bl_idname = "OBJECT_PT_avatar_toolkit_texture_atlas" - bl_space_type = 'VIEW_3D' - bl_region_type = 'UI' - bl_category = "Avatar Toolkit" - bl_parent_id = "OBJECT_PT_avatar_toolkit" - - def draw(self, context: Context): - layout = self.layout - row = layout.row() - boxoutter = row.box() - direction_icon = 'RIGHTARROW' if not context.scene.texture_atlas_Has_Mat_List_Shown else 'DOWNARROW_HLT' - row = boxoutter.row() - row.operator(ExpandSection_Materials.bl_idname, text=("Reload Texture Atlas Material List" if not context.scene.texture_atlas_Has_Mat_List_Shown else "Loaded Texture Atlas Material List"), icon=direction_icon) - if context.scene.texture_atlas_Has_Mat_List_Shown: - - #get_texture_node_list(bpy.context) - - row = boxoutter.row() - row.template_list(MaterialTextureAtlasProperties.bl_idname, 'material_list', context.scene, 'materials', - context.scene, 'texture_atlas_material_index', rows=12, type='DEFAULT') - row = layout.row() - row.operator(Atlas_Materials.bl_idname, text="Atlas Materials!") \ No newline at end of file From 7401ba78d518719b551ec45e1cc15fb70164996a Mon Sep 17 00:00:00 2001 From: 989onan Date: Wed, 24 Jul 2024 18:20:28 -0400 Subject: [PATCH 15/17] 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 16/17] 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 17/17] 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'