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.
This commit is contained in:
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user