Skip to content

Instantly share code, notes, and snippets.

@natecraddock
Last active March 18, 2016 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natecraddock/096ad099dfaf11703924 to your computer and use it in GitHub Desktop.
Save natecraddock/096ad099dfaf11703924 to your computer and use it in GitHub Desktop.
A broken audio visualizer
# Audio Visualizer Script
import bpy
import bmesh
import mathutils
import math
import os
# Set these variables to change things
number_of_cubes = 3
multiplier = 2 # Multiplier for the driver
# Place the filename of the song here (relative to the current .blend file)
filepath = ''
# Startframe for baking
bpy.data.scenes['Scene'].frame_current = 1
def add_driver_prop(object, type, axis, data_path, expression):
# Adds a driver with the custom property as the variable to the object
# Type..
driver = bpy.context.object.driver_add(type, axis)
driver.driver.type = 'SCRIPTED'
v = driver.driver.variables.new()
v.name = 'data'
v.type = 'SINGLE_PROP'
t = v.targets[0]
t.id = object
t.data_path = data_path
driver.driver.expression = expression
def bake_audio(object, l, h):
object['audio_data'] = 0.0
object.keyframe_insert(data_path='["audio_data"]')
back = bpy.context.area.type
bpy.context.area.type = 'GRAPH_EDITOR'
fp = os.path.join(os.path.dirname(bpy.data.filepath), filepath)
bpy.ops.graph.sound_bake(filepath=fp, low=l, high=h)
print(object)
bpy.context.area.type = back
step = 100000 / number_of_cubes
c = 0
# Create the grid
for x in range(0, number_of_cubes):
bpy.ops.mesh.primitive_cube_add(radius=1, location=(x * 2, 0.0, 0.0))
obj = bpy.context.active_object
# For now, call this.. Later we can fix the creation script
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')
low = float(c * step) + 0.0
high = float((c + 1) * step) + 0.0
# Now do the actual audio visualization
print(obj, low, high)
bake_audio(obj, low, high)
# Attach the baked data to the z scale and location
#add_driver_prop(obj, 'location', 2, '["audio_data"]', "data * " + str(multiplier))
#add_driver_prop(obj, 'scale', 2, '["audio_data"]', "data * " + str(multiplier))
# Deselect everything
bpy.ops.object.select_all(action='DESELECT')
c += 1
# Add the music to the VSE
bpy.context.area.type = 'SEQUENCE_EDITOR'
bpy.context.scene.sequence_editor_clear()
bpy.ops.sequencer.sound_strip_add(filepath = os.path.join(os.path.dirname(bpy.data.filepath), filepath), frame_start = 1)
bpy.context.scene.frame_end = bpy.context.sequences[0].frame_final_duration
bpy.context.area.type = 'TEXT_EDITOR'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment