4
$\begingroup$

How can I find out the name of a texture linked to an object?

Is there an image operator to find this out?

I searched in forums but wasn't able to find a working script, I tried this script:

import Blender
from Blender import*
img = ''
sel = bpy.context.object.name
if sel:
me = sel[0].getData(mesh=True)
for f in me.faces:
    if f.image:
        img = f.image.name
        print (img)

But the code doesn't work in my python console with Blender 2.68.

How could we find what are all the images in an object and its name and path?

$\endgroup$
1
  • 4
    $\begingroup$ This code is quite ancient (pre - blender 2.5). The Blender module does not exist anymore and we use the bpy module now for data access. $\endgroup$ Nov 26, 2013 at 14:13

3 Answers 3

9
$\begingroup$

This is a more direct version of the original script.

import bpy

obj = bpy.context.object
if obj:
    me = obj.data
    if me.uv_textures.active is not None:
        for tf in me.uv_textures.active.data:
            if tf.image:
                img = tf.image.name
                print(img)

Note that you now need to access the face's image though the uv_textures data-layer, in this case we assume one exists and choose the active one. me.uv_textures["UVTex.001"].data would work too if its present.

$\endgroup$
1
  • $\begingroup$ What is the up to date equivalent of that functionality? $\endgroup$
    – pbn
    Jan 11, 2019 at 16:21
6
$\begingroup$

This script will iterate over all objects and print texture names and image name when available, try the dump() method to find out more on attributes you are interested in. e.g. dump(mtex_slot)

import bpy

def dump(obj):
   for attr in dir(obj):
       if hasattr(obj, attr):
           print("obj.%s = %r" % (attr, getattr(obj, attr)))

print("\n\nObjects")                

for ob in bpy.data.objects:
    print(ob.name)
    for mat_slot in ob.material_slots:
        for mtex_slot in mat_slot.material.texture_slots:
            if mtex_slot:
                # dump(mtex_slot)
                print("\t%s" % mtex_slot)
                if hasattr(mtex_slot.texture , 'image'):
                    print("\t\t%s" % mtex_slot.texture.image.filepath)
$\endgroup$
1
  • 1
    $\begingroup$ This script access material images, the script in the question is asking about uv-texture images (still useful, but added own answer). $\endgroup$
    – ideasman42
    Nov 26, 2013 at 20:06
3
$\begingroup$

From the above two answer I got and This is code I desired to bring out. . . Thanks...This tells the Image name and its path.

ob = bpy.context.object
if ob:
 for mat_slot in ob.material_slots:
     for mtex_slot in mat_slot.material.texture_slots:
         if mtex_slot:
                 if hasattr(mtex_slot.texture , 'image'):
                 print("\t\t%s" % mtex_slot.texture.image.filepath)

o/p

    /home/vrmgm102/Desktop/splash.png
    /home/vrmgm102/Desktop/tree_5/tree_5.png

mtex_slot.texture --> to get Texture name

mtex_slot.texture.image --> to get Image name

mtex_slot.texture.image.filepath --> to get path and Image name

$\endgroup$

You must log in to answer this question.

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