I'm working on a mesh in Blender where I've intentionally merged some vertices in Edit Mode—the geometry is exactly how I want it. However, after merging these vertices, the UV map hasn't updated accordingly. This results in gaps between UV vertices because the merged vertices in the mesh remain unconnected in the UV space.
I can manually detect these gaps and use the Stitch tool (Shift+V) to fix them, but this is time-consuming for a large number of vertices. I'd like to automate this process with a script to speed up my workflow.
My goals are:
Detect in the UV map the vertices that are on the borders of UV islands, shared by two islands, which have been merged in the mesh but remain separate in the UV map. Automatically apply the Stitch operation to these vertices to close the gaps in the UV map. Is it possible to achieve this through Python scripting in Blender? If so, how can I approach writing such a script?
Any guidance, suggestions, or examples would be greatly appreciated!
I tried simple approaches like:
import bpy
import bmesh
def merge_vertices_and_uv_stitch():
# In Edit mode
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_mode(type="VERT")
# Get the active mesh
obj = bpy.context.edit_object
me = obj.data
bm = bmesh.from_edit_mesh(me)
# Merge selected vertices at center
bpy.ops.mesh.merge(type='CENTER')
# Update the mesh
bmesh.update_edit_mesh(me)
# Apply SHIFT+V (Stitch) operation in UV space
bpy.ops.uv.stitch()
# Update the mesh again
bmesh.update_edit_mesh(me)
# Run the function
merge_vertices_and_uv_stitch()
And even more abstract ideas like this which makes things weird:
import bpy
import bmesh
def merge_vertices_preserve_uvs():
# In edit mode
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_mode(type="VERT")
# Get the active mesh
obj = bpy.context.edit_object
me = obj.data
bm = bmesh.from_edit_mesh(me)
# Ensure UV layers are available
if not bm.loops.layers.uv:
return
uv_layer = bm.loops.layers.uv.verify()
# Store original UV coordinates
original_uvs = {}
for face in bm.faces:
for loop in face.loops:
if loop.vert.select:
original_uvs[loop.vert.index] = loop[uv_layer].uv.copy()
# Merge selected vertices at center
bpy.ops.mesh.merge(type='CENTER')
# Update the mesh
bmesh.update_edit_mesh(me)
# Restore original UV coordinates
for face in bm.faces:
for loop in face.loops:
if loop.vert.index in original_uvs:
loop[uv_layer].uv = original_uvs[loop.vert.index]
# Final update
bmesh.update_edit_mesh(me)
# Run the function
merge_vertices_preserve_uvs()
No luck until now :(