Is it possible to create vertex groups from seams via python?

I want to automate the creation of vertex groups using seams.


It would need to only create groups based on the smallest enclosed seam space, so this picture would result in 16 groups. Is it even possible? Perhaps this script from JA12 can be a starting point.

import bpy

for obj in bpy.context.selected_objects:
    if(obj.type == 'MESH' and len(obj.vertex_groups) > 0):
        bpy.context.scene.objects.active = obj
        bpy.ops.object.mode_set(mode='EDIT')
        bpy.ops.mesh.select_mode(type='VERT')
        for vgroup in obj.vertex_groups:
            bpy.ops.mesh.select_all(action='DESELECT')
            bpy.ops.object.vertex_group_set_active(group=vgroup.name)
            bpy.ops.object.vertex_group_select()
            bpy.ops.mesh.separate(type='SELECTED')
        bpy.ops.object.mode_set(mode='OBJECT')

You can select faces limited by seams in face selection mode:
bpy.context.tool_settings.mesh_select_mode = (False, False, True)

By using the select linked opreator:
bpy.ops.mesh.select_linked(limit=True)

You could start off with a list like
l = list(range(len(bpy.context.object.data.polygons))

pop a number, select the poly with that index exclusively and call the select_linked operator. Create a vertex group, remove the numbers of all selected polys from list and repeat.

Thanks for the help. I was looking through an outdated API and got stuck for a second.

import bpy

for obj in bpy.context.selected_objects:
    if(obj.type == 'MESH' and len(obj.vertex_groups) < 80):
        bpy.context.scene.objects.active = obj
        bpy.ops.object.mode_set(mode='EDIT')
        bpy.context.tool_settings.mesh_select_mode = (False, False, True)
        bpy.ops.mesh.select_linked(limit=True)
        bpy.ops.object.vertex_group_assign_new()

This is what it looks like updated with as much as I understand. Can anyone show me how to implement the list and number pop? This snippet selects linked faces within the seam boundaries when a single face is already selected and then creates a vertex group.

Standard API would require a mode switch for every selection change, 'cause you need to do that in object mode, but the other ops to select linked in edit mode. Here’s a working script that uses bmesh together with bpy ops to achieve your goal without mode switches:

import bpy
import bmesh

bpy.context.tool_settings.mesh_select_mode = (False, False, True)

for ob in bpy.context.selected_objects:
    if ob.type == 'MESH' and bpy.ops.object.mode_set.poll():
        
        bpy.context.scene.objects.active = ob
        bpy.ops.object.mode_set(mode='EDIT', toggle=False)
        
        me = ob.data
        bm = bmesh.from_edit_mesh(me)
        
        faces = bm.faces[:]
        
        while faces:
            bpy.ops.mesh.select_all(action='DESELECT')
            face = faces.pop()
            face.select_set(True)
            
            bpy.ops.mesh.select_linked(limit=True)
            bpy.ops.object.vertex_group_assign_new()
            
            for f in bm.faces:
                if f.select:
                    try:
                        faces.remove(f)
                    except:
                        pass

        bpy.ops.object.mode_set(mode='OBJECT', toggle=False)

Thank you for the help, this works perfectly.