Generate Blender Cube Audio Animation Using Python

Blender Cube Animation Controlled By Audio

Live stream set for 2025-05-19 at 14:00:00 Eastern

Ask questions in the live chat about any programming or lifestyle topic.

This livestream will be on YouTube or you can watch below.

Blender Is A 3D Creation Software Tool

Blender is cross-platform for Mac OS including POSIX systems, Windows and Linux.

Blender is written in C++ and Python. It is easy to customize using extensions which include add-ons and themes. Blender comes with a built-in video sequence editor (VSE).

The Graph Editor is used to add audio that controls the key frame animation.

The focus of this tutorial will be using the Blender Python API to create a custom script that generates a cube audio animation.

  1. Use default cube object
  2. Add bloom effect using compositing nodes.
  3. Tweak the material shading of Principled BSDF.
  4. Animate object using scale and key frames.
  5. Switch to the Graph Editor.
  6. Use sound to samples to select the audio file.
  7. If audio is desired then it can be added to the VSE.
  8. Render out the animation.
  9. Create a blend file.

Requirements For Blender Python API

Glossary:

API

Application Programming Interface is a set of rules or protocols that enables software applications to communicate with each other to exchange data, features, and functionality.

UI

User Interface is the point of interaction between humans and machines, allowing effective operation and control of the machine from the human end.

Blender Editors

Used for displaying and modifying different aspects of data.

Blender Areas

Window divided into a number of rectangles that reserve space for Editors.

Blender Workspaces

Predefined window layouts consisting of a set of Areas containing Editors for specific tasks.

Tools

Programming Tools
Name Description Example
Text editor For creating and editing source code Apache Netbeans IDE
SSH Secure Shell Client OpenSSH
Shell Access Access to the command line. Terminal
Name Description Example

Overview:

  • Text editor
  • Syntax highlighting
  • Autocompletion
  • Smart indent
  • Find and replace
  • Run script

Menus:

  1. File menu.
  2. Edit menu.
  3. Render menu.
  4. Help menu.

Workspaces:

  1. Scripting Workspace.
  2. Video Editing Workspace.

Scripting Context Menus:

  • Cut
  • Copy
  • Paste
  • Duplicate Line
  • Move Line(s) Up
  • Move Line(s) Down
  • Indent
  • Unindent
  • Toggle Comments
  • Text Auto Complete

import bpy

# Cube #
cube = bpy.data.objects["Cube"]

# Compositing Workspace #
bpy.context.window.workspace = bpy.data.workspaces["Compositing"]
scene = bpy.context.scene
scene.use_nodes = True
compositor_node_tree = scene.node_tree

# Add Composting Nodes
render_layers_node = compositor_node_tree.nodes.new(type="CompositorNodeRLayers")
glare_node = compositor_node_tree.nodes.new(type="CompositorNodeGlare")
glare_node.location = (300, 320)
glare_node.glare_type = "BLOOM"
composite_node = compositor_node_tree.nodes.new(type="CompositorNodeComposite")
composite_node.location = (500, 150)
compositor_node_tree.links.new(render_layers_node.outputs["Image"], glare_node.inputs["Image"])
compositor_node_tree.links.new(glare_node.outputs["Image"], composite_node.inputs["Image"])

# Material Shading #
material = bpy.data.materials.new(name="Diffuse")
material.use_nodes = True
material.node_tree.nodes.get('Principled BSDF').inputs["Metallic"].default_value = 0.855

# Animate #
bpy.context.window.workspace = bpy.data.workspaces["Layout"]
scene = bpy.context.scene
start_frame = 1
cube.keyframe_insert("scale", frame=start_frame)
cube.scale = (1, 1, 0.5)
mid_frame = 105
cube.keyframe_insert("scale", frame=mid_frame)
cube.scale = (1, 1, 2)
last_frame = 210
cube.keyframe_insert("scale", frame=last_frame)

# Bake Sound To F-curve #
for a in bpy.context.screen.areas:
    if a.type == "TIMELINE":
        bpy.context.area.type = "GRAPH_EDITOR"
        cube.select_set(True)
        bpy.ops.graph.sound_to_samples(filepath="cubeaudio.opus")
        break

# Bonus VSE Sound Strip #
sstr = bpy.context.scene.sequence_editor.sequences.new_sound(
    name="First Sound Strip",
    filepath="cubeaudio.opus",
    channel=1,
    frame_start=1
)

# Render Settings #
bpy.context.scene.world.node_tree.nodes["Background"].inputs["Color"].default_value = (0, 0, 0, 1)
bpy.context.scene.eevee.taa_render_samples = 16
bpy.context.scene.render.film_transparent = True
bpy.context.scene.render.resolution_x = 1920
bpy.context.scene.render.resolution_y = 1080
bpy.context.scene.render.resolution_percentage = 100
bpy.context.scene.render.fps = 30
bpy.context.scene.frame_end = 210
bpy.context.scene.render.filepath = "//video-cubeaudio-"
bpy.context.scene.render.image_settings.file_format = "FFMPEG"
bpy.context.scene.render.ffmpeg.format = "MPEG4"
bpy.context.scene.render.ffmpeg.codec = "H264"
bpy.context.scene.render.ffmpeg.audio_codec = "AAC"
bpy.context.scene.render.ffmpeg.constant_rate_factor = "MEDIUM"
bpy.context.scene.render.ffmpeg.ffmpeg_preset = "REALTIME"

# Render Animation #
bpy.ops.render.render(animation=True, write_still=True)

# Generate Blend File #
bpy.ops.wm.save_as_mainfile(filepath="cubeaudio.blend")

Blender Python API File Start Screen
Blender Python API Generated Blend File Start Screen

Blender Python API File Scripting Screen
Blender Python API Generated Blend File Scripting Screen

Blender Python API File Video Editing Screen
Blender Python API Generated Blend File Video Editing Screen


Usage

Blender can be installed at the any time before or after installing Python. Blender can be downloaded from Blender project. Python can be downloaded from Python language. The initial screen displays a default cube, camera and light source. The scripting screen displays a Python Interactive console window and an editor window. It is possible to open a custom python file or paste code into the editor. Editor features such as line numbers, word wrap, syntax highlighting, and zoom are available.

Open Source

Blender is licensed under the GNU General Public License, version 2 or later. The copyleft license comes with strict rules and requirements to ensure the software remains free and open-source. It allows commercial use, modification, distribution, and allows making derivatives proprietary, consult the license for more specific details.

Python is licensed under the Python Software Foundation License. The permissive license requires the preservation of the copyright notice and disclaimer. It allows commercial use, modification, distribution, and allows making derivatives proprietary, consult the license for more specific details.

Conclusion:

Install the Blender application by compiling from source or downloading binaries for your device. Use Python to create custom scripts or extensions for Blender customization. The Blender Python API can be used to dynamically generate cube audio animations and render the output.

If you enjoy this article, consider supporting me by purchasing one of my WordPress Ojambo.com Plugins or programming OjamboShop.com Online Courses or publications at Edward Ojambo Programming Books or become a donor here Ojambo.com Donate

References:

Leave a Reply

Your email address will not be published. Required fields are marked *