Getting UV Selection Data (solved)

Trying to get data related to which verts/edges/faces are selected in the UV editor, but have only been able to get results from the 3D Viewport. If the related data is accessible, I’d eventually like to make UI selections in the UV editor affect operations in the 3D viewport (such as an edge loop cut).

Example


It be nice, for example, to print out the values of the four vertices selected in the UV Editor (left panel) pictured above.

Where I’m at

It looks like selection related details are documented under MeshUVLoop. It’s unclear if MeshUVLoop.select would describe the UV vertex or the 3D view’s. But I’ve only been able to access data related to MeshUVLoopLayer anyway - which does not have a selection attribute.

Accessing MeshUVLoopLayer

import bpy
main_element = bpy.context.edit_object.data
main_element.uv_layers[0]
=> bpy.data.meshes['Cube'].uv_layers["UVMap"]

you can already do this if you enable “Sync Selection”.

To answer your question though, the correct way to do this would be using bmesh (given that your mockup is in edit mode, and the object’s mesh data is not guaranteed to be accurate until you leave edit mode).

Disclaimer: This is all off the top of my head so it hasn’t been tested for any errors, but is probably close enough that you can figure it out from here:

import bpy
import bmesh

bm = bmesh.from_edit_mesh(bpy.context.active_object.data)
uv_layer = bm.verts.layers.uv.verify()

for face in bm.faces:
    print(f"UVs in face {face.index}")

    for loop in face.loops:
        uv = loop[uv_layer]
        print(f"    - uv coordinate: {uv.co}, selection status: {uv.select}, corresponding vertex ID: {loop.vert.index}")
1 Like

Thanks @testure - after the second time getting this code sample from you, some lingering lessons are finally sinking in.

Not vorking for me (Blender 2.93.1) - AttributeError: 'BMLayerAccessVert' object has no attribute 'uv'


In API documentation attribute uv exist for loops but now I stuck again… no attribute co.

I just want to access and set face UV coordinates whine in edit mode by addon. Way like this by using bpy.data.meshes['Cube'].uv_layers.active.data[0].uv not work in Edit Mode. I guess it need to use Bmesh module. Any ideas about this?