2
$\begingroup$

I want to change the value of the Emission colour and strength of one of my materials I have set in the editor, I did the following code, but nothing happens:

led = bpy.data.objects["LED"]
led.animation_data_clear()

led_mat = bpy.data.materials['LED_mat']
led_mat.use_nodes = True

nodes = led_mat.node_tree.nodes
led_emit = nodes.new("ShaderNodeBsdfDiffuse")
led_emit.inputs["Color"].default_value = (1.0, 1.0, 0, 1)
led_emit.inputs["Color"].keyframe_insert("default_value", frame=1)

What is the correct way to do it? I needed to use the new function because even I set the Emission parameter in the editor for the material, I got the error of an undefined parameter.

$\endgroup$

1 Answer 1

1
$\begingroup$

Your code looks good. You only forgot to link the output socket of the Diffuse BSDF node you created with the input socket of the Material Output node using new_link = tree.links.new(nodes['Material Output'].inputs['Surface'], nodes['Diffuse BSDF'].outputs['BSDF'])

import bpy

led = bpy.data.objects["LED"]
led.animation_data_clear()

led_mat = bpy.data.materials['LED_mat']
led_mat.use_nodes = True

tree = led_mat.node_tree
nodes = tree.nodes

led_emit = nodes.new("ShaderNodeBsdfDiffuse")
led_emit.inputs["Color"].default_value = (1.0, 1.0, 0, 1)
led_emit.inputs["Color"].keyframe_insert("default_value", frame=1)

new_link = tree.links.new(nodes['Material Output'].inputs['Surface'], nodes['Diffuse BSDF'].outputs['BSDF'])

But it sounds like you want to use an Emission Node instead of the Diffuse BSDF. In this case you can use:

led_emit = nodes.new("ShaderNodeEmission")
led_emit.inputs["Color"].default_value = (1.0, 1.0, 0, 1)
led_emit.inputs["Color"].keyframe_insert("default_value", frame=1)
new_link = tree.links.new(nodes['Material Output'].inputs['Surface'], nodes['Emission'].outputs['Emission'])
$\endgroup$
3
  • 1
    $\begingroup$ Thank you, it works! I have a small question: Is the last colour parameter (R, G, B, X) for the alpha of the colour, or the strength of the Emission? $\endgroup$ Jan 27, 2023 at 7:15
  • 1
    $\begingroup$ @DarkPatate alpha, which for an emission shader is not useful AFAIK. The strength is inputs["Strength"]. Note that you can influence the strength of the emission if you use values > 1 in the R, G or B channels but the preferred way is way is by using the strength field $\endgroup$
    – Gorgious
    Jan 27, 2023 at 7:23
  • $\begingroup$ Does it work the same as Color? I did led_emit.inputs["Strength"].default_value = 1 led_emit.inputs["Strength"].keyframe_insert("default_value", frame=1) But it's not working, cannot see the Strength becoming yellow on the keyframe $\endgroup$ Jan 27, 2023 at 7:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .