Get selected UV vertices coordinates

Hi

I’m trying to get the selected UV vertices coordinates but unfortunately it does not work.


import bpy
import math

mesh = bpy.context.object.data

uv = mesh.uv_layers.active.data

print(uv[0].uv)

Thanks in advance.

Does the object have an uv_layer??

Or even worse, is the object data a mesh?

yes the object has a UV layer, and the object data is a mesh (cube).

Just because that code works… thought it just prints the first loop uv.

here’s another example that prints all uvs:


import bpy
obj=bpy.context.active_object
if obj.type=='MESH':
    obD=obj.data
    if obD.uv_layers.active:
        for poly in obD.polygons:
            for loop in poly.loop_indices:
                uv=obD.uv_layers[0].data[loop].uv
                print('Vertex[', obD.loops[loop].vertex_index, '].uv = ', uv)


Thanks for the fast reply, whenever I run the script I get a error message: “IndexError: bpy_prop_collection[index]: index 0 out of range, size 0” , actually the same error message that appeared with the script I wrote.

can you post the blend file you’re using? and btw what’s your blender version?

For the blend file it is just the default scene when you open blender (default cube) , I’m using blender 2.79 official build

the default cube doesn’t have UV’s…
you need to unwrap the cube first, and then run the script

Yes of course I did unwrap the cube then I ran the script

Then I cannot help, nor even understand the problem… In my end, everything is working as expected.
can you post to complete output of the console?


Thank you for your help.

exit the edit mode before running the script. :wink:

Thank you very much, it works perfectly I really appreciate your help! :o