Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

Create A Blender Material With A Python Script

** TL;DR

This Python script makes a basic "Principled BSDF" material in Blender. It takes an array a name for the material and an array of tuples for the settings

python
import bpy


def create_material(name, params):
    if name not in bpy.data.materials:
        print(f"Making {name}")
        bpy.data.materials.new(name)
        material = bpy.data.materials[name]
        material.use_nodes = True
        for param in params:
            material.node_tree.nodes['Principled BSDF'].inputs[param[0]].default_value = param[1]
    else:
        print(f"Already have {name}")
        
if __name__ == "__main__":
    create_material("BlackSquare", [
        ('Base Color', (0, 0, 0, 1)),
        ('Roughness', 1.0),
    ])
    create_material("WhiteSquare", [
        ('Base Color', (1, 1, 1, 1)),
        ('Roughness', 0.7),
    ])