3
$\begingroup$

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')

enter image description here

$\endgroup$
0

2 Answers 2

5
$\begingroup$

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]
$\endgroup$
0
1
$\begingroup$

At least for Blender 3.5 the accepted solution is missing a selection of mesh elements (verts, edges, etc.) and of UVs to take the islands from:

bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.uv.select_all(action='SELECT')

Before running bpy.ops.uv.seams_from_islands()

The full code looks like this:

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.mesh.select_all(action='SELECT')  # NEW LINE!!!
bpy.ops.uv.select_all(action='SELECT')   # NEW LINE!!!
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)

To obtain separate mesh objects for each newly splitter section, one can add this line at the end:

bpy.ops.mesh.separate(type='LOOSE')
$\endgroup$
2
  • 1
    $\begingroup$ Blender 3.7? Did I miss something? $\endgroup$ May 7, 2023 at 16:39
  • $\begingroup$ Blender 3.5.0 -- it's typo 😅 Thanks! $\endgroup$
    – MariaKo
    May 18, 2023 at 15:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .