Hi,
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:
#!BPY
"""
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?
mesh.setMaterials(materials_list)
# 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
mesh.update()
Window.EditMode(editmode)
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()
Cheers