How to get the name of image of "Image Texture" with python? I would like to get the name of of image of "Image Texture" like "Wood8" or "Wood8.png" below with python.



I tried to use object.material_slots[index].material to get materials, and it work.

However, then I tried to use material.texture_slots.texture.name to get it, it didn't work at all.

I have no idea what to do next?

2017/06/05 edited
@TLousky

I tried to use

import bpy
import bmesh
'''
I would like to get all textures 
'''
scene = bpy.context.scene
objects_in_scene = bpy.data.objects
texture_list = []
for obj in objects_in_scene :
    # Enter EDIT mode of this object since bmesh only can be acess in EDIT mode
    if obj.type == 'MESH':
        scene.objects.active = obj
        bpy.ops.object.mode_set(mode = 'EDIT')
        me = obj.data
        # Acess bmesh of this mesh
        bm = bmesh.from_edit_mesh(me)
        for f in bm.faces:
            if len(obj.material_slots) != 0:
                mat = obj.material_slots[f.material_index].material
                # Check there is a material in this material slot 
                if mat is not None:
                    # go through all texture slots in the material slot
                    for mtext_slot in mat.texture_slots:
                        #print(mtext_slot)
                        # Check the texture slot is not None
                        if mtext_slot is not None:
                            text = mtext_slot.texture
                            # Check there is a texture in this texture slot 
                            if text is not None:
                                # If the texture hasn't be counted ever, then Count it
                                if text.type == 'IMAGE':
                                        if text.image.name not in tList: 
                                            tList.append(text.image.name)
                                            print(text.image.name)
print(texture_list)`


to get all textures of all objects in the scene.

However, I got "[]" meaning nothing as feedback.

Then I added print(mtext_slot) in line 24 to get what is in the material.texture_slots

And I got the following feedback

`&lt;bpy_struct, MaterialTextureSlot("Tex")&gt;
None
None
None
None
None
None
None
None
None
&lt;bpy_struct, MaterialTextureSlot("Tex")&gt;
None
None
None
None
None
...


It seems there was something wrong in my codes
The screenshot of your texture indicates you are using cycles, the material texture slots are only used by blender internal. For a cycles material, you need to loop through the node tree and get the image info from any image texture nodes to find what images are being used.

import bpy

texture_list = []

for obj in bpy.context.scene.objects:
    for s in obj.material_slots:
        if s.material and s.material.use_nodes:
            for n in s.material.node_tree.nodes:
                if n.type == 'TEX_IMAGE':
                    texture_list += [n.image]
                    print(obj.name,'uses',n.image.name,'saved at',n.image.filepath)

print(texture_list)


If you wanted to list by face what images are used then you can loop through the objects face data -

for obj in bpy.context.scene.objects:
    if obj.type == 'MESH':
        for f in obj.data.polygons:
            mat = obj.material_slots[f.material_index].material
            if mat and mat.use_nodes:
                for n in mat.node_tree.nodes:
                    if n.type == 'TEX_IMAGE':
                        texture_list += [n.image]
                        print('Face', f.index, 'uses', n.image.name)

print(texture_list)