As madlaina says, A vertex doesn't have a 1:1 corraspondance with UV coordinates.
However using BMesh you can loop over a vertices connected face corners (loops), and find the UV's used by a vertex.
Heres a script that prints all vertices UV's in editmode.
Both the first-uv found (which is OK if you assume all UV's attached to a vertex will be the same).
And the mean average.
import bpy
import bmesh
from mathutils import Vector
obj = bpy.context.edit_object
me = obj.data
bm = bmesh.from_edit_mesh(me)
def uv_from_vert_first(uv_layer, v):
for l in v.link_loops:
uv_data = l[uv_layer]
return uv_data.uv
return None
def uv_from_vert_average(uv_layer, v):
uv_average = Vector((0.0, 0.0))
total = 0.0
for loop in v.link_loops:
uv_average += loop[uv_layer].uv
total += 1.0
if total != 0.0:
return uv_average * (1.0 / total)
else:
return None
# Example using the functions above
uv_layer = bm.loops.layers.uv.active
for v in bm.verts:
uv_first = uv_from_vert_first(uv_layer, v)
uv_average = uv_from_vert_average(uv_layer, v)
print("Vertex: %r, uv_first=%r, uv_average=%r" % (v, uv_first, uv_average))