# From a material's name, automatically “fill it in”, and have it point to a same-named image texture

I am attempting to make it so that I can do the following:

1. Import meshes. These add a large number of materials. already contain the (empty materials).

2. Add all of the image texture files.

3. Make all of the previous materials refer to the image texture files based on the materials' filenames. (I.E. if the material said brickfloor001, it would build a material that has one image texture named brickfloor001)

However, I have been unable to do the third step, automating the node setup and adding in the images automatically. What is a method / addon for doing this?

I've already checked Node Wrangler's texture setup. This still forces you to go material-by-material.

(I want to avoid manually creating the materials, because this is nearly a hundred different ones.)

Under the assumption, that you have one 'Image Texture' node in your material setup and all the image filenames are unique, it should be something like :

for material in bpy.data.materials:
#loops through all the materials
mname = material.name
#get the name of the material
img_fname = "{}.png".format(mname)
#get mname.png
img_full_path = "path/to/image/files/{}".format(img_fname)
#get path to image, including the image
#load the image to the internal database
material.node_tree.nodes['Image Texture'].image =bpy.data.images[img_fname]
#assign image by filename to material image texture input


You need to work out the path to the image files. Best to open the python console and try to load one image manually, using the

bpy.data.images.load()


command, then insert the base path that worked.

Edit: img_fname was in quotation marks in bpy.data.images. That would not have worked.

• Recommend img = bpy.data.images.load("/somepath/someimage.png", check_existing=True) WIthout the check existing will load same image and append .nnn to image name. Using foo.bar.image = img will make sure it's the newly loaded image, not the first existing image with that name.. – batFINGER Feb 19 '19 at 12:58
• Edited according to suggestion. – miceterminator Feb 19 '19 at 14:47