__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.