Skip to content

Instantly share code, notes, and snippets.

@sambler
Last active August 29, 2015 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sambler/273055c9a36eace8d384 to your computer and use it in GitHub Desktop.
Save sambler/273055c9a36eace8d384 to your computer and use it in GitHub Desktop.
Generate Data visualisation in blender
# created in response to
# http://blender.stackexchange.com/q/26495/935
import bpy
from operator import itemgetter
# mainpoints is an array of data to use
# to fill it in - place an empty at the location you want a red sphere
# STRICT - name the empty in the format data-frame-parts
# eg data-1-8 will show a sphere on frame 1 and emit 8 particles
mainpoints = []
for o in bpy.data.objects:
if o.name.startswith('data-'):
di = o.name.split('-')
mpi = [int(di[1]),int(di[2]),o.location]
mainpoints.append(mpi)
# we want the list sorted by frame so we can get the next point
# to stretch the curve to
mainpoints.sort(key=itemgetter(0))
scene = bpy.context.scene
# create these two objects before running script
pointObj = bpy.data.objects['Sphere']
particleObj = bpy.data.objects['Cube']
# options
particleLifetime = 500
particleSize = 0.75
particleDamping = 0.062
particleLineCount = 150
conSize = 0.05
conRes = 3
for i in range(len(mainpoints)):
pdata = mainpoints[i]
# start hidden one frame before
dp = bpy.data.objects.new('DataPoint', pointObj.data)
bpy.context.scene.objects.link(dp)
dp.scale = pointObj.scale
dp.location = pdata[2]
dp.keyframe_insert('location', frame=pdata[0]-1)
dp.hide_render=True
dp.keyframe_insert('hide_render', frame=pdata[0]-1)
dp.hide=True
dp.keyframe_insert('hide', frame=pdata[0]-1)
# now keyframe visibile
dp.hide_render=False
dp.keyframe_insert('hide_render', frame=pdata[0])
dp.hide=False
dp.keyframe_insert('hide', frame=pdata[0])
# create cube particles
ps = dp.modifiers.new(name='particles',type='PARTICLE_SYSTEM')
pset = ps.particle_system.settings
pset.count = pdata[1]
pset.frame_start = pdata[0]
pset.frame_end = pdata[0]
pset.lifetime = particleLifetime
pset.render_type = 'OBJECT'
pset.dupli_object = particleObj
pset.particle_size = particleSize
pset.use_scale_dupli = True
pset.effector_weights.gravity = 0.0
pset.damping = particleDamping
pset.factor_random = 3.8
# create line particles
ps = dp.modifiers.new(name='particles',type='PARTICLE_SYSTEM')
pset = ps.particle_system.settings
pset.count = pdata[1]
pset.frame_start = pdata[0]
pset.frame_end = pdata[0]
pset.lifetime = particleLifetime
pset.render_type = 'LINE'
pset.trail_count = particleLineCount
pset.effector_weights.gravity = 0.0
pset.damping = particleDamping
pset.use_absolute_path_time = True
pset.line_length_tail = .077
pset.path_end = mainpoints[-1][0]+80
pset.factor_random = 3.8
pset.tangent_phase = 1.0
if i < len(mainpoints)-1:
# create a curve to join them
jd = bpy.data.curves.new(name='curve',type='CURVE')
jd.bevel_depth = conSize
jd.bevel_resolution = conRes
jd.dimensions = '3D'
jd.fill_mode = 'FULL'
jd.materials.append(material=particleObj.material_slots[0].material)
jo = bpy.data.objects.new('joiner', jd)
bpy.context.scene.objects.link(jo)
spl = jd.splines.new(type='BEZIER')
spl.bezier_points.add()
p1 = spl.bezier_points[0]
p1.handle_left_type = 'VECTOR'
p1.handle_right_type = 'VECTOR'
p2 = spl.bezier_points[1]
p2.handle_left_type = 'VECTOR'
p2.handle_right_type = 'VECTOR'
# create hook modifier
scene.objects.active = jo
bpy.ops.object.mode_set(mode="EDIT")
p = jd.splines[0].bezier_points
p[0].select_control_point = False
p[1].select_control_point = True
hm1 = jo.modifiers.new(name='Hook1',type='HOOK')
bpy.ops.object.hook_assign()
bpy.ops.object.mode_set(mode="OBJECT")
# keyframe visibility
jo.hide_render=True
jo.keyframe_insert('hide_render', frame=pdata[0]-1)
jo.hide=True
jo.keyframe_insert('hide', frame=pdata[0]-1)
jo.location = pdata[2]
jo.keyframe_insert('location', frame=pdata[0]-1)
jo.hide_render=False
jo.keyframe_insert('hide_render', frame=pdata[0])
jo.hide=False
jo.keyframe_insert('hide', frame=pdata[0])
# add hooks to stretch the curve
h1 = bpy.data.objects.new('Hook', None)
bpy.context.scene.objects.link(h1)
# start at this points location
h1.location = pdata[2]
h1.keyframe_insert('location', frame=pdata[0]-1)
# then moves to next points location
h1.location = mainpoints[i+1][2]
h1.keyframe_insert('location', frame=mainpoints[i+1][0])
hm1.object = h1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment