UV unwrap each face of an object via python script I have an object with several faces, and I want to loop over the faces, adjust the view for each differently and then UV unwrap the face from the chosen view.

I managed to do it using Blender GUI but how can I automate it via a python script?

This is the code I was trying with... I keep getting either a context error or an error saying that BMFace has been removed :s

import bpy, bmesh
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='DESELECT')
bm = bmesh.from_edit_mesh(bpy.data.objects['my_obj'].data)
for face in bm.faces:
    face.select = True
    if [...] : # Condition chosen to know which face this is
        q = Quaternion([...]) # Set desired corresponding view for this face
    for area in bpy.context.screen.areas:
        if area.type == 'VIEW_3D':
            break
    for region in area.regions:
        if region.type == 'WINDOW':
            break
    space = area.spaces[0]
    context = bpy.context.copy()
    context['area'] = area
    context['region'] = region
    context['space_data'] = space
    r3d = space.region_3d
    r3d.view_perspective = 'ORTHO'
    r3d.view_rotation = q

    for window in bpy.context.window_manager.windows:
        screen = window.screen
        for area in screen.areas:
            if area.type == 'VIEW_3D':
                for space in area.spaces:
                    if space.type == 'VIEW_3D':
                        for region in area.regions:
                            if region.type == 'WINDOW':
                                override = {'window': window, 'screen': screen, 'area': area, 'region': region}
                                bpy.ops.uv.project_from_view(override, orthographic=False, correct_aspect=True, clip_to_bounds=False, scale_to_bounds=True)
    face.select = False


Please help!
Manipulating UV coordinates in python is usually done via the bmesh interface.

I include an example from barber-pole.py

def spiralUVs(mesh, xPlus):
    # add a UV layer called "spiral" and make it slanted.
    mesh.uv_textures.new("spiral")
    bm = bmesh.new()
    bm.from_mesh(mesh)

    bm.faces.ensure_lookup_table()

    uv_layer = bm.loops.layers.uv[0]

    nFaces = len(bm.faces)
    for fi in range(nFaces):
        x0 = fi*2/nFaces
        x1 = (fi+1)*2/nFaces
        bm.faces[fi].loops[0][uv_layer].uv = (x0, 0)
        bm.faces[fi].loops[1][uv_layer].uv = (x1, 0)
        bm.faces[fi].loops[2][uv_layer].uv = (xPlus+x1, 1)
        bm.faces[fi].loops[3][uv_layer].uv = (xPlus+x0, 1)
    bm.to_mesh(mesh)


Some other examples that set UV texture coordinates include


http://web.purplefrog.com/~thoth/blender/python-cookbook/expand-uv-to-fit.html
http://web.purplefrog.com/~thoth/blender/python-cookbook/uv-from-geometry-cubic.html
http://web.purplefrog.com/~thoth/blender/python-cookbook/meshFromBathymetry.html
http://web.purplefrog.com/~thoth/blender/python-cookbook/thicken-texture.html