1
$\begingroup$

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.

  1. Add the object.
  2. Assign vertex groups. These are used to get access to the vertices.

enter image description here

  1. Assign a texture to the object using vertex colors.

enter image description here

  1. 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.

enter image description here

  1. 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
  1. The result is an object with vertex group assigned colors that can be animated.

enter image description here

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?

A sample file with this setup from Blender 2.83 is here :

$\endgroup$

1 Answer 1

4
$\begingroup$

Animation nodes have Set Vertex Color node which can assign colors to vertices, loops or polygons.

enter image description here

$\endgroup$
2
  • $\begingroup$ I am glad to see this node got enhanced ! Previously you could only set a single color for each and every vertex in the object mesh $\endgroup$
    – Gorgious
    Jun 15, 2020 at 19:17
  • $\begingroup$ @Gorgious Yeah that feature was added recently. $\endgroup$ Jun 17, 2020 at 7:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .