Skip to content

Commit

Permalink
feat(material tools): rename operator and enhance functionality to re…
Browse files Browse the repository at this point in the history
…move unused material slots
  • Loading branch information
NMC-TBone committed Dec 9, 2024
1 parent 6665591 commit c051bc8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
31 changes: 18 additions & 13 deletions i3d_exporter_additionals/tools/material_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,27 @@ def execute(self, context: bpy.types.Context):
return {'FINISHED'}


class I3DEA_OT_remove_duplicate_material(bpy.types.Operator):
bl_idname = "i3dea.remove_duplicate_material"
bl_label = "Remove Duplicate Materials"
bl_description = "Removes all duplicated/not assigned materials"
class I3DEA_OT_remove_unused_material_slots(bpy.types.Operator):
bl_idname = "i3dea.remove_unused_material_slots"
bl_label = "Remove Unused Material Slots"
bl_description = "Removes duplicate materials and unused material slots"
bl_options = {'REGISTER', 'UNDO'}

def execute(self, context):
bpy.ops.object.mode_set(mode='OBJECT')
for ob in bpy.context.scene.objects:
if not ob.material_slots:
for obj in context.scene.objects:
if obj.type != 'MESH' and not obj.material_slots:
continue
bpy.ops.object.material_slot_remove_unused()
for mat in bpy.data.materials:
if not mat.users:
bpy.data.materials.remove(mat)
bpy.ops.outliner.orphans_purge()

mesh: bpy.types.Mesh = obj.data

used_material_indices = set(poly.material_index for poly in mesh.polygons)
used_materials = [mesh.materials[i] for i in used_material_indices if mesh.materials[i] is not None]

mesh.materials.clear()

for mat in used_materials:
mesh.materials.append(mat)

self.report({'INFO'}, "Unused material slots & Orphan Data removed")
return {'FINISHED'}

Expand Down Expand Up @@ -257,7 +262,7 @@ def execute(self, context):

classes = (
I3DEA_OT_mirror_material,
I3DEA_OT_remove_duplicate_material,
I3DEA_OT_remove_unused_material_slots,
I3DEA_OT_setup_material,
I3DEA_OT_i3dio_material,
)
Expand Down
2 changes: 1 addition & 1 deletion i3d_exporter_additionals/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def draw(self, context):
box_col.label(text="Material operators")
box_row = box_col.row(align=True)
box_row.operator("i3dea.mirror_material", text="Add Mirror Material")
box_row.operator("i3dea.remove_duplicate_material", text="Remove Duplicate Materials")
box_row.operator("i3dea.remove_unused_material_slots")

box = layout.box()
box_col = box.column(align=True)
Expand Down

0 comments on commit c051bc8

Please sign in to comment.