Need help adding image texture nodes based on the name of the material

I’m making a mod to an existing mod for BeamNG. The objects are .dae files. The geometry imports into blender with materials whose names exactly match the .dds image files used by those materials in the game engine. I’d like to automatically add an image texture node to each material and use the name of the material to point the image node to the correct file.

I tried getting meta ai to help me write the code but the fool literally tells me what it imagines the blender api would logically name objects and methods based on it’s familiarity with millions of code bases instead of actually consulting the real Blender 4.0 documentation. None of the code it gave me worked.

Of course! I’d be happy to explain each line of code in detail.

  1. material = bpy.context.object.material_slots[0].material:
    This line retrieves the material associated with the active object in Blender’s 3D view. It accesses the material slots (which hold multiple materials for an object) and selects the first slot (index 0). Then, it assigns the material from that slot to the variable _material.
  2. _material_name =
    This line is a placeholder for the material name, which would normally be assigned a value here.
  3. _texture_node = (link unavailable)(“TextureNode”):
    This line creates a new texture node (a type of node in Blender’s node editor) and assigns it to the variable _texture_node. The (link unavailable) part indicates that the node is not connected to any input.
  4. _texture_node.image = bpy.data.images.load(_material_name):
    This line loads an image file using the bpy.data.images.load() function and assigns it to the image property of the _texture_node. The image file name is passed as a parameter, which would normally be the value of _material_name.

In summary, this code seems to be setting up a texture node and assigning an image to it based on the material associated with the active object. If you have more context or questions, feel free to ask! :nerd_face:

Like you already noticed, don’t try to take shortcuts - better learn the basics. Blender’s API isn’t that nice but by googling you can get quite far.

If you know you have a material in your object, try to get it first.

Then if your material has “Use Nodes” enabled, then it has a node tree.

You can iterate all nodes of your node tree with a for loop. If you know that you for example have there an image node, you can find it like this:

if node.type == 'TEX_IMAGE':
   # do something

To load images use:

image = bpy.data.images.load(my_file_path_here)

To assign image, you can do something like this:

# assign image
node.image = image

To add new nodes, you can do this:
mtl.node_tree.nodes.new(type = 'ShaderNodeTexImage')

Instead of blindly using ChatGPT or similar conversational AI (which currently lead you to completely false solutions most of the time), try to see all the functions and variables available to you in Python console, it can autocomplete and list stuff for you (unlike Blender’s text editor). For example, if you already have your material in Python console in a variable “mtl”, you can write a line like this, and press tab to see all the available stuff:

mtl.node_tree.nodes['Image Texture'].image.

And after “.” Blender’s Console will list all kinds of stuff, which you can peek into in similar manner.

2 Likes