BPY UV-unwrap selected faces

Hello,

I was wondering if could select a few faces from a model and unwrap them to a part of an image? (The model is made within python using verts and faces)

I always used BGE because i only made games so i am new to BPY.

This is what i currenlty have (Removed some extra / failed stuff):


import bpy

barrel_metal_verts = [(0, 1, 0),(0, 1, 3),(0.75, 0.75, 0),(0.75, 0.75, 3),(1, 0, 0),(1, 0, 3),(0.75, -0.75, 0),(0.75, -0.75, 3),(0, -1, 0),(0, -1, 3),(-0.75, -0.75, 0),(-0.75, -0.75, 3),(-1, 0, 0),(-1, 0, 3),(-0.75, 0.75, 0),(-0.75, 0.75, 3)]

barrel_metal_faces = [(0, 2, 4, 6, 8, 10, 12, 14),(15, 13, 11, 9, 7, 5, 3, 1),(0, 1, 3, 2),(2, 3, 5, 4),(4, 5, 7, 6),(6, 7, 9, 8),(8, 9, 11, 10),(10, 11, 13, 12),(12, 13, 15, 14),(14, 15, 1, 0)]

mesh = bpy.data.meshes.new("Barrel")
obj = bpy.data.objects.new("Barrel", mesh)

obj.location = bpy.context.scene.cursor_location
bpy.context.scene.objects.link(obj)
mesh.from_pydata(barrel_metal_verts, [], barrel_metal_faces)
mesh.update(calc_edges = True)


#Material stuff
if bpy.data.materials.get("Barrel") == None:
    mat = bpy.data.materials.new("Barrel")
else:
    mat = bpy.data.materials.get("Barrel")
mat.diffuse_color = (0.41961, 0.24314, 0.05490)
mesh.materials.append(mat)


No one? Really :frowning:

Come on :frowning:

Thanks @cmomoney,

This is what i got now:
I changed the image to fit this.
(If there is a better way, feel free to post it here :slight_smile: )


import bpy
for obj in bpy.data.objects:
  if(obj.type == "MESH" and obj.name == "BarrelWood"):
      bpy.context.scene.objects.active = obj
      bpy.ops.mesh.uv_texture_add()
      bpy.ops.object.editmode_toggle()
      bpy.ops.uv.smart_project(angle_limit=66, island_margin=0)
      bpy.ops.object.editmode_toggle()