# I need to find out how to add an UV map to a newly made face via a script

This code creates a simple mesh (with two polygons instead of just one), but I am unable to find out how to give each vertex in the mesh a proper and precise place within the UV map. The idea is to create a quad polygon, give it a place within the UV map. Then when all the polygon faces are completed all vertices(within the exact same coordinates) get merged into one single mesh

Any ideas?

Thanx in advance! sorry if the question is too simple for this forum ...

## create mesh
import bpy

## CREATE MESH
verts = []
edges = []
faces = []

verts.append([
0.0, # x
10.0, # y
0.0, # z
])

verts.append([
10.0, # x
0.0, # y
0.0, # z
])

verts.append([
0.0, # x
0.0, # y
10.0, # z
])

verts.append([
-10.0, # x
0.0, # y
0.0, # z
])

edges.append([0,1])
edges.append([1,2])
edges.append([0,2])
edges.append([2,3])

faces.append([0,1,2])
faces.append([2,0,3])

##########
name = "New Object"
mesh = bpy.data.meshes.new(name)
obj = bpy.data.objects.new(name, mesh)
col = bpy.data.collections.get("Collection")
bpy.context.view_layer.objects.active = obj
mesh.from_pydata(verts, edges, faces)

• Just a small, friendly tip on code style: we all know the order of coordinates, no need for the # x, # y, # z comments. Also for simple values I would write them in one line. Then you can also instantiate entire verts list in one go, using space padding for readability. Also, using bmesh you don't need to specify edges - examples Sep 21 at 14:42
• You don't need edges for from_pydata either (just pass [] for the edge list). Sep 21 at 15:04
• Thx for all the tips! Sep 23 at 6:31

You need one UV coordinate for every corner of a polygon. Fill in an array with your UVs with something like:

uvs = []

# Face 0
faces.append([0,1,2])
uvs += [
0.0, 0.0,   # UV for first corner (vertex 0)
1.0, 0.0,   # UV for second corner (vertex 1)
1.0, 1.0,   # UV for third corner (vertex 2)
]

# Face 1
faces.append([2, 0, 3])
uvs += [
0.0, 0.0,   # UV for first corner (vertex 2)
1.0, 0.0,   # UV for second corner (vertex 0)
1.0, 1.0,   # UV for third corner (vertex 3)
]


Then after from_pydata create the UVMap like this:

# Create a new UVMap
uv_layer = mesh.uv_layers.new(name="UVMap")