3
$\begingroup$

For example I load 2 images (with suffixes: _normal and _color) into Blender. I'm using this code for importing images:

supported_file_types = '.png', '.jpg', '.tif', '.hdr', '.bmp', '.dds', '.psd'
def path_iterator(path_name):
for fp in os.listdir(path_name):
    if fp.endswith(supported_file_types) and ((bpy.context.scene.color_string) in fp[-10:]) or ((bpy.context.scene.normal_map_string) in fp[-10:]) or ((bpy.context.scene.height_map_string) in fp[-10:]) or ((bpy.context.scene.metallic_map_string) in fp[-12:]) or ((bpy.context.scene.rough_string) in fp[-15:]):
        yield fp

Then I have those images in Image Editor.

enter image description here

How to find imported image by its suffix and assign to Image Node? I can't use the exact name of those textures just suffix, so this method won't work:

image_node.image = bpy.data.images['YOUR_IMAGE_NAME']
$\endgroup$

1 Answer 1

5
$\begingroup$

you can iterate over the currently referenced images in bpy.data.images.

def get_img_reference(suffix):
    images = bpy.data.images
    for img in images:
        if suffix in img.name:
            return img

img = get_img_reference('_Roughness')
if img:
   image_node.image = img

It's possible to write this shorter but the extra checking is a sanity check. This is case sensitive, beware :)

$\endgroup$
5
  • $\begingroup$ Thank you, but I don't know the exact name of the textures. I know only the suffix "_roughness". $\endgroup$
    – Mc Gavish
    Commented Oct 16, 2015 at 15:34
  • 1
    $\begingroup$ you may want to learn more about Python string object, it's not strictly a Blender issue. docs.python.org/3.4/library/stdtypes.html#string-methods $\endgroup$
    – zeffii
    Commented Oct 16, 2015 at 15:41
  • 1
    $\begingroup$ Thank you so much! It works perfectly. I will check this link. $\endgroup$
    – Mc Gavish
    Commented Oct 16, 2015 at 15:47
  • $\begingroup$ I have one more question. Is it possible to search only in just imported images? When I use this addon three times I will have three different images but with the same suffix. $\endgroup$
    – Mc Gavish
    Commented Oct 16, 2015 at 16:00
  • 1
    $\begingroup$ not really, you might be able to use the bpy.data.images[0].filepath to your advantage, or make a set() from the names of images present before importing, then do a boolean difference on the set() of image names after importing, and only use the new ones. But this is a separate question which I'm happy to answer If you ask as a new Question (not adding it to this question) $\endgroup$
    – zeffii
    Commented Oct 16, 2015 at 16:46

You must log in to answer this question.

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