removing all materials - for UV mapped models


I have a large model in OBJ format and have imported it into Blender. It’s all UV mapped and so I just need one material which will be applied to every face (using TexFace) so it can be rendered (don’t need any fancy settings!). Upon importing the model, a new material is created for each mesh. Alas, I have over 700 meshes which is slightly over the 16 material limit in Blender! My only options I could think of were unlinking the materials from each mesh by hand, and then relinking a new material to each of them, or writing a script. Looking at the API I’ve managed to write the following:

Name: 'Delete Materials'
Blender: 242
Group: 'Add'
Tooltip: 'Get rid of materials, and replace with just one...'

import Blender
from Blender import NMesh, Material, Window

#print '-------------------'

editmode= Window.EditMode()
if editmode: Window.EditMode(0)

mesh_names = NMesh.GetNames()

# Set up a materials list - will be used for every mesh
materials_list = NMesh.GetRaw(mesh_names[0]).getMaterials() # Set up the list... Hmmm...
default_material = Material.New()
materials_list[0] = default_material
for i in range(1,len(materials_list)-1):
    materials_list[i] = None

for mesh_name in mesh_names:
    mesh = NMesh.GetRaw(mesh_name)
    #print len(mesh.materials)

    # Change all mesh's materials to None except the 1st one?
    # Material usage mask: use mesh material 0
    #mesh.colbits = (1<<0)

    # Change all faces to use mesh's 1st material
    for face in mesh.faces:
        face.mat = 0



any comments would be most appreciated as I’ve rather hacked this together… In particular, is there a better way to set up a materials list other than

materials_list = NMesh.GetRaw(mesh_names[0]).getMaterials()


select 1 object, remove all its materials.
then select all,
do Ctrl+L, Materials

I had a funny feeling there’d be an easier way! Still, I got to learn some Python :wink: