Convert UV coordinate to face index / face pass


I would like to get faces pass, similarly to UV pass but with face index.
Currently, it is not implemented and I want to get it through the UV pass.

For that, I need to convert UV coordinate to face index.
I found the opposite (from face index to UV) by:

>    for poly in me.polygons:
>         print("Polygon index: %d, length: %d" % (poly.index, poly.loop_total))
>     # range is used here to show how the polygons reference loops,
>     # for convenience 'poly.loop_indices' can be used instead.
>     for loop_index in range(poly.loop_start, poly.loop_start + poly.loop_total):
>         print("    Vertex: %d" % me.loops[loop_index].vertex_index)
>         print("    UV: %r" % uv_layer[loop_index].uv)

I thought to use kdtree and find the nearest neighbour and then I can find the closest vertex.
However, I would like to find the face.

Does anyone have an idea how to find it?
Alternatively, is there an existed concept of face pass?

1 Like

I’m not aware of its python equivalent, but in C:

UvVertMap *uv_vert_map = BKE_mesh_uv_vert_map_create(
      mpoly, mloop, mloopuv, totpoly, totvert, limit, false, false);
// vertex_index ranges from 0 to totvert;
const UvMapVert *uv_vert = BKE_mesh_uv_vert_map_get_vert(uv_vert_map, vertex_index);
printf("%u", uv_vert->poly_index);

If only MLoopUV is exposed in python, I’d be surprised if you can get face index easily/ quickly, since it only stores a flag & coordinates.

1 Like