Merge pull request #21 from Yusarina/main

Typing updates to Join meshes, combine materials and common.
This commit is contained in:
Onan Chew
2024-06-18 19:16:59 -04:00
committed by GitHub
3 changed files with 41 additions and 35 deletions
+3 -3
View File
@@ -1,11 +1,11 @@
import bpy import bpy
import numpy as np import numpy as np
from bpy.types import Object, ShapeKey from bpy.types import Object, ShapeKey, Mesh, Context
from functools import lru_cache from functools import lru_cache
### Clean up material names in the given mesh by removing the '.001' suffix. ### Clean up material names in the given mesh by removing the '.001' suffix.
def clean_material_names(mesh): def clean_material_names(mesh: Mesh) -> None:
for j, mat in enumerate(mesh.material_slots): for j, mat in enumerate(mesh.material_slots):
if mat.name.endswith(('.0+', ' 0+')): if mat.name.endswith(('.0+', ' 0+')):
mesh.active_material_index = j mesh.active_material_index = j
@@ -15,7 +15,7 @@ def clean_material_names(mesh):
# This will fix faulty uv coordinates, cats did this a other way which can have unintended consequences, # This will fix faulty uv coordinates, cats did this a other way which can have unintended consequences,
# this is the best way i could of think of doing this for the time being, however may need improvements. # this is the best way i could of think of doing this for the time being, however may need improvements.
def fix_uv_coordinates(context): def fix_uv_coordinates(context: Context) -> None:
obj = context.object obj = context.object
# Check if the object is in Edit Mode # Check if the object is in Edit Mode
+25 -22
View File
@@ -1,16 +1,18 @@
import bpy import bpy
import re 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
from ..core.register import register_wrap 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 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.color_space = node1.color_space
node2.coordinates = node1.coordinates 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: for node1 in mat1.node_tree.nodes:
if node1.type == 'TEX_IMAGE': if node1.type == 'TEX_IMAGE':
node2 = mat2.node_tree.nodes.get(node1.name) node2 = mat2.node_tree.nodes.get(node1.name)
@@ -18,7 +20,7 @@ def copy_tex_nodes(mat1, mat2):
node2.mapping = node1.mapping node2.mapping = node1.mapping
node2.projection = node1.projection 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: if mat1.node_tree and mat2.node_tree:
for node1 in mat1.node_tree.nodes: for node1 in mat1.node_tree.nodes:
if node1.type == 'TEX_IMAGE': if node1.type == 'TEX_IMAGE':
@@ -32,10 +34,10 @@ def consolidate_textures(mat1, mat2):
node2.image = node1.image node2.image = node1.image
copy_tex_nodes(mat1, mat2) 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 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): if not color_match(mat1.diffuse_color, mat2.diffuse_color, tolerance):
return False return False
@@ -46,32 +48,32 @@ def materials_match(mat1, mat2, tolerance=0.01):
return True return True
def get_base_name(name): def get_base_name(name: str) -> str:
mat_match = re.match(r"^(.*)\.\d{3}$", name) mat_match = re.match(r"^(.*)\.\d{3}$", name)
return mat_match.group(1) if mat_match else 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") self.report({'INFO'}, f"Combined {num_combined} materials")
@register_wrap @register_wrap
class CombineMaterials(bpy.types.Operator): class CombineMaterials(Operator):
bl_idname = "avatar_toolkit.combine_materials" bl_idname = "avatar_toolkit.combine_materials"
bl_label = "Combine Materials" bl_label = "Combine Materials"
bl_description = "Combine similar materials to optimize the model" bl_description = "Combine similar materials to optimize the model"
bl_options = {'REGISTER', 'UNDO'} bl_options = {'REGISTER', 'UNDO'}
@classmethod @classmethod
def poll(cls, context): def poll(cls, context: Context) -> bool:
return context.active_object is not None return context.active_object is not None
def execute(self, context): def execute(self, context: Context) -> set:
bpy.ops.object.mode_set(mode='OBJECT') 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: if not armature:
return {'CANCELLED'} 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: if not meshes:
return {'CANCELLED'} return {'CANCELLED'}
@@ -85,17 +87,17 @@ class CombineMaterials(bpy.types.Operator):
return {'FINISHED'} return {'FINISHED'}
def consolidate_materials(self, objects): def consolidate_materials(self, objects: List[Object]) -> None:
mat_mapping = {} mat_mapping: dict = {}
num_combined = 0 num_combined: int = 0
for ob in objects: for ob in objects:
for slot in ob.material_slots: for slot in ob.material_slots:
mat = slot.material mat: Optional[Material] = slot.material
if mat: if mat:
base_name = get_base_name(mat.name) base_name: str = get_base_name(mat.name)
if base_name in mat_mapping: 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): if materials_match(base_mat, mat):
consolidate_textures(base_mat, mat) consolidate_textures(base_mat, mat)
num_combined += 1 num_combined += 1
@@ -105,12 +107,12 @@ class CombineMaterials(bpy.types.Operator):
report_consolidated(self, num_combined) report_consolidated(self, num_combined)
def remove_unused_materials(self): def remove_unused_materials(self) -> None:
for mat in bpy.data.materials: 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): 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) bpy.data.materials.remove(mat, do_unlink=True)
def cleanmatslots(self): def cleanmatslots(self) -> None:
for obj in bpy.data.objects: for obj in bpy.data.objects:
if obj.type == 'MESH': if obj.type == 'MESH':
obj.select_set(True) obj.select_set(True)
@@ -118,7 +120,8 @@ class CombineMaterials(bpy.types.Operator):
bpy.ops.object.material_slot_remove_unused() bpy.ops.object.material_slot_remove_unused()
obj.select_set(False) obj.select_set(False)
def clean_material_names(self): def clean_material_names(self) -> None:
for obj in bpy.data.objects: for obj in bpy.data.objects:
if obj.type == 'MESH': if obj.type == 'MESH':
clean_material_names(obj) clean_material_names(obj)
+13 -10
View File
@@ -1,23 +1,25 @@
import bpy import bpy
from typing import List, Optional
from bpy.types import Operator, Context, Object
from ..core.register import register_wrap from ..core.register import register_wrap
from ..core.common import fix_uv_coordinates from ..core.common import fix_uv_coordinates
@register_wrap @register_wrap
class JoinAllMeshes(bpy.types.Operator): class JoinAllMeshes(Operator):
bl_idname = "avatar_toolkit.join_all_meshes" bl_idname = "avatar_toolkit.join_all_meshes"
bl_label = "Join All Meshes" bl_label = "Join All Meshes"
bl_description = "Join all meshes in the scene" bl_description = "Join all meshes in the scene"
bl_options = {'REGISTER', 'UNDO'} bl_options = {'REGISTER', 'UNDO'}
@classmethod @classmethod
def poll(cls, context): def poll(cls, context: Context) -> bool:
return context.mode == 'OBJECT' return context.mode == 'OBJECT'
def execute(self, context): def execute(self, context: Context) -> set:
self.join_all_meshes(context) self.join_all_meshes(context)
return {'FINISHED'} return {'FINISHED'}
def join_all_meshes(self, context): def join_all_meshes(self, context: Context) -> None:
if not bpy.data.objects: if not bpy.data.objects:
self.report({'INFO'}, "No objects in the scene") self.report({'INFO'}, "No objects in the scene")
return return
@@ -25,7 +27,7 @@ class JoinAllMeshes(bpy.types.Operator):
bpy.ops.object.mode_set(mode='OBJECT') bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT') bpy.ops.object.select_all(action='DESELECT')
meshes = [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']
for mesh in meshes: for mesh in meshes:
mesh.select_set(True) mesh.select_set(True)
@@ -41,22 +43,22 @@ class JoinAllMeshes(bpy.types.Operator):
self.report({'WARNING'}, "No mesh objects selected") self.report({'WARNING'}, "No mesh objects selected")
@register_wrap @register_wrap
class JoinSelectedMeshes(bpy.types.Operator): class JoinSelectedMeshes(Operator):
bl_idname = "avatar_toolkit.join_selected_meshes" bl_idname = "avatar_toolkit.join_selected_meshes"
bl_label = "Join Selected Meshes" bl_label = "Join Selected Meshes"
bl_description = "Join selected meshes" bl_description = "Join selected meshes"
bl_options = {'REGISTER', 'UNDO'} bl_options = {'REGISTER', 'UNDO'}
@classmethod @classmethod
def poll(cls, context): def poll(cls, context: Context) -> bool:
return context.mode == 'OBJECT' return context.mode == 'OBJECT'
def execute(self, context): def execute(self, context: Context) -> set:
self.join_selected_meshes(context) self.join_selected_meshes(context)
return {'FINISHED'} return {'FINISHED'}
def join_selected_meshes(self, context): def join_selected_meshes(self, context: Context) -> None:
selected_objects = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH'] selected_objects: List[Object] = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH']
if not selected_objects: if not selected_objects:
self.report({'WARNING'}, "No mesh objects selected") self.report({'WARNING'}, "No mesh objects selected")
@@ -78,3 +80,4 @@ class JoinSelectedMeshes(bpy.types.Operator):
self.report({'INFO'}, "Selected meshes joined successfully") self.report({'INFO'}, "Selected meshes joined successfully")
else: else:
self.report({'WARNING'}, "No mesh objects selected") self.report({'WARNING'}, "No mesh objects selected")