How to assign a material to a face ?

Hello, I am working on a script to simplify some material job I often have.
RIght now my script browse my model for each face and what I don’t find is how to assigne an existing material to the faces.
It looks like that:

sce =
ob =
mesh = ob.getData(mesh=1)

for face in mesh.faces:

I thried that to assigne the first material index:


But it seems to does nothing.
Can someone help ?


Here is some 2.49 code I wrote as an example a while ago.

import Blender
from Blender import *

def makeCube(x,y,name,passedMesh,passedScene):
    ob = Object.New("Mesh",name)

    return ob
#Create cubes with materials.
localScene = Scene.GetCurrent()

matRed = Blender.Material.New('matRed')
matRed.rgbCol = 1,0,0

matBlue = Blender.Material.New('matBlue')
matBlue.rgbCol = 0,0,1

redMesh = Mesh.Primitives.Cube(1)
redCube = makeCube(0,0,"redCube",redMesh,localScene)
redMesh.materials = [matRed]

blueMesh = Mesh.Primitives.Cube(1)
blueCube = makeCube(2,0,"blueCube",blueMesh,localScene)
blueMesh.materials = [matBlue]

mixedMesh = Mesh.Primitives.Cube(1)
mixedCube = makeCube(4,0,"mixedCube",mixedMesh,localScene)

#Lets add to materials to this mesh.
mixedMesh.materials = [matRed, matBlue]

#Manually assign faces a material index. (We know the cube has 6 faces i.e. 0-5).
mixedMesh.faces[0].mat = 0    #Face #0 gets index #0 which is Red.
mixedMesh.faces[1].mat = 1    #Face #1 gets index #1 which is Blue.
mixedMesh.faces[2].mat = 0
mixedMesh.faces[3].mat = 1
mixedMesh.faces[4].mat = 0
mixedMesh.faces[5].mat = 1


yes it seems to work, thanks. Is it possible to get the index in a mesh of a given material ?

EDIT: I tried that:

for m in mesh.materials:
    if == 'mat_'
print i

but I get this error:

Attribute error: “NoneType” object has no attribute ‘name’

I found my mistake… thanks

I’ve written a whole bunch of material scripts… (one that assigns materials directly to faces for example

Here’s mine:

(call from the header scripts menu in object or edit mode (in object mode you can assign a material to all selected objects, in edit mode, to selected faces, but from any material in teh scene…

you may also be interested in my "texface utilities"scripts too… see this thread for an explanation

THanks for your answer it seems than your had the exact same problem as me using only the uv image editor, but too late, my script works now and does the same as yours (just texface to material), but I have a problem with mine, I can’t assign more than 16 materials, do you have this problem too ?

That’s a limitation with 2.4x… only 16 I’m afraid

this limit has been made much much larger in 2.5 can’t remeber what exactly, either 3000 ish or 32000 ish (plus some obligatory numbers that make the actual number strange but efficient from a memory point of view…

you may want to check out the “clean unused” script from my assign materials script… it will remove materials from your objects that have no faces assigned.