Skip to content

Instantly share code, notes, and snippets.

@zeffii
Last active March 30, 2022 22:36
Show Gist options
  • Save zeffii/6544504 to your computer and use it in GitHub Desktop.
Save zeffii/6544504 to your computer and use it in GitHub Desktop.
import bpy
from mathutils import Vector
# start in object mode
obj = bpy.data.objects["Plane.001"]
mesh = obj.data
mesh.vertex_colors.new()
color_layer = mesh.vertex_colors.active
#color_layer = mesh.vertex_colors["Col"]
# first scan through to figure out min and max stretch
verts = mesh.vertices
polys = []
for poly in mesh.polygons:
face_median = Vector()
for idx in poly.vertices:
face_median += verts[idx].co
face_median *= 1 / poly.loop_total
qdist = lambda idx: (verts[idx].co - face_median).length
stretch = sum([qdist(idx) for idx in poly.vertices])
stretch /= poly.loop_total
polys.append(stretch)
# now colour them as a function of their relative stretch
min_val = min(polys)
max_dif = max(polys) - min(polys)
i = 0
for g, poly in enumerate(mesh.polygons):
for idx in poly.loop_indices:
c = (polys[g] - min_val) / max_dif
color_layer.data[i].color = (c,c,c)
i += 1
## set to vertex paint mode to see the result
bpy.ops.object.mode_set(mode='VERTEX_PAINT')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment