# Scripting help for Audio Visualizer Tutorial

So I was watching a tutorial and following along with how to make this audio visualizer: http://cgcookie.com/archive/building-an-audio-visualizer-in-python/

This is what the portion of the script in the video that I am having problems with looks like. It's on line 32:

This is my entire script so far (Up to the point in the video shown in the picture):

import bpy

rows = 5
columns = 5

r = 0
c = 0

for i in range(0, rows*columns):
if c == columns:
r += 1
c = 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')
####
bpy.context.active_object.scale.x = 0.5
bpy.context.active_object.scale.y = 0.5
bpy.context.active_object.scale.z = 5
bpy.ops.object.transform_apply(scale=True)

bpy.context.active_object.animation_data.action.fcurves[0],lock = true
bpy.context.active_object.animation_data.action.fcurves[1],lock = true

bpy.context.area.type = 'GRAPH_EDITOR'

step = 20000/ (rows*columns)
bpy.ops.graph.sound_bake(filepath="/Users/USER/Desktop/14 Federation.m4a", low=i*step, high=i*step + step)

bpy.context.active_object.animation_data.action.fcurves[2],lock = true

c += 1


I can see that in the video the part with step = 20000 and the part with bpy.ops.graph.sound_bake(...) are on the same line, but when I do that, it gives me syntax errors.

As of now, this script tells me that it has failed and then tells me to look in the python console. In addition, it only creates one of the cubes that the script is supposed to make a 5x5 grid of. It also unlocks all of the scale parameters and changes nothing for the graph editor for the one cube that it creates.

Strangely enough, removing the entire portion starting with bpy.context.area.type = 'GRAPH_EDITOR' to bpy.context.active_object.animation_data... causes the full grid of 25 cubes to appear.

• it's either the formatting of your post or the script is broken / incomplete? The for loop does nothing as it is, shouldn't all subsequent lines be indented? – CoDEmanX Jan 26 '14 at 2:43
• You can find several answers with complete code for this visualizer at blender.stackexchange.com/questions/30751/… with further explanations about API changes that broke the original tutorial code. – zeffii May 13 '15 at 4:21

It is only creating one cube because your for loop is wrong. In python the indentation is very important. Note that in the code below, all functions for object creation have been indented to become part of the for loop, so that it is run 25 times, rather than once.

for i in range(0, rows*columns):
if c == columns:
r += 1
c = 0

c += 1
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')
####
bpy.context.active_object.scale.x = 0.5
bpy.context.active_object.scale.y = 0.5
bpy.context.active_object.scale.z = 5
bpy.ops.object.transform_apply(scale=True)

bpy.context.active_object.animation_data.action.fcurves[0].lock = True
bpy.context.active_object.animation_data.action.fcurves[1].lock = True


The other errors were since you used bpy.context.active_object.animation_data.action.fcurves[0],lock = True instead of bpy.context.active_object.animation_data.action.fcurves[0].lock = True. That should be it, just pay very close attention to detail when copying code. It's helpful if you can find tutorials that include working code, and then modify it yourself, that way you know what changes break it.

Here's a full code sample that should work. It uses the spiral iteration technique in the tutorial.

import bpy

rows = 3
columns = 3

def spiral(X, Y):
scn = bpy.context.scene
x = y = 0
dx = 0
dy = -1
for i in range(max(X, Y)**2):
if (-X/2 < x <= X/2) and (-Y/2 < y <= Y/2):
obj = bpy.context.active_object

scn.cursor_location = obj.location
scn.cursor_location.z -= 1
bpy.ops.object.origin_set(type="ORIGIN_CURSOR")
obj.scale = (0.5, 0.5, 7)

bpy.ops.object.transform_apply(scale=True)

action = obj.animation_data.action
action.fcurves[0].lock = True
action.fcurves[1].lock = True

bpy.context.area.type = 'GRAPH_EDITOR'

step = 20000 / (rows * columns)
fp = r"C:\Folder\Anotherfolder\Cool song.mp3"
bpy.ops.graph.sound_bake(filepath=fp, low=i*step, high=i*step+step)
action.fcurves[2].lock = True

if x == y or (x < 0 and x == -y) or (x > 0 and x == 1-y):
dx, dy = -dy, dx
x, y = x+dx, y+dy

spiral(rows, columns)


I hope that helps!