I tried to write my own code that changes vertex colors based on the vertex Z-location(global). Which I derived from these links:
How can I get vertex positions from a mesh?
https://blenderartists.org/t/setting-vertex-color-via-python/481060/5
https://blenderartists.org/t/meshvertex-co-vertex-position-issue/670866
The base code looks like this:
from bpy import *
from random import *
bpy.context.scene.update()
#obj = context.active_object.
obj = data.objects["Cube"]
mesh = obj.data
verts = mesh.vertices
faces = mesh.polygons
vertexColour = mesh.vertex_colors[0].data
for v in verts:
vertZloc = ((obj.matrix_world * v.co)[2])
#print(vertZloc) # for sanity check
redMod = (28.3333333 * (vertZloc) - 28.3333333)
grnMod = (-(28.3333333 * (vertZloc)) + 283.3333333)
vColour = [(redMod,grnMod,0) for i in mesh.vertices]
# asign colours to verts
for i in range(len(faces)):
v = vertexColour[i]
f = faces[i].vertices
#print('\n'.join(map(str, faces[i].vertices))) # sanity check
v.color = vColour[f[0]]
v.color = vColour[f[1]]
v.color = vColour[f[2]]
v.color = vColour[f[3]]
When I ran a longer version of my code to change the vertex coloring of 5 'cubes' of different heights, I found that any "between" heights have the same color, when there should be a linear correlation to the height and green/red values.
Why is this? My formulae are both linear plots:
vertZloc = ((obj.matrix_world * v.co)[2])
redMod = (28.3333333 * (vertZloc) - 28.3333333)
grnMod = (-(28.3333333 * (vertZloc)) + 283.3333333)
Stretch Help:
Also as a side note, I do want the all the top-vertex colors to change, not just the two vertices of the top plane, but I have not tried to solve that yet, I have just been focusing on the coloring function.
EDIT:
My full code, in case it is wanted, although it shouldn't explain anything else:
from bpy import *
from random import *
bpy.context.scene.update()
#obj = context.active_object.
obj = data.objects["Cube"]
mesh = obj.data
verts = mesh.vertices
faces = mesh.polygons
vertexColour = mesh.vertex_colors[0].data
for v in verts:
vertZloc = ((obj.matrix_world * v.co)[2])
#print(vertZloc) # for sanity check
redMod = (28.3333333 * (vertZloc) - 28.3333333)
grnMod = (-(28.3333333 * (vertZloc)) + 283.3333333)
vColour = [(redMod,grnMod,0) for i in mesh.vertices]
# asign colours to verts
for i in range(len(faces)):
v = vertexColour[i]
f = faces[i].vertices
#print('\n'.join(map(str, faces[i].vertices))) # sanity check
v.color = vColour[f[0]]
v.color = vColour[f[1]]
v.color = vColour[f[2]]
v.color = vColour[f[3]]
################### CUBE 1
#obj = context.active_object.
obj = data.objects["Cube.001"]
mesh = obj.data
verts = mesh.vertices
faces = mesh.polygons
vertexColour = mesh.vertex_colors[0].data
for v in verts:
vertZloc = ((obj.matrix_world * v.co)[2])
#print(vertZloc) # for sanity check
redMod = (28.3333333 * (vertZloc) - 28.3333333)
grnMod = (-(28.3333333 * (vertZloc)) + 283.3333333)
vColour = [(redMod,grnMod,0) for i in mesh.vertices]
# asign colours to verts
for i in range(len(faces)):
v = vertexColour[i]
f = faces[i].vertices
#print('\n'.join(map(str, faces[i].vertices))) # sanity check
v.color = vColour[f[0]]
v.color = vColour[f[1]]
v.color = vColour[f[2]]
v.color = vColour[f[3]]
################### CUBE 2
#obj = context.active_object.
obj = data.objects["Cube.002"]
mesh = obj.data
verts = mesh.vertices
faces = mesh.polygons
vertexColour = mesh.vertex_colors[0].data
for v in verts:
vertZloc = ((obj.matrix_world * v.co)[2])
#print(vertZloc) # for sanity check
redMod = (28.3333333 * (vertZloc) - 28.3333333)
grnMod = (-(28.3333333 * (vertZloc)) + 283.3333333)
vColour = [(redMod,grnMod,0) for i in mesh.vertices]
# asign colours to verts
for i in range(len(faces)):
v = vertexColour[i]
f = faces[i].vertices
#print('\n'.join(map(str, faces[i].vertices))) # sanity check
v.color = vColour[f[0]]
v.color = vColour[f[1]]
v.color = vColour[f[2]]
v.color = vColour[f[3]]
################### CUBE 3
#obj = context.active_object.
obj = data.objects["Cube.003"]
mesh = obj.data
verts = mesh.vertices
faces = mesh.polygons
vertexColour = mesh.vertex_colors[0].data
for v in verts:
vertZloc = ((obj.matrix_world * v.co)[2])
#print(vertZloc) # for sanity check
redMod = (28.3333333 * (vertZloc) - 28.3333333)
grnMod = (-(28.3333333 * (vertZloc)) + 283.3333333)
vColour = [(redMod,grnMod,0) for i in mesh.vertices]
# asign colours to verts
for i in range(len(faces)):
v = vertexColour[i]
f = faces[i].vertices
#print('\n'.join(map(str, faces[i].vertices))) # sanity check
v.color = vColour[f[0]]
v.color = vColour[f[1]]
v.color = vColour[f[2]]
v.color = vColour[f[3]]
################### CUBE 4
#obj = context.active_object.
obj = data.objects["Cube.004"]
mesh = obj.data
verts = mesh.vertices
faces = mesh.polygons
vertexColour = mesh.vertex_colors[0].data
for v in verts:
vertZloc = ((obj.matrix_world * v.co)[2])
#print(vertZloc) # for sanity check
redMod = (28.3333333 * (vertZloc) - 28.3333333)
grnMod = (-(28.3333333 * (vertZloc)) + 283.3333333)
vColour = [(redMod,grnMod,0) for i in mesh.vertices]
# asign colours to verts
for i in range(len(faces)):
v = vertexColour[i]
f = faces[i].vertices
#print('\n'.join(map(str, faces[i].vertices))) # sanity check
v.color = vColour[f[0]]
v.color = vColour[f[1]]
v.color = vColour[f[2]]
v.color = vColour[f[3]]

