0
$\begingroup$

Linked to my previous question about the colour, I would like to modify the strength of the emission. I did something similar with the colour, but it's not working:

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("ShaderNodeEmission")
led_emit.inputs["Color"].default_value = (1.0, 1.0, 0, 1)
led_emit.inputs["Color"].keyframe_insert("default_value", frame=1)
led_emit.inputs["Strength"].default_value = 10
led_emit.inputs["Strength"].keyframe_insert("default_value", frame=1)
new_link = tree.links.new(nodes['Material Output'].inputs['Surface'], nodes['Emission'].outputs['Emission'])

The colour is modified, but the strength stays at 1. Is there an additional parameter regarding the strength?

$\endgroup$
2
  • $\begingroup$ Several emission nodes, probably, replace the last line by "new_link = tree.links.new(nodes['Material Output'].inputs['Surface'], led_emit.outputs['Emission'])" $\endgroup$
    – lemon
    Jan 29, 2023 at 9:49
  • $\begingroup$ Thanks it worked! Another question: when I put the strength to 0 the material becomes black instead of not emitting light, is it normal? $\endgroup$ Jan 29, 2023 at 10:03

1 Answer 1

0
$\begingroup$

Your script works but each time you run it, it creates a new emission node.

And as you are using nodes['Emission'].outputs['Emission'] to create the link, you always connect the first that have been created.

I suggest to define a function that will retreive the node if it already exists or create a new one if not. As in the code below.

import bpy

def get_or_create_node(nodes, type, class_name):
    # Find a previous node of type
    for node in nodes:
        if node.type == type:
            return node

    # if it does not exist create it
    return nodes.new(class_name)
    
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 = get_or_create_node(nodes, "EMISSION", "ShaderNodeEmission")
output_material = get_or_create_node(nodes, "OUTPUT_MATERIAL", "ShaderNodeOutputMaterial")

led_emit.inputs["Color"].default_value = (1.0, 1.0, 0, 1)
led_emit.inputs["Color"].keyframe_insert("default_value", frame=1)
led_emit.inputs["Strength"].default_value = 2
led_emit.inputs["Strength"].keyframe_insert("default_value", frame=1)
new_link = tree.links.new(led_emit.outputs['Emission'], output_material.inputs['Surface'])

Concerning your second question (in the comments), yes this is normal for an emission node. When emission strength is set to 0, no strength, no light, so the objet is black.

If you want an object that keeps its color when not emitting light, use a principled bsdf node instead. The code could be the following.

import bpy

def get_or_create_node(nodes, type, class_name):
    # Find a previous node of type
    for node in nodes:
        if node.type == type:
            return node

    # if it does not exist create it
    return nodes.new(class_name)
    
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 = get_or_create_node(nodes, "BSDF_PRINCIPLED", "ShaderNodeBsdfPrincipled")
output_material = get_or_create_node(nodes, "OUTPUT_MATERIAL", "ShaderNodeOutputMaterial")

led_emit.inputs["Base Color"].default_value = (1.0, 1.0, 0, 1)
led_emit.inputs["Emission"].default_value = (1.0, 1.0, 0, 1)
led_emit.inputs["Emission Strength"].default_value = 0
new_link = tree.links.new(led_emit.outputs['BSDF'], output_material.inputs['Surface'])
$\endgroup$
7
  • $\begingroup$ Thank you, I used the last solution. The strength is changing (as well as the base colour), but the emission colour is keeping the same, should I address the colour node differently? $\endgroup$ Jan 30, 2023 at 8:24
  • $\begingroup$ it depends on the result you want, you can effectively lower the emission color as well as the strength. $\endgroup$
    – lemon
    Jan 30, 2023 at 17:08
  • $\begingroup$ I mean, the colour is not changing, it is keeping green (the colour set manually before), even if I set another colour (at the initialisation like in my post that should be yellow, or later with a for loop) $\endgroup$ Jan 31, 2023 at 4:27
  • $\begingroup$ @DarkPatate, what is your code, exactly? $\endgroup$
    – lemon
    Jan 31, 2023 at 6:41
  • $\begingroup$ for simplicity, please upload here a blend file showing the issue. $\endgroup$
    – lemon
    Jan 31, 2023 at 6:57

You must log in to answer this question.

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