Atom's 2.5 Material Function Library

Hi All,

As I work my way through the BlendText conversion, I have come up with a few test suites to iron out bugs.

Here is my recent work on converting the materials functions I will need for the BlendText engine:


import bpy

def fetchIfObject (passedName= ""):
    try:
        result = bpy.data.objects[passedName]
    except:
        result = None
    return result

def returnTexturesNameList(passedMaterialName):
    result = []
    tempMat = returnMaterialByName(passedMaterialName)
    l = len(tempMat.texture_slots)
    if l > 0:
        for ts in tempMat.texture_slots:
            if ts != None:
                result.append(ts.name)
    return result

def returnTextureByName(passedName):
    result = None
    for t in bpy.data.textures:
        if t.name == passedName:
            result = t
            break
    return result
    
def returnMaterialsNameList():
    result = []
    for m in bpy.data.materials:
       result.append(m.name)
    return result
    
def returnMaterialByName(passedName):
    result = None
    for m in bpy.data.materials:
        if m.name == passedName:
            result = m
            break
    return result

def createNewMaterial (passedName):
    tempMat = bpy.data.materials.new(passedName)
    if tempMat != None:
        tempMat.diffuse_color = (0.1,0.2,0.3)
        tempMat.diffuse_shader = 'LAMBERT'
        tempMat.diffuse_intensity = 1.0
        tempMat.specular_color = (0.9,0.9,0.9)
        tempMat.specular_shader = 'COOKTORR'
        tempMat.specular_intensity = 0.5
        tempMat.alpha = 1.0
        tempMat.ambient = 0.3
        tempMat.emit = 0.2
    return tempMat

def aquireOrCreateMaterial(passedName):
    tempMat = returnMaterialByName(passedName)
    if tempMat == None:
        tempMat = createNewMaterial(passedName)
    return tempMat


def assignMaterial(passedObjectName, passedMaterial, passedLink = "DATA"):
    result = False
    tempObject = fetchIfObject(passedObjectName)
    if tempObject != None:
        if tempObject.material_slots.__len__() > 0:        
            tempObject.material_slots[0].material = passedMaterial
            tempObject.material_slots[0].link = passedLink 
            print("OBJECT Slot #0 " + passedMaterial.name)
        else:
            bpy.ops.object.material_slot_add()
            tempObject.material_slots[tempObject.material_slots.__len__() - 1].material = passedMaterial
            tempObject.material_slots[tempObject.material_slots.__len__() - 1].link = passedLink
            print("OBJECT Added to end.") 
        result = True       
    else:
        print("No object called [" + passedObjectName + "].")
    return result

# Program starts here.
lstMaterialNames = returnMaterialsNameList()
print(lstMaterialNames)
name = "Material"        #Use a material name already in the scene.
n = returnMaterialByName(name)
print (n.diffuse_color)
lstTextureNames = returnTexturesNameList(name)
print(lstTextureNames)
tempName = lstTextureNames[0]
print(tempName)
tempTexture = returnTextureByName(tempName)
print(tempTexture.use_preview_alpha)

myMaterial = aquireOrCreateMaterial("myMAT")    #Fetch existing material or create new one if it does not exist.
#r = assignMaterial("Cube",myMaterial,"OBJECT")  #Override default link type of "DATA".
r = assignMaterial("Cube",myMaterial)           #Defaults to "DATA" if 3rd parameter is ommited.

The assign material to object function makes the assumption that if you already have a list of materials and you are assigning a material, the new assignment goes to slot #0. This could be expanded upon, of course, but for BlendText, this will always be the case because the script is managing the materials.