0
$\begingroup$

In Blender 2.8, I have an image/plane ("sky") serving as a background that shows an unwanted shadow from an object in the foreground. Manually, I can eliminate the unwanted shadow on the background 'Sky' by changing the "Diffuse BSDF" shader to "Emission."

I would like to do this in Python, but my code doesn't produce the desired result. Instead, the entire background image goes either gray or purple depending on which lines are executing. I have noted where the code starts to create unwanted results.

Any suggestions would be much appreciated. Thanks in advance!

bpy.ops.object.select_all(action='DESELECT')
bpy.data.objects['Sky'].select_set(True)
obj=bpy.data.objects['Sky']
skymaterial = bpy.data.materials.new('SkyMat')
obj.active_material = skymaterial  #UNWANTED RESULTS START HERE
skymaterial.use_nodes = True
nodes = skymaterial.node_tree.nodes
nodes.clear()
node_emission = nodes.new(type='ShaderNodeEmission')
$\endgroup$
1
$\begingroup$

I suggest that you learn how to do this manually first.

To create any material you will need a material output and link the emissive shader to that. Use the Shader Editor to review your result.

import bpy

bpy.ops.object.select_all(action='DESELECT')
bpy.data.objects['Sky'].select_set(True)

obj = bpy.data.objects['Sky']

skymaterial = bpy.data.materials.new('SkyMat')
obj.active_material = skymaterial  #UNWANTED RESULTS START HERE
skymaterial.use_nodes = True
nodes = skymaterial.node_tree.nodes
nodes.clear()


# Create needed Nodes
nodeOut = nodes.new(type='ShaderNodeOutputMaterial')
nodeEmission = nodes.new(type='ShaderNodeEmission')
nodeTexture = nodes.new(type='ShaderNodeTexImage')


# Link them together
links = skymaterial.node_tree.links
linkOut = links.new(nodeEmission.outputs[0], nodeOut.inputs[0])
linkTexture = links.new(nodeTexture.outputs[0], nodeEmission.inputs[0])

# Configure Texture node
img = bpy.data.images.load('//test.png')
img.name = 'Test'
nodeTexture.image = img
$\endgroup$
  • $\begingroup$ Thank you very much. That is an outstanding solution. Do you know of a good tutorial for understanding the concepts? $\endgroup$ – vndep Jul 25 at 18:38
  • $\begingroup$ You will find that there is a lot of well done tutorials on youtube. Search for blender material nodes. Like this for instance: youtu.be/f5Gb1VK98Wc. $\endgroup$ – Jackdaw Jul 26 at 15:46

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.