# Quick way to remove thousands of materials from an object?

I've put in about 3 days of work manually editing a mesh (with lots of copying and pasting) to get around the issues of junk vertices with boolean operations in Blender and noticed something troubling. I started with a single material but after all the edits that single material has multiplied into approximately 20,000 duplicates of the same material, all assigned to separate parts of the same mesh. I've been clicking away at the - button for awhile and am only up to ~700 deleted materials - is there a way to clear all materials from an object then simply reapply the one I want?

This will remove the materials(except the first) from the object and assign the first material to all the mesh

• select the object
• run this script :

import bpy

C = bpy.context

for i in range(1,len(C.object.material_slots)):
C.object.active_material_index = 1
bpy.ops.object.material_slot_remove()

bpy.ops.object.mode_set(mode = 'EDIT')
bpy.ops.mesh.select_all(action = 'SELECT')
bpy.ops.object.material_slot_assign()
bpy.ops.object.mode_set(mode = 'OBJECT')

• It's been running for a good 20 minutes now, how long should I expect it to run for approx 20k materials on a mesh of ~110,000 vertices and ~60,000 faces? – CoryG Mar 16 '15 at 15:55
• Thanks - it finally finished after about 40 minutes, but well worth not having to click the - 20,000 times. – CoryG Mar 16 '15 at 17:10
• how can I change this script to removed unused texture instead? – RollRoll Oct 25 '15 at 3:22

This method takes time under one second if you have 20k materials object. Select first your multimaterial object then object which has one material. Click in material panel black arrow > "Copy material to Others".

Quick? maybe use clear

import bpy

obj = bpy.context.active_object

materials = obj.data.materials
materials.clear(1)   # 0 will crash, 1 will update as it goes along


bpy.ops is full of cooties. This seems a lot simpler and faster:

import bpy

obj = bpy.context.active_object
mesh = obj.data

while len(mesh.materials)>1:
mesh.materials.pop()

• Blender official 2.74, tested 2 times your script with 14000 materials and Blender crashed both times. – JuhaW May 14 '15 at 14:02
• That is disappointing. It works when I test it on my computer. If you happen to be able to compile and debug blender from source then there is a chance the crash bug could be fixed. Otherwise I am not optimistic. – Mutant Bob May 14 '15 at 17:50