Creating a UV Map using python

I have been trying to write a COLLADA DAE importer using python. However I am struggling with the UV mapping. I cannot get the following code snippet to work. I get the error “MeshTexturePoly” object has no attribute “uv1”…

https://wiki.blender.org/index.php/Dev:Py/Scripts/Cookbook/Code_snippets/Materials_and_textures

Does anyone have any ideas what is going wrong? I am using Blender v2.76.

can you at least show which code you are talking about ?

happy bl

Yes, it’s this bit:

#----------------------------------------------------------
# File uvs.py
#----------------------------------------------------------
import bpy
import os
 
def createMesh(origin):
    # Create mesh and object
    me = bpy.data.meshes.new('TetraMesh')
    ob = bpy.data.objects.new('Tetra', me)
    ob.location = origin
    # Link object to scene
    scn = bpy.context.scene
    scn.objects.link(ob)
    scn.objects.active = ob
    scn.update()
 
    # List of verts and faces
    verts = [
        (1.41936, 1.41936, -1), 
        (0.589378, -1.67818, -1), 
        (-1.67818, 0.58938, -1), 
        (0, 0, 1)
    ]
    faces = [(1,0,3), (3,2,1), (3,0,2), (0,1,2)]
    # Create mesh from given verts, edges, faces. Either edges or
    # faces should be [], or you ask for problems
    me.from_pydata(verts, [], faces)
 
    # Update mesh with new data
    me.update(calc_edges=True)
 
    # First texture layer: Main UV texture
    texFaces = [
        [(0.6,0.6), (1,1), (0,1)],
        [(0,1), (0.6,0), (0.6,0.6)],
        [(0,1), (0,0), (0.6,0)],
        [(1,1), (0.6,0.6), (0.6,0)]
    ]
    uvMain = createTextureLayer("UVMain", me, texFaces)
 
    # Second texture layer: Front projection
    texFaces = [
        [(0.732051,0), (1,0), (0.541778,1)],
        [(0.541778,1), (0,0), (0.732051,0)],
        [(0.541778,1), (1,0), (0,0)],
        [(1,0), (0.732051,0), (0,0)]
    ]
    uvFront = createTextureLayer("UVFront", me, texFaces)
 
    # Third texture layer: Smart projection
    bpy.ops.mesh.uv_texture_add()
    uvCyl = me.uv_textures.active
    uvCyl.name = 'UVCyl'
    bpy.ops.object.mode_set(mode='EDIT')
    bpy.ops.uv.cylinder_project()
    bpy.ops.object.mode_set(mode='OBJECT')
 
    # Set Main Layer active
    me.uv_textures["UVMain"].active = True
    me.uv_textures["UVMain"].active_render = True
    me.uv_textures["UVFront"].active_render = False
    me.uv_textures["UVCyl"].active_render = False
 
    return ob
 
def createTextureLayer(name, me, texFaces):
    uvtex = me.uv_textures.new()
    uvtex.name = name
    for n,tf in enumerate(texFaces):
        datum = uvtex.data[n]
        datum.uv1 = tf[0]
        datum.uv2 = tf[1]
        datum.uv3 = tf[2]
    return uvtex
 
def createMaterial():    
    # Create image texture from image. Change here if the snippet 
    # folder is not located in you home directory.
    realpath = os.path.expanduser('~/snippets/textures/color.png')
    tex = bpy.data.textures.new('ColorTex', type = 'IMAGE')
    tex.image = bpy.data.images.load(realpath)
    tex.use_alpha = True
 
    # Create shadeless material and MTex
    mat = bpy.data.materials.new('TexMat')
    mat.use_shadeless = True
    mtex = mat.texture_slots.add()
    mtex.texture = tex
    mtex.texture_coords = 'UV'
    mtex.use_map_color_diffuse = True 
    return mat
 
def run(origin):
    ob = createMesh(origin)
    mat = createMaterial()
    ob.data.materials.append(mat)
    return
 
if __name__ == "__main__":
    run((0,0,0))

The specific part that causes the error is:

def createTextureLayer(name, me, texFaces):
    uvtex = me.uv_textures.new()
    uvtex.name = name
    for n,tf in enumerate(texFaces):
        datum = uvtex.data[n]
        datum.uv1 = tf[0]
        datum.uv2 = tf[1]
        datum.uv3 = tf[2]
    return uvtex

I’ve also been trying a very simple one with the default startup scene:

import bpy

CubeMesh = bpy.data.meshes.get("Cube")

CubeMesh.uv_textures.new()

CubeMesh.uv_textures["UVMap"].name = "Dom2_map"

CubeMaterial = CubeMesh.materials["Material"]

CubeMaterial.texture_slots[0].texture_coords = "UV"

CubeMaterial.texture_slots[0].uv_layer = "Dom2_map"

bpy.data.objects['Cube'].select = True

bpy.ops.object.editmode_toggle()

print(CubeMesh.uv_layers['Dom2_map'].data[0])

I think the uv_layers[“map_name”].data should be an array of coordinate positions, but I can’t work out how to populate it.

There are a lot of options in bpy.ops.uv… but none of them seem to allow me to set individual coordinates.

datum = uvtex.data[loop.index] instead of [n]?

Thanks for the pointer, in a roundabout sort of way it got me to this…

def createTextureLayer(name, me, texFaces):
    uvtex = me.uv_textures.new()
    uvtex.name = name
    for n,tf in enumerate(texFaces):
        datum = uvtex.data[n]
        datum = tf[0],tf[1],tf[2]

… which makes the example code create the UV maps correctly.

If I put this at the end, it also dsiplays the texture correctly.

TetMesh.uv_textures[“UVCyl”].data[0].image = bpy.data.images[“image name”]

Success!

Actually I have realised that the last bit will only display the texture for the first face. It needs to loop through each face, adding the image:

for face in TetMesh.polygons:
    TetMesh.uv_textures["UVCyl"].data[face.index].image = bpy.data.images['image_name']