Skip to content

Instantly share code, notes, and snippets.

@kutu
Last active March 4, 2019 07:44
Show Gist options
  • Save kutu/6434212 to your computer and use it in GitHub Desktop.
Save kutu/6434212 to your computer and use it in GitHub Desktop.
source code for 3D Audio Visualization https://www.youtube.com/watch?v=c1I0XjSaVmg
import bpy
import math
nums = 18
min = 20
max = 8000
min_log = math.log10(min)
max_log = math.log10(max)
step = (max_log - min_log) / nums
mp3 = "D:\\projects\\blender\\visualizer\\inthesun.mp3"
#mp3 = "D:\\projects\\blender\\visualizer\\test.mp3"
group_name = "cubes"
if group_name in bpy.data.groups:
if len(bpy.data.groups[group_name].objects):
bpy.ops.object.select_name(name=bpy.data.groups[group_name].objects[0].name)
bpy.ops.object.select_grouped(type='GROUP')
bpy.ops.object.delete()
else:
bpy.data.groups.new(group_name)
bpy.context.scene.frame_current = 1
bpy.context.area.type = 'GRAPH_EDITOR'
for i in range(nums):
bpy.ops.mesh.primitive_cube_add(location=(i + .5, -.5, 5))
active = bpy.context.active_object
active.dimensions = (1, 1, 10)
active.name="cube%.2d" % i
bpy.ops.object.transform_apply(scale=True)
bpy.context.scene.cursor_location = bpy.context.active_object.location
bpy.context.scene.cursor_location.z = 0
bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
bpy.ops.object.group_link(group=group_name)
active.keyframe_insert("scale", index=2)
action = active.animation_data.action
low = math.pow(10, min_log + i * step)
high = math.pow(10, min_log + (i + 1) * step)
bpy.ops.graph.sound_bake(filepath=mp3, low=low, high=high)
action.fcurves[0].lock = True
bpy.context.area.type = 'TEXT_EDITOR'
import bpy
group_name = "cubes"
cubes = bpy.data.groups[group_name].objects
nums = len(cubes)
start = bpy.context.scene.frame_start
end = bpy.context.scene.frame_end + 1
#start = 5275
#end = 5276
colors = [
(0, 0, 1),
(0, 0, 0.75),
(0, 0.50, 0.50),
(0, 1, 0.25),
(0, 0.66, 0),
(0.25, 0.33, 0),
(0.50, 0, 0),
(0.75, 0, 0),
(1, 0, 0),
(1, 0, 0),
]
def get_material(name):
if name in bpy.data.materials:
mat = bpy.data.materials[name]
else:
mat = bpy.data.materials.new(name)
return mat
def get_color(v):
r = g = b = 0
n = len(colors)
step = 1 / (n - 1)
for k in range(3):
for i in range(n):
pp = 1 - (n - 1) * abs(v - step * i)
pp = max(0, min(1, pp))
if k == 2:
r = max(r, colors[i][0] * pp)
elif k == 1:
g = max(g, colors[i][1] * pp)
elif k == 0:
b = max(b, colors[i][2] * pp)
return (r, g, b)
for cube in cubes:
mat = get_material(cube.name)
if len(cube.data.materials) == 0:
cube.data.materials.append(mat)
mat.diffuse_intensity = 1
mat.specular_intensity = 0
mat.emit = 2
mat.use_transparency = True
#mat.transparency_method = 'RAYTRACE'
mat.transparency_method = 'Z_TRANSPARENCY'
mat.alpha = .3
#mat.raytrace_transparency.ior = 1.3
#mat.raytrace_transparency.depth = 5
for i in range(start, end):
value = cube.animation_data.action.fcurves[0].evaluate(frame=i)
mat.diffuse_color = get_color(value)
mat.keyframe_insert("diffuse_color", frame=i)
for f in mat.animation_data.action.fcurves:
for k in f.keyframe_points:
k.interpolation = 'CONSTANT'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment