1
$\begingroup$

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.)

$\endgroup$
2
$\begingroup$

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
    bpy.data.images.load(img_full_path, check_existing=True) 
    #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.

$\endgroup$
  • $\begingroup$ 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.. $\endgroup$ – batFINGER Feb 19 at 12:58
  • $\begingroup$ Edited according to suggestion. $\endgroup$ – miceterminator Feb 19 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.