# How to get the UV corresponding to a vertex via the Python API?

How do I get a selected vertex and it's corresponding uv vertex?

A vertex I can get with

bpy.context.active_object.data.vertices[0]


A uv vertex I can get with

bpy.context.active_object.data.uv_layers['UVMap'].data[0]


But the index is not the same for uv and mesh vertex.

• In your first line you retrieve a vertex from the active object's mesh, in the second line you retrieve the uv layer from the active object's mesh, and you don't need to add '.data[0]'. What you are refering to as the "index" of the uv map is its name itself, there is not way you will have a correlation between a mesh's vertices index and a mesh's UV_layer – Yvain Mar 23 '16 at 12:11

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):
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
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))

• exactly what I was looking for. Thank you! – ndee Mar 23 '16 at 13:33

The thing you need to consider is that a vertex can have multiple uv-coordinates (for example if you unwrap two faces then there are two uv coordinates for one vertex where the seams are)

The UV Coordinates are actually stored per face-loop and not per vertex. It is a good idea to read up on this here to understand the concept better:

https://wiki.blender.org/index.php/Dev:Source/Modeling/BMesh/Design

Maybe you can give us more information, what you would like to do with the uv coordinates.