Assign a material to an object in Blender using Python

14 January 2021

This is how to create a new material, add a shader, create a new object and assign the material to the object in Blender using Python.

First, create a new material. The function takes a string as the name for the new material.

import bpy

def newMaterial(id):

    mat =

    if mat is None:
        mat =

    mat.use_nodes = True

    if mat.node_tree:

    return mat

Then add a shader to the material. Input the type of shader (i.e. diffuse, emission, glossy) and the rgb colour.

def newShader(id, type, r, g, b):

    mat = newMaterial(id)

    nodes = mat.node_tree.nodes
    links = mat.node_tree.links
    output ='ShaderNodeOutputMaterial')

    if type == "diffuse":
        shader ='ShaderNodeBsdfDiffuse')
        nodes["Diffuse BSDF"].inputs[0].default_value = (r, g, b, 1)

    elif type == "emission":
        shader ='ShaderNodeEmission')
        nodes["Emission"].inputs[0].default_value = (r, g, b, 1)
        nodes["Emission"].inputs[1].default_value = 1

    elif type == "glossy":
        shader ='ShaderNodeBsdfGlossy')
        nodes["Glossy BSDF"].inputs[0].default_value = (r, g, b, 1)
        nodes["Glossy BSDF"].inputs[1].default_value = 0[0], output.inputs[0])

    return mat

Then create the object, assign the material and call the function.

def drawObject():

    mat = newShader("Shader1", "diffuse", 1, 1, 1)
    bpy.ops.mesh.primitive_cube_add(size=2, align='WORLD', location=(0, 0, 0))


I like working with awesome people on awesome projects.
Let me know what you’re working on. Drop me a line at