python: Split mesh by uv islands This question has a solution, but it doesn't work properly. It looks like a mesh is splitting randomly.

Can you help me to fix the script, or indicate me what I'm doing wrong?

Code by TLousky:

import bpy, bmesh
C = bpy.context

ob = C.object

bpy.ops.object.mode_set(mode = 'EDIT') 
bm = bmesh.from_edit_mesh( C.object.data )

faceGroups = []
bm.faces.ensure_lookup_table()

bpy.ops.mesh.select_mode(use_extend=False, use_expand=False, type='FACE')
bpy.context.scene.tool_settings.use_uv_select_sync = True

for face in bm.faces:
    allFaceIndices = [ i for fg in faceGroups for i in fg ]

    if face.index not in allFaceIndices:
        bpy.ops.mesh.select_all( action = 'DESELECT' )

        face.select = True
        bm.select_flush(True)

        bpy.ops.uv.select_linked()

        faceGroups.append( [ f.index for f in bm.faces if f.select ] )

bpy.context.scene.tool_settings.use_uv_select_sync = False

for g in faceGroups:
    bm.faces.ensure_lookup_table()
    bpy.ops.mesh.select_all(action='DESELECT')
    for i in g: bm.faces[i].select = True
    bm.select_flush(True)
    bpy.ops.mesh.split()

ob.data.update()
bpy.ops.object.mode_set(mode = 'OBJECT')
Using bpy.ops.uv.seams_from_islands(...)



Here is a script to run in edit mode.

import bpy
import bmesh
context = bpy.context
obj = context.edit_object
me = obj.data
bm = bmesh.from_edit_mesh(me)
# old seams
old_seams = [e for e in bm.edges if e.seam]
# unmark
for e in old_seams:
    e.seam = False
# mark seams from uv islands
bpy.ops.uv.seams_from_islands()
seams = [e for e in bm.edges if e.seam]
# split on seams
bmesh.ops.split_edges(bm, edges=seams)
# re instate old seams.. could clear new seams.
for e in old_seams:
    e.seam = True
bmesh.update_edit_mesh(me)


You may want to remove boundary seams

boundary_seams = [e for e in bm.edges if e.seam and e.is_boundary]