From 31f76931b964acd648ef649eff2264709c013048 Mon Sep 17 00:00:00 2001 From: Yusarina Date: Wed, 19 Jun 2024 00:01:26 +0100 Subject: [PATCH] Tpying 2 --- core/common.py | 2 +- functions/combine_materials.py | 47 ++++++++++++++++++---------------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/core/common.py b/core/common.py index 5b98ec5..b052b2c 100644 --- a/core/common.py +++ b/core/common.py @@ -1,7 +1,7 @@ import bpy import numpy as np -from bpy.types import Object, ShapeKey +from bpy.types import Object, ShapeKey, Mesh, Context from functools import lru_cache ### Clean up material names in the given mesh by removing the '.001' suffix. diff --git a/functions/combine_materials.py b/functions/combine_materials.py index 1741fc8..4eaa93d 100644 --- a/functions/combine_materials.py +++ b/functions/combine_materials.py @@ -1,16 +1,18 @@ 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.register import register_wrap -def textures_match(tex1, tex2): +def textures_match(tex1: bpy.types.ImageTexture, tex2: bpy.types.ImageTexture) -> bool: return tex1.image == tex2.image and tex1.extension == tex2.extension -def consolidate_nodes(node1, node2): +def consolidate_nodes(node1: bpy.types.ShaderNodeTexImage, node2: bpy.types.ShaderNodeTexImage) -> None: node2.color_space = node1.color_space node2.coordinates = node1.coordinates -def copy_tex_nodes(mat1, mat2): +def copy_tex_nodes(mat1: Material, mat2: Material) -> None: for node1 in mat1.node_tree.nodes: if node1.type == 'TEX_IMAGE': node2 = mat2.node_tree.nodes.get(node1.name) @@ -18,7 +20,7 @@ def copy_tex_nodes(mat1, mat2): node2.mapping = node1.mapping node2.projection = node1.projection -def consolidate_textures(mat1, mat2): +def consolidate_textures(mat1: Material, mat2: Material) -> None: if mat1.node_tree and mat2.node_tree: for node1 in mat1.node_tree.nodes: if node1.type == 'TEX_IMAGE': @@ -32,10 +34,10 @@ def consolidate_textures(mat1, mat2): node2.image = node1.image copy_tex_nodes(mat1, mat2) -def color_match(col1, col2, tolerance=0.01): +def color_match(col1: Tuple[float, float, float, float], col2: Tuple[float, float, float, float], tolerance: float = 0.01) -> bool: return abs(col1[0] - col2[0]) < tolerance -def materials_match(mat1, mat2, tolerance=0.01): +def materials_match(mat1: Material, mat2: Material, tolerance: float = 0.01) -> bool: if not color_match(mat1.diffuse_color, mat2.diffuse_color, tolerance): return False @@ -46,32 +48,32 @@ def materials_match(mat1, mat2, tolerance=0.01): return True -def get_base_name(name): +def get_base_name(name: str) -> str: mat_match = re.match(r"^(.*)\.\d{3}$", name) return mat_match.group(1) if mat_match else name -def report_consolidated(self, num_combined): +def report_consolidated(self: Operator, num_combined: int) -> None: self.report({'INFO'}, f"Combined {num_combined} materials") @register_wrap -class CombineMaterials(bpy.types.Operator): +class CombineMaterials(Operator): bl_idname = "avatar_toolkit.combine_materials" bl_label = "Combine Materials" bl_description = "Combine similar materials to optimize the model" bl_options = {'REGISTER', 'UNDO'} @classmethod - def poll(cls, context): + def poll(cls, context: Context) -> bool: return context.active_object is not None - def execute(self, context): + def execute(self, context: Context) -> set: bpy.ops.object.mode_set(mode='OBJECT') - armature = next((obj for obj in bpy.data.objects if obj.type == 'ARMATURE'), None) + armature: Optional[Object] = next((obj for obj in bpy.data.objects if obj.type == 'ARMATURE'), None) if not armature: return {'CANCELLED'} - meshes = [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] = [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: return {'CANCELLED'} @@ -85,17 +87,17 @@ class CombineMaterials(bpy.types.Operator): return {'FINISHED'} - def consolidate_materials(self, objects): - mat_mapping = {} - num_combined = 0 + def consolidate_materials(self, objects: List[Object]) -> None: + mat_mapping: dict = {} + num_combined: int = 0 for ob in objects: for slot in ob.material_slots: - mat = slot.material + mat: Optional[Material] = slot.material if mat: - base_name = get_base_name(mat.name) + base_name: str = get_base_name(mat.name) if base_name in mat_mapping: - base_mat = mat_mapping[base_name] + base_mat: Material = mat_mapping[base_name] if materials_match(base_mat, mat): consolidate_textures(base_mat, mat) num_combined += 1 @@ -105,12 +107,12 @@ class CombineMaterials(bpy.types.Operator): report_consolidated(self, num_combined) - def remove_unused_materials(self): + 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): bpy.data.materials.remove(mat, do_unlink=True) - def cleanmatslots(self): + def cleanmatslots(self) -> None: for obj in bpy.data.objects: if obj.type == 'MESH': obj.select_set(True) @@ -118,7 +120,8 @@ class CombineMaterials(bpy.types.Operator): bpy.ops.object.material_slot_remove_unused() obj.select_set(False) - def clean_material_names(self): + def clean_material_names(self) -> None: for obj in bpy.data.objects: if obj.type == 'MESH': clean_material_names(obj) +