Hi,
some time ago I was writing a small function to create an image texture material.
This is the function:
def _create_new_image_material(name, fname, alpha=1.):
"""Create a new material.
Args:
name: name of material
fname: path to image to load
Returns:
The new material.
"""
nodescale = 300
# load image texture
bpy.data.images.load(fname, check_existing=False)
mat = bpy.data.materials.new(name)
mat.use_nodes = True
nodes = mat.node_tree.nodes
nodes.clear()
texture = nodes.new(type="ShaderNodeTexCoord")
texture.location = 0, 0
img = nodes.new(type="ShaderNodeTexImage")
img.location = nodescale, 0
img.image = bpy.data.images[fname.split('/')[-1]]
bsdf = nodes.new(type="ShaderNodeBsdfPrincipled")
bsdf.location = 2*nodescale, 0
#bsdf.inputs[0].default_value = color
bsdf.inputs['Alpha'].default_value = alpha
bsdf.inputs['Specular'].default_value = 0.
node_output = nodes.new(type="ShaderNodeOutputMaterial")
node_output.location = 3*nodescale, 0
links = mat.node_tree.links
link = links.new(texture.outputs[2], img.inputs[0])
link = links.new(img.outputs[0], bsdf.inputs[0])
link = links.new(bsdf.outputs[0], node_output.inputs[0])
return mat
Hope it helps.