access bpy.data.images['imgname.png'] from object (blender 2.66.1)

Hi all,
my question is quite simple. i need to find the name of the texture applied to a model. the image name is showed on the structure bpy.data.images[“imgname.png”], the users property equals 1. this means that the image is used.
How to build my scenario.

  1. add a model (cube or whatever)
  2. unwrap textures
  3. on the uv image editor panel image ->open image
  4. on 3d view, viewport shading texture
    and the texture should appear on the model.

I have a scene with different models, with different textures (usually 1 texture for model).
In my custom exporter i have to cycle on models and save the texture name.

Thanks,
Andrea

just to make sure: you really want to know only the name of what blender calls “texture” right? Not image name, not material name, not any kind of filepath, just texture name?

Well there’s a general problem with blender, you assign images in uv editor, but textures in material tab.

This gives for example the uv-editor assigned image of the first face:

>>> bpy.context.object.data.uv_textures.active.data[0].image
bpy.data.images[‘cube loops.png’]

and this the material index as assigned in the material tab:
>>> bpy.context.object.data.polygons[0].material_index
0

And this uses the index to find one of the texture names (texture “Tex”)
>>> me = bpy.context.object.data.
>>> me.materials[me.polygons[0].material_index].texture_slots[‘Tex’].name
‘Tex’

You may look at the export scripts, e.g. OBJ, or here is my blender-cod export script:

http://code.google.com/p/blender-cod/source/browse/blender_26/export_xmodel.py

(search for “material” to see where it handles them)

Hi! thank you for your promptly and complete response. You showed me the right way: what i wanted to achieve was exactly:

for obj_to_process in obj_to_export:
    if obj_to_process.data.uv_textures.active.data[0] is not None:        myscriptclass.add_texture(obj_to_process.data.uv_textures.active.data[0].image.name)

This almost works, but in a test i just did i had:

>>> background.data.uv_textures.active.data[0].image
bpy.data.images[‘background.jpg’]

>>> background.data.uv_textures.active.data[1].image
bpy.data.images[‘background.jpg’]

>>> background.data.uv_textures.active.data[2].image
bpy.data.images[‘crayons-xchng.jpg’]

Now in the 3dView the image visible is ‘crayons-xchng.jpg’, that is the one i opened from the uv_editor. What is the criteria to choose the index of data correctly to have a WYSIWYG behaviour?

by the way, from background.data.uv_textures.active.data[2].image to background.data.uv_textures.active.data[30].image i found bpy.data.images[‘crayons-xchng.jpg’]

A.

PS. it seems that the vector data contains info relative to faces, so you can have different images for different faces. This solves my question above.