# Python 2.8 creating Diffuse BSDF in a shader node, when I want it in a surface node

In Blender/Python 2.8, I can't get my code to create the surface node I want (it creates a shader node along with a surface node that I don't want). The objective is to assign an image texture to an object as seen in three related posts (1, 2 and 3):

The manual process produces the desired result by setting up the node as follows:

In Python I am attempting to do the same (code below), but the results aren't working out. First I'll show the output, then the code:

Here's the objectionable output:

And here's the node tree as created by my code; when I make the manual adjustments I note in the picture I get the final result that I want:

My flawed code is below. Before you criticize the obvious problem (that I am creating shaders), I have tried other approaches (replacing "Shader" with "Surface" and various other words) without success. I am also baffled as to why the "Principled BSDF" node appears out of nowhere.

Here's my code. Your suggestions would be highly appreciated. Thanks in advance!

matP_name='CCmat'
matP = bpy.data.materials.get(matP_name)
if matP is None:
matP = bpy.data.materials.new(matP_name)
matP.use_nodes=True
nodes = matP.node_tree.nodes
diffuse_BSDF = matP.node_tree.nodes.new(nodeID)
path=TextureFolder+TextureFilenamePost
node_texture.image = imgTex
node_texture.location = 0,200
obj=bpy.data.objects['CubeCopy1']
obj.active_material=matP

• If I read correctly this code does not link the diffuse to the output. Also creating a new material will create principled and output by default. So you need to clear them. material.node_tree.links.clear() material.node_tree.nodes.clear() – lemon Jul 29 '19 at 16:02
• @lemon Doesn't this create the link? link = links.new(node_texture.outputs[0], nodes.get('Diffuse BSDF').inputs[0]) – vndep Jul 29 '19 at 16:11
• Yes but between texture and diffuse, not between diffuse and output – lemon Jul 29 '19 at 16:11
• @lemon What python code would link diffuse and output? – vndep Jul 29 '19 at 16:13

Small code that can bring the indication you need.

import bpy

# Clear all nodes in a mat
def clear_material( material ):
if material.node_tree:
material.node_tree.nodes.clear()

# Create a node corresponding to a defined group
def instanciate_group( nodes, group_name ):
group = nodes.new( type = 'ShaderNodeGroup' )
group.node_tree = bpy.data.node_groups[group_name]

materials = bpy.data.materials

mat_name = 'test'

material = materials.get( mat_name )

if not material:
material = materials.new( mat_name )

# We clear it as we'll define it completely
clear_material( material )

material.use_nodes = True

nodes = material.node_tree.nodes

output = nodes.new( type = 'ShaderNodeOutputMaterial' )

diffuse = nodes.new( type = 'ShaderNodeBsdfDiffuse' )

#With names