I have tried the following but the mat is not assigned to any of the faces.
import bpy
import bmesh
# Creating a new Mat
mat = bpy.data.materials.new(name="newMat")
mat.diffuse_color = (0.5,0,0.5,1)
# Generating a Box
bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=True, location=(0, 0, 0))
# Resizing and Translating the box
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.transform.resize(value=(0.5, 1, 1))
bpy.ops.transform.translate(value=(0.5, 0, 0))
bpy.ops.mesh.select_all(action='DESELECT')
# Instantiating the bmesh of the Box
obj = bpy.context.edit_object
me = obj.data
bm = bmesh.from_edit_mesh(me)
# Selecting just one of the faces
bm.faces[0].select = True
# toggle to edit mode
bpy.ops.object.mode_set(mode='EDIT')
# make sure face select mode is enabled
bpy.context.tool_settings.mesh_select_mode = [False, False, True]
# use second material slot
ob = bpy.context.active_object
ob.active_material_index = 1
# assign the material
bpy.ops.object.material_slot_assign()
# toggle to object mode
bpy.ops.object.mode_set(mode='OBJECT')
What am I doing wrong, or how else could I achieve this?