Skip to content

Instantly share code, notes, and snippets.

@restartthedream
Created December 7, 2014 08:44
Show Gist options
  • Save restartthedream/c1b665a121c57f927b23 to your computer and use it in GitHub Desktop.
Save restartthedream/c1b665a121c57f927b23 to your computer and use it in GitHub Desktop.
Audio Visualizer w/ color gradient and camera placement
import bpy
def makeMaterial(name, diffuse, specular, alpha): #Easy to use function for making cube materials
mat = bpy.data.materials.new(name)
mat.diffuse_color = diffuse
mat.diffuse_shader = 'LAMBERT'
mat.diffuse_intensity = 1.0
mat.specular_color = specular
mat.specular_shader = 'COOKTORR'
mat.specular_intensity = 0.5
mat.alpha = alpha
mat.ambient = 1
return mat
def setMaterial(ob, mat): #Sets material to cube. mat is returned by makeMaterial
me = ob.data
me.materials.append(mat)
rows = 5
columns = 5
r = 0
c = 0
for i in range(0, rows*columns):
if c == columns:
r +=1 #row count
c = 0 #column count
bpy.ops.mesh.primitive_cube_add(location = (r*2,c*2,0))
bpy.context.scene.cursor_location = bpy.context.active_object.location
bpy.context.scene.cursor_location.z -= 1
bpy.ops.object.origin_set(type='ORIGIN_CURSOR')
###Adds cube, sets the cursor at the cube location.
###Finally, sets object origin as new cursor location to help correct z shifting
bpy.context.active_object.scale.x = 0.5
bpy.context.active_object.scale.y = 0.5
bpy.context.active_object.scale.z = 4
bpy.ops.object.transform_apply(scale=True)
###Scales cube into a rectangle with largest length along z axis. Cubes will scale with these base values
bpy.ops.anim.keyframe_insert_menu(type='Scaling')
bpy.context.active_object.animation_data.action.fcurves[0].lock = True
bpy.context.active_object.animation_data.action.fcurves[1].lock = True
###Cubes will animate by scaling. X and Y are locked as they will not scale
bpy.context.area.type = 'GRAPH_EDITOR'
###View is set to graph editor for sound baking
step = 20000 / (rows*columns)
thecolor = makeMaterial('Color',((step/20000),(step/30000)+(c/11),(step/25000)+(c/20)),(1,1,1),1) #Creates object color based on step and column
setMaterial(bpy.context.object, thecolor) #Sets object color
###The size of sound scaling will gradually decrease as more cubes are added
bpy.ops.graph.sound_bake(filepath="/Users/Michael/Desktop/Music/Superhumanoids/Superhumanoids_-_01_-_Malta.mp3", low=i*step, high=i*step + step)
###Song to bake to graph.
bpy.context.active_object.animation_data.action.fcurves[2].lock = True
###Locks any further changes to z scaling beyond song
c += 1 #Adds one to the column count
scene = bpy.data.scenes["Scene"]
#Sets a variable as the current scene
scene.camera.location.x += 10
scene.camera.location.y -= 2
scene.camera.location.z += 2
#Sets the camera so it has a good view of the visualization. Note that this does not make it rotate, I did that in program.
@mateor
Copy link

mateor commented Dec 17, 2014

Tulane has a Data Visualization class, which is primarily in Python. OpenGL (in python) VTK and a bunch of other tools. It can count as an upper level course for CS major if you ask.

@restartthedream
Copy link
Author

Thanks for letting me know, I'll be sure to look into it. I'll likely be taking a visualization class as a music tech minor as well, so its good to know there are a lot of opportunities to improve.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment