Here is some python code for uv texturing in Blender 2.64.9: It is hardwired as to the verts, faces, uvs, but you could probably figure out how to do it with a For loops instead.
import bpy
vertices = []
vertices.append([-2.274060,-1.122663,4.727275])
vertices.append([-2.238834,-1.085469,4.957502])
vertices.append([-2.236941,-1.128021,4.961401])
vertices.append([-1.910057,-1.124442,4.727501])
vertices.append([-2.278937,-1.100917,4.720205])
vertices.append([-2.273533,-1.126146,4.728076])
faces = []
faces.append([0,1,2])
faces.append([3,4,0])
uvs = []
uvs.append([0.839357,0.230592])
uvs.append([0.839559,0.231844])
uvs.append([0.839327,0.231865])
uvs.append([0.839347,0.230594])
uvs.append([0.839475,0.230554])
uvs.append([0.839338,0.230597])
texture = "/Users/paulcoones/UNPODDED/art/2_wicked.tif"
# Create mesh and object
me = bpy.data.meshes.new("box")
ob = bpy.data.objects.new("box", me)
# Link object to scene
scn = bpy.context.scene
scn.objects.link(ob)
scn.objects.active = ob
scn.update()
me.from_pydata(vertices, [], faces)
bpy.ops.mesh.uv_texture_add()
uvtex = me.uv_textures[-1]
uvtex.name = 'UVLayer'
uvLayer = me.uv_layers[-1]
uvLayer.data[0].uv = uvs[0]
uvLayer.data[1].uv = uvs[1]
uvLayer.data[2].uv = uvs[2]
uvLayer.data[3].uv = uvs[3]
uvLayer.data[4].uv = uvs[4]
uvLayer.data[5].uv = uvs[5]
for i in range(0,(len(uvs))):
print("50 - uv:",i ,uvLayer.data[i].uv)
tex = bpy.data.textures.new('ColorTex', type = 'IMAGE')
tex.image = bpy.data.images.load(texture)
tex.use_alpha = True
# Create shadeless material and MTex
mat = bpy.data.materials.new("box")
mat.use_shadeless = True
mtex = mat.texture_slots.add()
mtex.texture = tex
mtex.texture_coords = 'UV'
mtex.use_map_color_diffuse = True
# add material to object
ob.data.materials.append(mat)
me.validate()
me.update(calc_edges=True)
# You may have to view in 3D window as User Persp (Local) because mesh is so small.
bpy.ops.object.editmode_toggle()
bpy.ops.mesh.faces_shade_smooth()
# Rotate 180 degrees around z axis because texture applied on back of face.
bpy.ops.transform.rotate(value=-3.14159, axis=(0, 0, 1), constraint_axis=(False, False, True), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1, snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0), snap_align=False, snap_normal=(0, 0, 0), release_confirm=False)
bpy.ops.object.editmode_toggle()
# forced view mode with textures
bpy.context.scene.game_settings.material_mode = 'GLSL'
areas = bpy.context.screen.areas
for area in areas:
if area.type == 'VIEW_3D':
area.spaces.active.viewport_shade='TEXTURED'
area.spaces.active.show_textured_solid = True
print("test done.")
Terminal print out:
50 - uv: 0 <Vector (0.8394, 0.2306)>
50 - uv: 1 <Vector (0.8396, 0.2318)>
50 - uv: 2 <Vector (0.8393, 0.2319)>
50 - uv: 3 <Vector (0.8393, 0.2306)>
50 - uv: 4 <Vector (0.8395, 0.2306)>
50 - uv: 5 <Vector (0.8393, 0.2306)>
test done.