1
$\begingroup$

I am trying to read the color from an HSV-Node via the scripting api. This is my material shadernodes tree (simple test scene, no other materials etc). I have confirmed that the HSV node is correctly connected to the "Base Color" input of the Principled BSDF, and changes to the Hue/Saturation/Value sliders change the appearance of the default cube using this material.

Shader node layout for test material

Why does this simple test script return [0,0,0] for the color? I expected it to be anything else? It correctly confirms that the color is coming from an input, but the color value (black) seems to be wrong.

I can reproduce the problem in Blender 3.6 and 4.0. Other nodes that output a color also produces 0,0,0,0 as value. Windows 11.

import bpy

# Get the active material
material = bpy.context.active_object.active_material

# Assuming your PrincipledBSDF node is named "Principled BSDF" in the Shader Editor
principled_bsdf_node = material.node_tree.nodes.get("Principled BSDF")

# Get the input node connected to the base_color socket
base_color_source = principled_bsdf_node.inputs['Base Color'].links[0].from_socket

# Check if there's a connected node
if base_color_source:
    # Get the output value from the connected node
    print("getting color via input")
    base_color = list(base_color_source.default_value)
else:
    # If no connected node, use the local color of the PrincipledBSDFWrapper
    print("getting local color")
    base_color = list(principled_bsdf_node.inputs['Base Color'].default_value)

print(f"Base Color of material {material.name}: is {base_color}")
$\endgroup$
2
  • 1
    $\begingroup$ There's no API for reading the computed value of a socket. In this particular case it makes sense since your node is a pure function with fixed inputs. But in general it only makes sense in the context of executing the entire shader with all of its inputs, which is only done as part of the render engine, and can't be done "standalone". $\endgroup$
    – scurest
    Commented Apr 8 at 16:21
  • $\begingroup$ You can only read the "default value" of the shader node, you can't access the value at runtime. What you may want to do is bake the values to a texture and then read the color values. Beware of color transform if you do that $\endgroup$
    – Gorgious
    Commented Apr 9 at 15:18

0

You must log in to answer this question.

Browse other questions tagged .