Add uv_layer to mesh, add uv-coords, with python I am trying to create a mesh via a Python script and set a texture for it.

My code so far:

def add_mesh(self, coords, uvs, faces, texture, texHeight, texWidth):
  me = bpy.data.meshes.new("myMesh")
  ob = bpy.data.objects.new("myObject", me)

  me.from_pydata(parsedCoords, [], parsedFaces)
  me.update(calc_edges=True)

  mat = self._make_material("myMaterial", (1,1,1), (1,1,1), 1)
  tex = self._make_texture("myTexture", texture, texWidth, texHeight)
  mTex = mat.texture_slots.add()
  mTex.texture_coods = 'UV'
  mTex.use_map_color_diffuse = True
  mTex.diffuse_color_factor = 1.0
  mTex.blend_type = 'MULTIPLY'

  # Missing: Set the uv-coords for the mesh

  me.materials.append(mat)


I use _make_material and _make_texture which are self-defined. But I think they work.

For the missing part I found using the API-documentation following

uv_layer = me.uv_layers.active.data
for poly in me.polygons:
    for loop_ind in range(poly.loop_start, poly.loop_start+ poly.loop_total):
        vInd = me.loops[loop_index].vertex_index
        uv_layer[loop_index].uv = tuple( uvs[vertex_index] )


The passed uvs is a list uv-coordinates as lists. They are in the same order as the vertices.

My problem now is that the uv_layer = me.uv_layers.active.data does not work. It results in AttributeError: 'NoneType' object has no attribute 'data' 

I think my mesh has no uv_layer. How do I add one?
Is my code correct if I add a uv_layer to the mesh? I am really unsure about it.

Thank you for your help!

Greetings,
Dragonseel
As Dragonseel mentioned: the bmesh is important.  http://web.purplefrog.com/~thoth/blender/python-cookbook/barber-pole.html contains a spiralUVs function which assigns UV coordinates to the faces of a cylinder.  It demonstrates the mesh.uv_textures.new(name) function for creating a new UV layer.

Here is the function from that page:

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)

    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)