Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

June 27, 2011

Filling a gap between two edges (interpolating between poly lines)

this script performs linear interpolation, with definable number of sections between two polylines. A polyline is a connected chain of edges. This script requires two polylines with the same number of edges to run.

import bpy

# constants
NUM_SECTIONS = 13 # num faces to span.

def generate_main_temp_list(selected_objects):
# take two selected polylines (edge only mesh type)
iterator = 0
main_temp_list = []

# after this for loop we have a 2*n list of verts
for i in selected_objects:
sub_temp_list = []
for vert in i.data.vertices:

# in order to generalize the algorithm - note the length of the polyline
num_verts = len(main_temp_list[0])

# generate main_temp_list (multidimensional, num_sections*n)
iter = 1
for i in range(1,NUM_SECTIONS):
sub_temp_list = []

# vnum is used for going through each vert
for vnum in range(num_verts):
vertn = main_temp_list[0][vnum]
vertx = main_temp_list[len(main_temp_list)-1][vnum]
vert = vertn.lerp(vertx, iter*RATE)

# inserts new section after linear interpolation (lerp)
main_temp_list.insert(iter, sub_temp_list)
iter += 1

return main_temp_list

def generate_straight_list(main_temp_list):
# generate straight_vert_list (1 row of (n*n) elements)
straight_vert_list = []
small_iter = 0
for i in main_temp_list:
for m in i:
small_iter += 1
return straight_vert_list

# wrap faces onto edges all the way down.
def make_edges_from_vertlist(Verts):

takes a multi dimensional list, makes face indices

axis1: The amount of faces per edge as n-1, where n is num_verts
axis2: The amount of faces will be NUM_SECTIONS-1
faces created: is (axis1-1)*(axis2-1)


def list_shift(mylist, n):
tmplist = []
for element in mylist:
return tmplist

Faces = []
axis1, axis2 = len(Verts), len(Verts[0])

print('dimension 1', axis1, "so,", axis1-1, "faces")
print('dimension 2', axis2, "so,", axis2-1, "faces")

# example of format..if 11*11 verts
# Faces = [[0, 11, 12, 1],[9, 20, 21, 10],[11,22,23,12]]

for x in range(axis1-1):
tmpl = [x*axis2, x*axis2+axis2, x*axis2+axis2+1, x*axis2+1]
for z in range(axis2-1):
tlist = list_shift(tmpl, z)

return Faces

# takes care of the verts to polygon issue.
def make_polygon(Verts, Faces, object_name):
object_mesh = object_name + "_mesh"
mesh = bpy.data.meshes.new(object_mesh)
mesh.from_pydata(Verts, [], Faces)
new_object = bpy.data.objects.new(object_name, mesh)
new_object.data = mesh

scene = bpy.context.scene

# main_temp_list: will contain multi dimensional list of verts per section
# straight_vert_list: will contain a single dimensional list of n vertices.
def init_functions(selected_objects):
main_temp_list = generate_main_temp_list(selected_objects)
Faces = make_edges_from_vertlist(main_temp_list)
straight_vert_list = generate_straight_list(main_temp_list)
make_polygon(straight_vert_list, Faces, "combo")


becomes, (this is a wireframe view, the script _does_ create faces too)