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