How can animation nodes drive vertex colors on an object?
Details
I have a time varying dataset the defines the color for each vertex in a model. I'd like to use animation nodes to drive the colors of the vertices.
The best solution found so far requires a very complex setup. It requires defining vertex groups and assigning colors to the vertex groups. This could require assigning a larger number of vertex groups. The preferred solution is to work directly with the vertices.
- Add the object.
- Assign vertex groups. These are used to get access to the vertices.
- Assign a texture to the object using vertex colors.
- Setup animation nodes using a script to assign colors to the vertex groups. In this example, instead of colors, a temperature is used, and converted to a color.
- A script is used to assign the list of values passed to colors for the vertex groups.
def fraction(x,idx,n):
# on a domain of 0,1 with n center points and
center = idx/(n-1)
dist_2_center = center-x
portion = 1-abs(dist_2_center*(n-1))
fract = max(0,portion)
return fract
def find_color(x,colors):
n = len(colors)
f = [fraction(x,idx,n) for idx in range(n)]
c = [0,0,0,0]
for ii,ff in enumerate(f):
for jj,cc in enumerate(c):
c[jj] += ff*colors[ii][jj]
return c
def t_color(t):
colors = [ (0,0,0,1),(0,0,1,1),(0,1,1,1),(0,1,0,1),(1,1,0,1),(1,0,0,1),(1,1,1,1)]
c = find_color(t,colors)
return c
####################################################################
import bmesh
o = Object # passed into node
colors = [t_color(t) for t in Temps] # passed into node
#print(colors)
#########################
mesh = o.data # get the mesh
bm = bmesh.new() # place holder for working bmesh
bm.from_mesh(mesh) # create a copy of the mesh
#build list of vertex indices in each group
vert_group_indices = []
for vg in o.vertex_groups:
# check if vertex is in this vertext group using indexes
vert_indices = [v.index for v in o.data.vertices if vg.index in [ vv.group for vv in v.groups]]
vert_group_indices += [vert_indices]
# walk list of vertex groups, assign colors based on vertext groups
#colors = [(0,0,0,1),(1,0,0,1),(0,1,0,1),(0,0,1,1)]
color_layer = bm.loops.layers.color[0] # use first color layer
for vg_idx,vert_indices in enumerate(vert_group_indices):
c = colors[vg_idx]
for face in bm.faces:
for loop in face.loops:
if loop.vert.index in vert_indices:
loop[color_layer] = c
bm.to_mesh(mesh) # put mesh in the object
- The result is an object with vertex group assigned colors that can be animated.
Is there any easier way? Is there a way to assign colors to vertices without using vertex groups when using animation nodes? Is there anything in animation nodes that makes this easier?