How to get material parameter by name, and check if texture is assigned to parameter I'm writing a exporter, and I have most of it already working, the missing piece now is to export the texture maps properly. I'm having trouble understanding how I can fetch those with python.

I have written custom nodes in c, and the export routine is currently written in python.

My current material export works fine, color \ float values and all that works, but as soon as I assign a texture into a color slot I'm having trouble accessing that texture.

So, my question is this:

How can I loop through each parameter in a material node, check the parameter by name (if I'm looking for a specific one), then check if a texture is assigned to that parameter?

Take for example the 'kd' slot here:



The kd color parameter I fetch like this:

nodes = mat.node_tree.nodes
r = nodes["Matte"].inputs[0].default_value[0]
g = nodes["Matte"].inputs[0].default_value[1]
b = nodes["Matte"].inputs[0].default_value[2]


And I know using the name 'matte' there is wrong, what I would like is to get the name of the 'class' of material - then check if the material is of type 'matte', instead of relying on the name. (only way I can explain it, I come from 16 years of 3dsmax, and that's one way of checking there when you write scripts). So a pointer on that too would be nice.

And now I have my next question - how can I check if a texture map is assigned like this on that parameter:



I would like to get that texture info (file path) if a texture is assigned to it.
I am going to link this here as intro:

Control Cycles material nodes and material properties in Python

To see what and if something is connected to your Kd socket:

kd_socket = nodes["Matte"].inputs[0]
# you can check if kd_socket.name returns "Kd"
# and kd_socket.type returns 'RGBA'

links = mat.node_tree.links
link = next(l for l in links if l.to_socket == kd_socket)
if link:
    image = link.from_node.image          # = bpy.data.images['Name']


In Blender everything is referenced by names, they are unique by design. If you don't know the name, you need to search through all items and pick what you want based on some criteria. You can recognize nodes by:


type: link.from_node.type         # 'TEX_IMAGE'
number of inputs/outputs, type of sockets, etc.
how the nodes are linked with links


You get the node class with:

mat.node_tree.nodes["Diffuse"].bl_idname
# 'ShaderNodeBsdfDiffuse'