Vertices—loop indices vs. unique indices I need to set the colors of vertices from data generated by third-party simulation. To do this, I need to access the vertices by their unique indices with the Python API. 

I can do this with bpy.data.objects[].data.vertices[], but there doesn't seem to be a color attribute there that I can set. I can access (and maybe set) vertex colors with bpy.data.objects[].data.vertex_indices[].color, but it seems that vertex_indices gives me the loop indices (which are not unique, as neighboring edge loops will generally share at least one vertex).

Can someone clarify how to do this (access vertices by unique index and set color)?

Many thanks in advance!
In Blender, Vertex Color maps don't assign a single color to each vertex but instead assign a color to each vertex of each polygon, as described in this answer by zeffii.

As you noted, loop indices are not unique. So if you want to assign a color the vertices by their unique index you would have to create a mapping of vertex indices to loop indices, i.e. for each polygon, map poly.vertices to poly.loop_indices:

import bpy
from collections import defaultdict

obj = bpy.data.objects['Cube']
col = obj.data.vertex_colors['Col']
polygons = obj.data.polygons

vertex_map = defaultdict(list)
for poly in polygons:
    for v_ix, l_ix in zip(poly.vertices, poly.loop_indices):
        vertex_map[v_ix].append(l_ix)


Once this is done, vertex_map will be a dictionary with the unique vertex indices as keys and lists of loop indices as the values. E.g. something like this for the default cube:

{ 0: [0, 8, 21],
  1: [1, 11, 12],
  2: [2, 15, 16], ... etc.
}


To assign the colors, you can now loop over the dictionary and set the color for each loop index of each vertex:

# color vertices 0 to 3 in red, all other vertices white
for v_ix, l_ixs in vertex_map.items():
    for l_ix in l_ixs:
        if v_ix in [0, 1, 2, 3]:
            col.data[l_ix].color = (1, 0, 0)
        else:
            col.data[l_ix].color = (1, 1, 1)