0
$\begingroup$

In Blender/Python 2.8, I am trying to create an image texture node for an object. The last line of the code below throws this error:

AttributeError: 'NoneType' object has no attribute 'inputs'

I tried replacing 'Diffuse BSDF' with 'ShaderNodeTexImage' and it still threw the same error.

Your suggestions would be much appreciated. Thanks in advance.

material = bpy.data.materials.get(material_name)
if material is None:
    material = bpy.data.materials.new(material_name)
material.use_nodes=True
nodes = material.node_tree.nodes
material_output = nodes.get("Material Output")
path=TextureFolder+TextureFilenamePost
imgTex = bpy.data.images.load(path)
node_texture = nodes.new(type='ShaderNodeTexImage')
node_texture.image = imgTex
node_texture.location = 0,200
links = material.node_tree.links
link = links.new(node_texture.outputs[0], nodes.get("Diffuse BSDF").inputs[0])
$\endgroup$
1
$\begingroup$

This happens because there is no Diffuse BSDF in your material. If you create a new material with bpy.data.materials.new() it will contain only as Principle BSDF and Material Output Node. You need to create the Diffuse BSDF node first.

nodes.get("Diffuse BSDF") returns None because it can't find a node with that name. The error occurs when you try to access input[0], which as the errors says, is no attribute of NoneType.

$\endgroup$
  • $\begingroup$ Thanks. @rjg - This gives rise to another set of issues with the same code which I raise in this post: blender.stackexchange.com/questions/146746/… If you care to comment on the follow on post, it would be greatly appreciated. $\endgroup$ – vndep Jul 29 at 15:53
  • $\begingroup$ @vndep I was wrong about the material being empty. It does create a Principle BSDF and Material Output Node by default. Sorry for that. $\endgroup$ – Robert Gützkow Jul 29 at 16:15
  • $\begingroup$ That's OK - I appreciate any advice you can give! I've whittled the problem down to a simple issue of linking the Diffuse BSDF to the output. Do you have any suggestions? I am now on this question...blender.stackexchange.com/questions/146746/… $\endgroup$ – vndep Jul 29 at 16:21
  • 1
    $\begingroup$ @vndep lemons answer should work well. Basically you need to connect the BSDF output socket from the Diffuse BSDF node with the Surface socket input of the Material Output node. So something like links.new(diffuse_BSDF.outputs["BSDF"], nodes.get("Material Output").inputs["Surface"]) $\endgroup$ – Robert Gützkow Jul 29 at 16:23
  • 1
    $\begingroup$ It's better to do thing cleanly as lemon has shown by clearing the initial material and checking the return value of .get(). $\endgroup$ – Robert Gützkow Jul 29 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.