if you want to use python, you can use this script:
Explanation:
newMat = bpy.data.materials.new(name = 'Material')
This just creates a new material with name "Material". Is is not used by an object right now.
object.data.materials.append(newMat)
This appends the materials to the object's data.
newMat.use_nodes = True
This tells the material to use nodes (instead of being a "pure" color).
nodes = newMat.node_tree.nodes
With this the variable nodes gets access to the nodeTree/nodes (this is basically what you see in the shader editor).
material_output = nodes.get("Material Output")
Here i get the material output node.
node_emission = nodes.new(type='ShaderNodeBsdfDiffuse')
Creates a new Diffuse node
node_emission.inputs[0].default_value = (random.uniform(0, 1),random.uniform(0, 1),random.uniform(0, 1),1)
Sets the color to the diffuse node. The inputs array represents all incoming values. Default value is the value/color in this case.
links = newMat.node_tree.links
Gets access to the links
newLink = links.new(node_emission.outputs[0], material_output.inputs[0])
Creates the link between your diffuse and material output
import bpy
import random
def createRandomMat(object):
newMat = bpy.data.materials.new(name = 'Material')
object.data.materials.append(newMat)
newMat.use_nodes = True
nodes = newMat.node_tree.nodes
material_output = nodes.get("Material Output")
node_emission = nodes.new(type='ShaderNodeBsdfDiffuse')
node_emission.inputs[0].default_value = (random.uniform(0, 1),random.uniform(0, 1),random.uniform(0, 1),1)
links = newMat.node_tree.links
newLink = links.new(node_emission.outputs[0], material_output.inputs[0])
for x in range(10):
for y in range(10):
location = (x*4, y*4, 0)
bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=False, align='WORLD', location = location, scale = (1, 1, 1))
object = bpy.context.active_object
createRandomMat(object)
result:
material created: