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 random
import bmesh
import math
from mathutils import *

def centroid(coords):

    sum = []
    sum.extend(coords[0])
    n = len(coords)
    for i in range(1, n):
        for j in range(len(sum)):
            sum[j] += coords[i][j]
    return [ x/ n for x in sum]
    
def bridgeRings(top, bottom):
    """
    :param top: a list of vertex indices for the top ring
    :param bottom: a list of verex indices for the bottom ring
    :return: a list of faces bridging the two rings
    """

    rval  = []
    n = len(top)
    for i in range(n):
        v1 = top[i]
        v2 = top[ (i+1)%n]
        v3 = bottom[i]
        v4 = bottom[ (i+1)%n]
        rval.append( [ v1, v3, v4, v2] )
    return rval


def make_prism(name, z1, z2, coords, zBevel, insetFactor):
    center = centroid(coords)
    ring1 = [[c[0] - center[0], c[1] - center[1], z1] for c in coords]
    ring2 = [[c[0] - center[0], c[1] - center[1], z2] for c in coords]
    ring2Low = [[c[0] - center[0], c[1] - center[1], z2 - zBevel] for c in coords]
    ring2inset = [[insetFactor * (c[0] - center[0]), insetFactor * (c[1] - center[1]), z2] for c in coords]
    verts = []
    verts.extend(ring1)
    verts.extend(ring2Low)
    verts.extend(ring2)
    verts.extend(ring2inset)
    faces = []
    ringList = [
        [i + base for i in range(len(coords))] for base in range(0, 4 * len(coords), len(coords))
    ]
    # bottom = [ i for i in range(len(coords)) ]
    faces.append(list(reversed(ringList[-1])))
    #top = [ len(coords)*3 + i for i in range(len(coords)) ]
    faces.append(ringList[0])
    for i in range(1, len(ringList)):
        faces.extend(bridgeRings(ringList[i - 1], ringList[i]))
    mesh = bpy.data.meshes.new(name)
    #print(verts)
    #print(faces)
    mesh.from_pydata(verts, [], faces)
    #
    for face in mesh.polygons:
        face.use_smooth = True

    #
    obj = bpy.data.objects.new(name, mesh)
    obj.location = [center[0], center[1], 0]
    return obj


def interp_vvw(a, b, cornerBevel):
    return Vector(a) * (1 - cornerBevel) + Vector(b) * cornerBevel


def make_prism_beveled(name, z1, z2, coords, cornerBevel=0.1, zBevel=0.3, insetFactor=0.7):
    c2 = []
    for i in range(len(coords)):
        a = coords[i]
        b = coords[ (i+1)%len(coords)]

        c2.append(a)
        c2.append( interp_vvw(a, b, cornerBevel) )
        c2.append( interp_vvw(a,b, (1-cornerBevel)))

    obj = make_prism(name, z1, z2, c2, zBevel, insetFactor)

    obj.modifiers.new("subdiv", 'SUBSURF')

    return obj


def make_prism_sharp(name, z1,z2, coords, zBevel = 0.3, insetFactor = 0.7):
    obj = make_prism(name, z1, z2, coords, zBevel, insetFactor)

    #

    obj.modifiers.new("subdiv", 'SUBSURF')

    bm = bmesh.new()
    bm.from_mesh(obj.data)

    cr = bm.edges.layers.crease.active
    if cr is None:
        cr = bm.edges.layers.crease.new()

    creaseRings = set([1,2])
    for e in bm.edges:
        ringa = [ math.floor(v.index/len(coords)) for v in e.verts ]
        if ( ringa[0] != ringa[1] and (ringa[0] in creaseRings or ringa[1] in creaseRings) ):
            e[cr] = 1

    bm.to_mesh(obj.data)

    #

    obj.modifiers.new("edgesplit", 'EDGE_SPLIT')

    return obj

def prismMaterials():
    rval = []
    for mat in bpy.data.materials:
        if (mat.name[:5] == "stick"):
            rval.append(mat)
    return rval

def robinson(v1, v2, v3):
    obj = make_prism_sharp("robinson", -4, random.uniform(0,1), [v1,v2,v3], 0.1)
    scn.objects.link(obj)
    obj.layers = [i in set([ 2, 5]) for i in range(20)]

def robinsonq(v1, v2, v3, v4, acute):
    obj = make_prism_beveled("robinson", -4, random.uniform(0.5,1), [v1,v2,v3, v4], 0.04)
    scn.objects.link(obj)
    obj.layers = [i in set([ 2, 5]) for i in range(20)]
    if (acute):
        mat = bpy.data.materials.get("stick 1")
    else:
        mat = bpy.data.materials.get("stick 2")
    obj.data.materials.append(mat)

def robinsons():

    robinson([1.6653345369377348E-16,0.0], [0.8620385149429254,-0.5068427751827931], [0.39948909991113335,-0.9167379445905968])
    robinson([0.39948909991113335,-0.9167379445905968], [-0.3489315750866878,-1.5799622605168193], [1.6653345369377348E-16,0.0])
    robinson([1.6653345369377348E-16,0.0], [0.8620385149429254,-0.5068427751827931], [0.21565157315160932,0.97647037794151])
    robinson([-0.7796669437263946,0.8798212705489941], [-0.9953185168780039,-0.09664910739251598], [0.21565157315160932,0.97647037794151])
    robinson([1.6653345369377348E-16,0.0], [-0.9953185168780039,-0.09664910739251598], [-0.7484206749978211,-0.6632243159262224])
    robinson([-0.7484206749978211,-0.6632243159262224], [-0.3489315750866878,-1.5799622605168193], [1.6653345369377348E-16,0.0])
    robinson([1.6653345369377348E-16,0.0], [-0.9953185168780039,-0.09664910739251598], [0.21565157315160932,0.97647037794151])

def squares():
    global colors, u, v, obj, ci, i
    colors = prismMaterials()
    for u in range(w):
        for v in range(w):
            obj = make_prism_sharp("stick", -4, random.uniform(0, 2), [[u, v], [u + 1, v], [u + 1, v + 1], [u, v + 1]])
            scn.objects.link(obj)
            ci = random.randint(0, len(colors) - 1)
            print(ci)
            obj.data.materials.append(colors[ci])
            obj.layers = [i in set([0, 3, 5]) for i in range(20)]


#
#

scn = bpy.context.scene
w=20

            # obj = makePrism("bacon", 0, 2, [ [0,0], [1,0], [0.5, 0.7]])
            #scn.objects.link(obj)


#squares()
#robinsons()

Blender python API quick-start

Syntax highlighting by Pygments.