I’m working on a project and need help with using python to create an audio visualizer. I need a emission node’s strength to be controlled from a f-curve baked from audio.
This code can so far create the visualizer
import bpy
audio = "C:/path/to/audio.mp3"
### Max frequency
max = 10000
rows = 3
columns = 3
r = 0
c = 0
for i in range(0, rows * columns):
if c == columns:
r += 1
c = 0
### Create cube grid
bpy.ops.mesh.primitive_cube_add(location = (r, 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')
###
### Scale cubes into bars
bpy.context.active_object.scale.x = 0.35
bpy.context.active_object.scale.y = 0.35
bpy.context.active_object.scale.z = 5
bpy.ops.object.transform_apply(scale=True)
###
### Bake audio to scale
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 = max/ (rows*columns)
bpy.ops.graph.sound_bake(filepath= audio, low=i*step, high=i*step + step)
bpy.context.active_object.animation_data.action.fcurves[2].lock = True
###
### Bake audio to emission
###
c += 1