How to update UVs after a geometry modification in bmesh

I did the most simple code I could. you need to select 2 verts in edit mode on the default cube. which has already UVs. So my question is how to update UVs to the new geometry?

import bpy,bmesh
me = bpy.context.object.data
bm = bmesh.from_edit_mesh(me) 
bm.verts.ensure_lookup_table()
hist = bm.select_history
v1=hist[0]
v2=hist[1]         

v1.co = v1.co.lerp(v2.co,1)
bmesh.ops.remove_doubles(bm, verts=list(hist), dist=0.0001)

#there? if I want to update UVs
uv_layer = bm.loops.layers.uv.active
for face in bm.faces:
    for loop in face.loops:
        loop[uv_layer].uv = ?

bm.normal_update()
bmesh.update_edit_mesh(me)

uvs are modified by accessing their layer data, which is stored in loops. hereā€™s a similar question over on stack exchange that has some example code in the solution that should help get you going: https://blender.stackexchange.com/questions/53709/bmesh-how-to-map-vertex-based-uv-coordinates-to-loops

ok I give up this idea correct uv option in some operator gives really strange things

I thought it was possible to do same operation on UV that on vertices like a merge but apparently like several loops are linked to the two vertices this is a messā€¦

right- thatā€™s kinda what i was talking about, UVs are stored per LOOP, not per vert. there is no such thing as ā€œmerging uvsā€. If two uvs share an edge and are cospatial then they are ā€˜mergedā€™.

and itā€™s not really a mess, thatā€™s just how UVs work. In other tech (unity and UE4 for example) uvs are stored per triangle, and itā€™s the same thing- there is no ā€˜mergingā€™ of uvs.

1 Like

I just remembered a lengthy post I made a year or so ago over on devtalk that goes into much greater detail about all of this in case youā€™re interested in learning more about it:

1 Like

I understood what you explained and I saw it before but I really donā€™t see how to transform my code to have the wanted result. before I was using the remove double operator, it has an option correct UVs. so if you are merging several vertices on 2 edges you get a hole in UVs without correct UVs (the missing face). but now Iā€™m using a lerp fonction to reduce distance between vertices and at value=1 itā€™s merging (bmesh remove double which has not uv option). what is happening at line 147 in the script https://github.com/1C0D/Remove_doubles_plus/blob/master/remove_doubles_plus%20v1_6_1.py