How to retrieve the texture associated with a texture uv?

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
MrTeapot
Posts: 0
Joined: Thu Jan 17, 2013 8:29 pm

How to retrieve the texture associated with a texture uv?

Post by MrTeapot » Thu Jan 17, 2013 9:15 pm

I am writing an exporter that among other things exports uv coordinates and the associated image name.

Back in the 2.4x day I retrieved this image information by "mesh.data.uv_textures['UVMap'].data[0].image.filepath" (I just took whatever the first uv was associated with).

I am now using 2.65 and have updated the uv export part to use the new "uv_layers and loops" interface which works fine. However, I am unable to find the image and its filename. As I understand from the documentation it should be available through "mesh.data.tessface_uv_textures['UVMap'].data.image", but my "tessface_uv_textures" is empty. I have also looked in the uv_layer, vertex and uv data structs but I am unable to find it.

To clarify, the image I am looking for is the one shown in the UVImageEditor when an object is in edit mode and its vertices selected.

If anyone can point me in the right direction it would be really appreciated.

Cheers!

CoDEmanX
Posts: 0
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Post by CoDEmanX » Fri Jan 18, 2013 11:07 am

you seem to use the tessellated geometry (tessfaces), therefore no loops are involved. But Blender works with loops, and tessfaces are only available on demand.

So make sure you update the mesh before accessing tessface* data.

- Make sure you are not in edit-mode, or uv_* and tessface_uv_* won't be available

- Ensure tessfaces are available (once they are available, they should stay in sync with the mesh...)

Code: Select all

me = ob.data
if me.polygons and not me.tessfaces:
    me.calc_tessface()
    # could also use me.update(calc_tessface=True)
- This isn't correct:
mesh.data.tessface_uv_textures['UVMap'].data.image

if the variable "mesh" is a mesh object, you should call it "ob", and its data "mesh" / "me"

instead of ['UVMap'], you should use tessface_uv_textures.active.data ...

to access a tessface' image, you need to use the face index:
tessface_uv_textures.active.data[#num].image
(e.g. [0] for the image of the first face)
I'm sitting, waiting, wishing, building Blender in superstition...

MrTeapot
Posts: 0
Joined: Thu Jan 17, 2013 8:29 pm

Post by MrTeapot » Fri Jan 18, 2013 11:48 am

Hi CoDEmanX,

Thanks for the super-quick and useful reply!

Indeed, what I had failed to do was generate the tessface data. After I added the calc_tessface() command it all works nicely, hooray!

I also discovered that it is actually still possible to use the old interface so o.data.uv_textures['UVMap'].data[0].image and o.data.tessface_uv_textures['UVMap'].data[0].image gives the same object, I just assumed it wasn't available since the uv data was removed from o.data.uv_textures (assumption really is the mother of all f#"!-ups!).

Post Reply