3
$\begingroup$

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:

enter image description here

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.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')
####
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.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

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.

$\endgroup$
2
  • 3
    $\begingroup$ 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? $\endgroup$
    – CodeManX
    Commented Jan 26, 2014 at 2:43
  • $\begingroup$ 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. $\endgroup$
    – zeffii
    Commented May 13, 2015 at 4:21

2 Answers 2

3
$\begingroup$

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


    bpy.ops.mesh.primitive_cube_add(location =(r*2, c*2, 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.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

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.

$\endgroup$
1
$\begingroup$

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):
            bpy.ops.mesh.primitive_cube_add(location = (x, y, 1))
            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)
            bpy.ops.anim.keyframe_insert_menu(type='Scaling')

            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!

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .