The UVs and Vcols (vertex Colors) have the same logic of "per vertex in a face" order, flated in a simple sequence.
- You have the polygon vertex indices and vertices list, so it can be deduces from here. Simple and very useful. More so, if u actually generate the mesh data and not read from an object.
- You can access the mesh loops (the very rule of vert per face etc) to read what contains the vert index / uv. This should be done probably with foreach_get/set function that is more efficient.
Let's see var 1, as you actually seam to know these but just miss the relation poly/vert index
(note that I create list of indices of the uv/vcol and verts, for precise coords etc you can further use them to read or write to actual data):
creating perPolygon and perVertex lists of lists
like [[1, 3],[2, 5], [6, 8, 9]..] that correspond to the polygon / vertex list
perPoly = []
perVert = [] # best variable name yet :D
if Object is not None and getattr(Object, 'type', '') == "MESH":
# these are the poly vertex indices (as in the vert list)
polyIndices = [p.vertices for p in Object.data.polygons]
# create an list with empty lists for all verts
perVert = [ [] for i in range(len(Object.data.vertices))]
increment = 0
for poly in polyIndices:
lenP = len(poly)
perPoly.append(i + increment for i in range(lenP))
for i, vi in enumerate(poly): perVert[vi].append(i + increment)
increment += lenP
Note that I have not even read the uv (or vcol) data, if any. So you can use this in the very process of creating your own mesh or where there is no uv/vcol yet.
perVert / perPoly contain indication to positions in the ...uv_layers[0].data
the opposite, creating a list that parallels the uv list but contains the vertex index goes by flattening the polygon indices (polygon.vertices) list
Basically, that is the way the uv list is done..
# explicit
flatVertsLikeUVs = []
for poly in Object.data.polygons:
for i in poly.vertices:
flatVertsLikeUVs.append(i)
or using chain, same stuff..
from itertools import chain
flatVertsLikeUVs = list( chain.from_iterable([p.vertices for p in Object.data.polygons]) )
an example:
uvs = Object.data.uv_layers[0].data
matchingVertIndex = list(chain.from_iterable(polyIndices))
# example, matching list of uv coord and 3dVert coord:
uvs_XY = [i.uv for i in Object.data.uv_layers[0].data]
vertXYZ= [v.co for v in Object.data.vertices]
matchingVertIndex = list(chain.from_iterable([p.vertices for p in Object.data.polygons]))
# and now, the coord to pair with uv coord:
matchingVertsCoord = [vertsXYZ[i] for i in matchingVertIndex]
-
ps:
getting the mesh loop data/ uv layers with another option, but logic is the same:
you may wanna look here for populating the list with fromeach get / set
http://blenderartists.org/forum/showthread.php?391847-Efficient-copying-of-vertex-coords-to-and-from-numpy-arrays&highlight=numpy
you can skip the numpy part and just populate a previously created list, (like the perVert above).