0
$\begingroup$

Still pretty new to blender here, but wow, i'm really enjoying working with it. Next question i've tried to search for information on, is: Is there any way to extract the input- and output-values from the blender shaders while they are applied to the object/mesh?

E.g. I have a cube, and i have complex setup of shader-nodes attached to that cube. Is there any way for me to dig into the code of those shaders that are attached to the cube? Can only assume the code is written in python, but would be fun to look at and maybe play around with the code.

Going to translate the values to GLSL to customize a fragment shader which I am going to use on the web, if that makes sense(?)

Wish you all a wonderful day!

$\endgroup$
6
  • 2
    $\begingroup$ What do you mean look at the code? Shaders are not written in Python, they are either C or C++. You can look at the source code of the shaders in Blender repository $\endgroup$ Commented Jun 25 at 9:07
  • $\begingroup$ Duarte is right, the shading code is not written in Python. It's C or C++ in which Vulkan is used to convert the nodes (using a stack machine) to GLSL (openGL Shading Language). $\endgroup$ Commented Jun 25 at 12:08
  • $\begingroup$ So what you actually want is access your nodes' values and connections via python, please edit your question to reflect that. You could also explain why you want to do that, this sounds like a potential XY problem situation waiting to happen, people could help with it to. $\endgroup$
    – Lauloque
    Commented Jun 25 at 12:11
  • 1
    $\begingroup$ Possibly useful: What's the most reliable way of converting a material into reusable code blocks? $\endgroup$ Commented Jun 25 at 12:40
  • $\begingroup$ @MarkusvonBroady Managed to extract the values and links, so the questions is solved, i think. As long as nobody know of an add-on (or similar) that could just extract the nodes-values and links directly to GLSL? $\endgroup$
    – Martin
    Commented Jun 25 at 13:12

1 Answer 1

1
$\begingroup$

What i was looking for was the input and output values for each node e.g.:

  • Node: Noise Texture.001
  • Type: TEX_NOISE
  • Input Vector is linked to Noise Texture.002
  • Input W has value 0.0
  • Input Scale has value 1.4000000953674316 Input Detail has value 2.0
  • Input Roughness has value 0.5
  • Input Lacunarity has value 2.0
  • Input Distortion has value 0.19999998807907104
  • Output Fac is not linked
  • Output Color is linked to Mix.006

But I figured I'd just had to write a script in Python to extract the information to my console.

I wrote a fairly simple script, there are certainly people here who have far more knowledge than me on this topic, but here is the script I used to query shader node trees from the console:

import bpy

# Specify your material in "Material_Name"
material_name = "Material_name"

# Get the material
material = bpy.data.materials.get(material_name)

if material and material.use_nodes:

    # Get the node tree
    node_tree = material.node_tree
    nodes = node_tree.nodes
    
    # Iterate through the nodes
    for node in nodes:
        print("Node:", node.name)
        print("Type:", node.type)
        for input in node.inputs:
            if input.is_linked:
                linked_node = input.links[0].from_node
                print(f"  Input {input.name} is linked to {linked_node.name}")
            else:
                try:
                    print(f"  Input {input.name} has value {input.default_value}")
                except AttributeError:
                    print(f"  Input {input.name} does not have a default value")
        for output in node.outputs:
            if output.is_linked:
                for link in output.links:
                    linked_node = link.to_node
                    print(f"  Output {output.name} is linked to {linked_node.name}")
            else:
                print(f"  Output {output.name} is not linked")
else:
    print("Material not found or it does not use nodes.")
$\endgroup$
4
  • 1
    $\begingroup$ Was this generated by artificial intelligence, chat assistants or otherwise with help from large language models? According to our site rules decided by the community through democratic voting, we do not currently accept answers generated by artificial intelligence, chat assistants or otherwise with help from large language models. If that is the case here, we'd kindly ask you to delete the post so as not to infringe the rules. Thanks for understanding $\endgroup$ Commented Jun 25 at 22:04
  • $\begingroup$ Oh, you wanted a reference lookup of all the nodes’ inputs and outputs with their default values. In that case, this might help you: docs.blender.org/manual/en/latest/render/shader_nodes/… $\endgroup$
    – TheLabCat
    Commented Jun 27 at 1:13
  • $\begingroup$ @DuarteFarrajotaRamos As long as you don't count my dad as a large language model i'm good😅 But he has been working at HP for the past 30 years or so, so maybe he should be... $\endgroup$
    – Martin
    Commented Jun 27 at 8:36
  • $\begingroup$ Fair enough, dads don't count as LLMs even though they often have vast knowledge on various fields. $\endgroup$ Commented Jun 27 at 10:02

You must log in to answer this question.

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