cloning
link-mesh-array.py
link-mesh.py

mesh fabrication
staircase.py
triangle-donut.py
vertexAccumulator.py
randomSquareArray.py
meshFromBathymetry.py
cylinders-from-list-of-radii.py
binary-image-to-mesh.py
sphere-minecraft-schematic.py
spikify.py
add-to-mesh.py
mobius-strip.py
split-copy-mesh.py

fabricating other objects
create-text.py
text-from-file.py
create-camera.py
create-bezier.py
helix-bezier.py

material slots
cube-copy-blue.py
cube-turns-red.py
red-blue-per-object.py

animation and fcurves
csv-to-fcurve-loc-rot.py
csv-to-fcurve.py
pop-in-material.py
spike-wiggle-2.py
spike-wiggle.py
sweep-animate-size.py
animate-cycles-lamp-strength.py

incorporating python libraries
exec-text-library.py
exec-external-python.py
import-python.py

constraints
camera-track-object.py
text-track-camera.py

shape keys
explore-shape-keys.py
shape-key-fin.py
docking-tube.py

animating curve bevel
data-graph.py

drivers
scan-drivers.py
copy-drivers.py
driver-fin.py
driver-multi-chain.py

UV layers
barber-pole.py
expand-uv-to-fit.py
uv-from-geometry-cubic.py
flip-texture-v-coordinate.py

modifiers
hook-modifier-curve.py
rounded-prisms.py
make-tile.py
remove-action-modifiers.py

NLAs
explore-NLAs.py
spinning-frogs.py

video sequence editor (VSE)
create-vse-image-strips.py
slide-show.py
vse-strip-gap.py

images and textures
image-on-mesh.py
image-to-material-node.py
load-image-texture.py
texture-one-cube-face.py
condense-duplicate-images.py

analytic geometry
animate-random-spin.py
camera-cone-exp-2.py
camera-cone-exp.py
compute-circle-center.py
dihedral-angle-from-xy.py
extrude-edge-along-custom-axis.py
orientation-matrix.py
two-spheres.py
bezier-interpolate.py
rotate-to-match.py

node trees
change-diffuse-to-emission-node.py

etc
add-plane-from-selected-vertices.py
adjust-all-materials.py
all-nodes-cycles-materials.py
bit_shift.py
bone-orientation-demo.py
cannonball-packing.py
comb.py
convert-quaternion-keyframes-to-euler.py
copy-location-from-vertex-group.py
create-cycles-material.py
demonstrate-decomposition-instability.py
dump-point-cache.py
dump-screen-layout-info.py
expand-nla-strips.py
explore-edge-bevel-weight.py
find-action-users.py
find-green-rectangle.py
find-new-objects.py
fix-scene-layers.py
generate-makefile.py
link-external-data-blocks.py
list-referenced-files.py
material-readout.py
movie-card-stack.py
movies-on-faces.py
next-file-name.py
object-font-from-regular-font.py
operator-mesh-gridify.py
particle-animator.py
particle_loop.py
pose-match.py
pose-sequence-to-fbx.py
prepare-texture-bake.py
raining-physics.py
random-pebble-material.py
reverse-keyframes.py
scale-parallelogram.py
screenshot-sequence.py
select-objects-in-modifiers.py
select-vertices.py
shift-layers.py
snapshot-keyframes-as-mesh.py
sphere-project-texture.py
squish-mesh-axis.py
subdivide-fcurve.py
thicken-texture.py
transform-selected.py
voronoi-madness.py

import bpy
import bmesh

def vertsForTube(dxArray, dyArray, dzArray):
    z=0
    verts = []
    for i in range(len(dzArray)):
        verts.append( [ dxArray[i], dyArray[i], z] )
        verts.append( [ -dxArray[i], dyArray[i], z] )
        verts.append( [ -dxArray[i], -dyArray[i], z] )
        verts.append( [ dxArray[i], -dyArray[i], z] )
        z = z+dzArray[i]
        verts.append( [ dxArray[i], dyArray[i], z] )
        verts.append( [ -dxArray[i], dyArray[i], z] )
        verts.append( [ -dxArray[i], -dyArray[i], z] )
        verts.append( [ dxArray[i], -dyArray[i], z] )

    return verts

def facesForRings(ring1, ring2):
    faces = []
    n = len(ring1)
    for u in range(n):
        v0 = ring1[ u ]
        v1 = ring1[ (u+1)%n ]
        v2 = ring2[ u ]
        v3 = ring2[ (u+1)%n ]
        faces.append( [v0, v1, v3, v2])
    return faces

def makeBaseMesh(name, dxArray, dyArray, dzArray):
    verts = vertsForTube(dxArray, dyArray, dzArray)
    faces = []
    faces.append( [0,3,2,1] )
    for u in range(len(dzArray)):
        v0 = u*8
        v1 = v0+1
        v2 = v0+2
        v3 = v0+3
        v4 = v0+4
        v5 = v0+5
        v6 = v0+6
        v7 = v0+7
        faces.extend( facesForRings( [ v0,v1,v2,v3] , [v4,v5,v6,v7] ) )
        if (u+1 < len(dzArray)) :
            v8 = v0+8
            v9 = v0+9
            v10 = v0+10
            v11 = v0+11
            faces.extend( facesForRings( [v4,v5,v6,v7], [v8,v9,v10,v11] ) )
        else:
            faces.append( [v4,v5,v6,v7] )

    mesh = bpy.data.meshes.new(name)
    mesh.from_pydata(verts, [], faces)

    mesh.show_normal_face = True

    return mesh

def zsForStage(stage, total):
    rval = []
    for i in range(total):
        if i<stage:
            rval.append(1)
        else:
            rval.append(0.1)
    return rval

def addShapeKeys(obj, dxArray, dyArray):

    n = len(dxArray)
    for s in range(n+1):
        if (s==0):
            obj.shape_key_add("Basis")
        else:
            kn = "stage %d"%s
            obj.shape_key_add(kn)
            if (s>1):
                obj.data.shape_keys.key_blocks[kn].relative_key = obj.data.shape_keys.key_blocks["stage %d"%(s-1)]
            bm = bmesh.new()
            bm.from_mesh(obj.data)
            bm.verts.ensure_lookup_table()
            sl = bm.verts.layers.shape.get(kn)
            newVerts = vertsForTube(dxArray, dyArray, zsForStage(s, n))
            for u in range(len(newVerts)):
                bm.verts[u][sl] = newVerts[u]
            bm.to_mesh(obj.data)
    obj.data.shape_keys.use_relative = True


def fabArmature(name):
    arm = bpy.data.armatures.new(name)
    oa = bpy.data.objects.new(name, arm)

    bpy.context.scene.objects.link(oa)

    bpy.context.scene.objects.active = oa

    bpy.ops.object.mode_set(mode='EDIT')

    bone = arm.edit_bones.new("bone")
    bone.head = (0,0,0)
    # if we don't set the tail!=head, the bone disappears
    bone.tail = (0,0,1)

    bpy.ops.object.mode_set(mode='OBJECT')

    return oa

def addDriver(obj, armature, nSegs):

    extended = 1
    retracted = 0.1

    mesh = obj.data
    for i in range(nSegs):
        data_path = "key_blocks[\"stage %d\"].value" % (i+1)
        dr = mesh.shape_keys.driver_add(data_path)
        dr.driver.type='MIN'
        var = dr.driver.variables.new()
        var.type = 'TRANSFORMS'
        var.targets[0].id = armature
        var.targets[0].bone_target = "bone"
        var.targets[0].transform_type = 'LOC_Z'
        mod = dr.modifiers[0]
        slope = extended/(extended-retracted)
        base = (nSegs-i)*retracted + (i*extended)
        mod.coefficients = [-base*slope, 
                            slope]

dxArray = [ 1, 0.9, 0.8, 0.7, 0.6, 0.5]
dyArray = [ 2, 1.9, 1.8, 1.7, 1.6, 1.5]

dzArray = zsForStage(0, len(dxArray))

name="tube"
mesh = makeBaseMesh( name, dxArray, dyArray, dzArray)

obj = bpy.data.objects.new(name, mesh)
bpy.context.scene.objects.link(obj)

obj.select = True
bpy.context.scene.objects.active = obj

addShapeKeys(obj, dxArray, dyArray)


oa = fabArmature("armature")
oa.select = True

addDriver(obj, oa, len(dxArray))

Blender python API quick-start

Syntax highlighting by Pygments.