Adding materials to mesh faces

I am wanting to add a material to a single face of a mesh. For simplicity’s sake, lets make the mesh a cube. How can I access the mesh’s data in a way to be able to add a material to only a single face of the mesh so I can in a sense just color one side?

Any help would be appreciated.

Thanks!

you need to add the material to the mesh and add the material index for the material on this mesh to the face…



import bpy

#get the scene and active object
sce = bpy.data.scenes.active
ob_act = sce.objects.active


#get the mesh
me = ob_act.getData(mesh=1)

#get the materials from the mesh
materials = me.materials

#create a ne material called dave
mat = Material.New('dave')

#add it to the list of materials
materials.append(mat)


add it to the mesh
me.materials =materials


#get the index
index = materials.index(mat)


#apply to selected faces
for f in me.faces:
    if f.sel:
        f.mat = index


#update the mesh
me.update()