5
$\begingroup$

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?

$\endgroup$
4
$\begingroup$

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') 
$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – CoryG Mar 16 '15 at 15:55
  • 2
    $\begingroup$ Thanks - it finally finished after about 40 minutes, but well worth not having to click the - 20,000 times. $\endgroup$ – CoryG Mar 16 '15 at 17:10
  • $\begingroup$ how can I change this script to removed unused texture instead? $\endgroup$ – RollRoll Oct 25 '15 at 3:22
6
$\begingroup$

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".

enter image description here

$\endgroup$
4
$\begingroup$

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
$\endgroup$
2
$\begingroup$

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()
$\endgroup$
  • $\begingroup$ Blender official 2.74, tested 2 times your script with 14000 materials and Blender crashed both times. $\endgroup$ – JuhaW May 14 '15 at 14:02
  • $\begingroup$ 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. $\endgroup$ – Mutant Bob May 14 '15 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.