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

__author__ = 'thoth'

import bpy
import random

random.seed=4262

def whack(x, scale):
    delta = random.random()-0.5

    if (delta<0):
        return x + (delta-0.5)*scale
    else:
        return x + (delta+0.5)*scale

def make_speckle_nodes(nt, x, y, texCoord, time, time_span):
    colorRamp = nt.nodes.new("ShaderNodeValToRGB")
    colorRamp.location = (x-100,y)

    rgb = (random.random(), random.random(), random.random())
    rgb2 = [ whack(x, 0.5) for x in rgb]

    elements = colorRamp.color_ramp.elements
    elements.new(2)
    elements[0].color = (rgb[0], rgb[1], rgb[2], 1)
    elements[0].position=0
    elements[1].color = (rgb2[0], rgb2[1], rgb2[2], 1)
    elements[1].position=0.2
    elements[2].color = (rgb2[0], rgb2[1], rgb2[2], 0)
    elements[2].position=0.3

    voronoi = nt.nodes.new("ShaderNodeGroup")
    voronoi.location = (x-300, y)
    voronoi.node_tree = bpy.data.node_groups['voronoi animate']
    voronoi.inputs[2].default_value = time_span
    voronoi.inputs[3].default_value = random.random()*10+10

    nt.links.new(colorRamp.inputs[0], voronoi.outputs[0])

    add = nt.nodes.new("ShaderNodeVectorMath")
    add.operation = 'ADD'
    add.location = (x-500, y)
    add.inputs[1].default_value = [0,0,y]

    nt.links.new(voronoi.inputs[0], add.outputs[0])
    nt.links.new(voronoi.inputs[1], time)

    nt.links.new(add.inputs[0], texCoord)

    return colorRamp

def random_pebble_material_old():
    mat = bpy.data.materials.new("pebble")

    mat.use_nodes = True

    mat.node_tree.nodes.clear()

    nt = mat.node_tree

    out = nt.nodes.new('ShaderNodeOutputMaterial')
    out.location = (0,0)

    mix = nt.nodes.new('ShaderNodeMixShader')
    mix.location = (-200,0)
    mix.inputs[0].default_value = 0.15

    nt.links.new(out.inputs[0], mix.outputs[0])

    glossy = nt.nodes.new("ShaderNodeBsdfGlossy")
    glossy.location = (-400, 0)
    glossy.inputs['Roughness'].default_value = 0.2

    nt.links.new(mix.inputs[2], glossy.outputs[0])

    diffuse = nt.nodes.new("ShaderNodeBsdfDiffuse")
    diffuse.location = (-400,100)

    nt.links.new(mix.inputs[1], diffuse.outputs[0])

    mix2 = nt.nodes.new('ShaderNodeMixRGB')
    mix2.location = (-600,100)

    nt.links.new(diffuse.inputs[0], mix2.outputs[0])

    texCoord = nt.nodes.new("ShaderNodeTexCoord")
    texCoord.location = (-1500,0)

    speckle1 = make_speckle_nodes(nt, -800, 100, texCoord.outputs['Generated'], None, 1)
    speckle2 = make_speckle_nodes(nt, -800, 300, texCoord.outputs['Generated'], None, 1)

    nt.links.new(mix2.inputs[0], speckle1.outputs[1])
    nt.links.new(mix2.inputs[1], speckle2.outputs[0])
    nt.links.new(mix2.inputs[2], speckle1.outputs[0])

    return mat

def random_pebble_material():

    time_span=0.2
    mat = bpy.data.materials.new("pebble")

    mat.use_nodes = True

    mat.node_tree.nodes.clear()

    nt = mat.node_tree

    out = nt.nodes.new('ShaderNodeOutputMaterial')
    out.location = (0,0)

    mix = nt.nodes.new('ShaderNodeMixShader')
    mix.location = (-200,0)
    mix.inputs[0].default_value = 0.15

    nt.links.new(out.inputs[0], mix.outputs[0])

    glossy = nt.nodes.new("ShaderNodeBsdfGlossy")
    glossy.location = (-400, 0)
    glossy.inputs['Roughness'].default_value = 0.2

    nt.links.new(mix.inputs[2], glossy.outputs[0])

    diffuse = nt.nodes.new("ShaderNodeBsdfDiffuse")
    diffuse.location = (-400,100)

    nt.links.new(mix.inputs[1], diffuse.outputs[0])

    mix2 = nt.nodes.new('ShaderNodeMixRGB')
    mix2.location = (-600,100)
    
    nt.links.new(diffuse.inputs[0], mix2.outputs[0])

    texCoord = nt.nodes.new("ShaderNodeTexCoord")
    texCoord.location = (-1500,0)

    time = nt.nodes.new("ShaderNodeValue")
    time.location = (-1500, -500)
    time.outputs[0].default_value = 0
    nt.keyframe_insert(data_path = 'nodes["Value"].outputs[0].default_value', frame=1)
    time.outputs[0].default_value = time_span
    nt.keyframe_insert(data_path = 'nodes["Value"].outputs[0].default_value', frame=bpy.context.scene.frame_end+1)

    speckle1 = make_speckle_nodes(nt, -800, 100, texCoord.outputs['Generated'], time.outputs[0], time_span)
    speckle2 = make_speckle_nodes(nt, -800, 300, texCoord.outputs['Generated'], time.outputs[0], time_span)

    nt.links.new(mix2.inputs[0], speckle1.outputs[1])
    nt.links.new(mix2.inputs[1], speckle2.outputs[0])
    nt.links.new(mix2.inputs[2], speckle1.outputs[0])

    for fc in nt.animation_data.action.fcurves:
        for kp in fc.keyframe_points:
            kp.interpolation = 'LINEAR'

    return mat

def mission1():
    mat = random_pebble_material()
    obj = bpy.context.active_object
    obj.data.materials[0] = mat

def mission2(scn, cols, rows):
    for u in range(cols):
        for v in range(rows):
            name  = "pebble %d,%d"%(u,v)
            obj = bpy.data.objects.get(name)
            if obj is None:
                obj = bpy.data.objects.new(name, bpy.data.meshes['pebble'])
                scn.objects.link(obj);
            obj.scale = (1,1,0.2)
            obj.location = ((u-cols*0.5+0.5)*3, (v-rows*0.5+0.5)*3, 0)
            obj.material_slots[0].link = 'OBJECT'
            obj.material_slots[0].material = random_pebble_material()

#
#
#

mission2(bpy.context.scene, 12, 7)

Blender python API quick-start

Syntax highlighting by Pygments.