Skip to content

Instantly share code, notes, and snippets.

@SabinT
Created May 26, 2015 05:54
Show Gist options
  • Save SabinT/e539b5a8d709a0e012e6 to your computer and use it in GitHub Desktop.
Save SabinT/e539b5a8d709a0e012e6 to your computer and use it in GitHub Desktop.
Generate blends of three compatible meshes and arrange in a triangle.
# A blender script that creates a triangular grid of different blends of three meshes
# of identical topology in a triangle.
#
# 'a', 'b', and 'c' are the meshes to be blended.
# 'n' is the "height" of the triangle
# 'side' is the length of the side of the triangle in which the
# blended meshes will be arranged
def blendingTriangle(scene, a, b, c, n, side):
index = 0;
# the vertices of the positions
X = Vector((0.5 * side,0,0))
Y = Vector((-0.5 * side ,0,0))
Z = Vector((0, 0, sqrt(3) * 0.5 * side)) # the top vertex
av = a.data.vertices
bv = b.data.vertices
cv = c.data.vertices
for i in range(0, n + 1):
for j in range(0, i+1):
u = (n - i) / float(n)
v = (i - j) / float(n)
w = j / float(n)
# duplicate the object
name = a.name + "blend" + str(i) + "_" + str(j)
mesh = bpy.data.meshes.new(name)
ob_new = bpy.data.objects.new(name, mesh)
ob_new.data = a.data.copy()
ob_new.location = u * Z + v * X + w * Y # place object at the vertex
scene.objects.link(ob_new)
ob_new.select = True
index = 0
for vert in ob_new.data.vertices:
# v.co = u * (av[index].co) + v * (bv[index].co) + w * (cv[index].co)
vert.co = (av[index].co * u) + (bv[index].co * v) + (cv[index].co * w)
index = index + 1
#
# Usage example
# cow = D.objects['cow']
# dino = D.objects['dino']
# horse = D.objects['horse']
# blendingTriangle(C.scene, cow, dino, horse, 3, 30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment