Check if a mesh has a material in Python
If the mesh has no materials then

1&gt;len(obj.data.materials)


although it is also possible that it has a material slot with nothing in it

obj.data.materials[i] is None


and the complete script is

import bpy

def no_material_on(mesh):
    if 1&gt;len(mesh.materials):
        return True

    for i in range(len(mesh.materials)):
        if mesh.materials[i] is not None:
            return False
    return True

def my_grey():
    name = "my grey"
    rval = bpy.data.materials.get(name)
    if rval is None:
        rval = bpy.data.materials.new(name)
        g=.735
        rval.diffuse_color = (g,g,g)
    return rval


def mission1(obj):
    mesh = obj.data
    if no_material_on(mesh):
        mat = my_grey()
        if 1&gt;len(mesh.materials):
            mesh.materials.append(mat)
        else:
            mesh.materials[0] = mat

#

mission1(bpy.context.active_object)