I am trying to create a Script that does a Smart UV unwrap of seperate connected parts of a mesh This is what I got so far. Note that I have a counter which I check with 10 while I'm testing, so it doesn't pass through everything, since it takes quite a while.

The reason for doing this is that if I select all and smart unwrap, I get a sub optimal result compared to the result I get when selecting each connected part and smart unwrap those parts separately, and then finally at the end, re-pack everything.

I am using this for light UV maps in UE4

Question 1) Seems the select-linked part doesn't work. What am I missing?

Question 2) Seems nothing gets unwrapped (is probably related to Q1)

def getNextVisibleVertex():
    bpy.ops.object.mode_set(mode="EDIT")
    bpy.ops.mesh.select_all(action="DESELECT")
    bpy.context.tool_settings.mesh_select_mode = (True , False , False)
    mesh=bmesh.from_edit_mesh(bpy.context.object.data)

    if hasattr(mesh.verts, "ensure_lookup_table"): 
        mesh.verts.ensure_lookup_table()

    for vert in mesh.verts:
        if (not vert.hide):
            vert.select = True

            # Select linked
            bm = bmesh.from_edit_mesh(bpy.context.object.data)
            for e in vert.link_edges: 
                e.select = True
            return True
    return False

def execute(self, context):
    obj = context.object
    data = obj.data
    i = 0

    found = getNextVisibleVertex()

    while (found and i &lt; 10):
        bpy.ops.uv.smart_project()
        bpy.data.objects[name].select = False
        # Hide selection so we don't pickup same 1st vertex next time
        bpy.ops.mesh.hide(unselected = False)
        i = i + 1

        found = getNextVisibleVertex()

    return {'FINISHED'}
I finally got something that works, it's slow as molasses, but works.

    def execute(self, context):     # execute() is called by blender when running the operator

        while (hasMoreVerts()):
            bpy.ops.uv.smart_project()
            bpy.ops.mesh.hide(unselected = False)
            bpy.ops.object.mode_set( mode = 'OBJECT' )

        return {'FINISHED'}         # this lets blender know that the operator finished successfully

def register():
    bpy.utils.register_class(LightMapUVUnwrapper)

def unregister():
    bpy.utils.unregister_class(LightMapUVUnwrapper)

if __name__ == "__main__":
    register()

def hasMoreVerts():
    bpy.ops.object.mode_set(mode="EDIT")
    bpy.ops.mesh.select_all(action="DESELECT")
    bpy.context.tool_settings.mesh_select_mode = (True , False , False)
    mesh=bmesh.from_edit_mesh(bpy.context.object.data)

    if hasattr(mesh.verts, "ensure_lookup_table"): 
        mesh.verts.ensure_lookup_table()

    for vert in mesh.verts:
        if (not vert.hide):
            vert.select = True
            bpy.ops.mesh.select_linked()
            return True

    return false