Generate Animated Candle With Blender Python API For Website

CODE to 3D WEB! (Blender + )
CODE to 3D WEB! (Blender + )

Live stream set for 2025-10-27 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.


Animate a 3D Candle with Blender Python & View it Online with <model-viewer>

Welcome, fellow creator! Today, we’re diving into a fun, beginner-friendly project: generating an **animated 3D candle** using the **Blender Python API** and then displaying it right in your web browser using Google’s awesome **<model-viewer>** element. You don’t need to be a Python expert-just ready to copy, paste, and run!

Blender is a powerful 3D suite, and its Python API lets you automate complex tasks, like modeling and animating, with just a few lines of code.

Step 1: The Blender Python Script

We’ll use a simple Python script to create a basic candle shape, add a flickering flame animation, and export the final animated model as a **GLB file** (a compact format perfect for the web).

Save the following code as a file named **create_candle.py**:

import bpy

# --- 1. Clean up the scene ---
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()

# --- 2. Create the Candle Body (Cylinder) ---
bpy.ops.mesh.primitive_cylinder_add(radius=0.1, depth=0.5, location=(0, 0, 0.25))
candle = bpy.context.object
candle.name = "Candle"

# --- 3. Create the Wick (Smaller Cylinder) ---
bpy.ops.mesh.primitive_cylinder_add(radius=0.01, depth=0.05, location=(0, 0, 0.525))
wick = bpy.context.object
wick.name = "Wick"

# --- 4. Create the Flame (Simple Cone) ---
bpy.ops.mesh.primitive_cone_add(radius1=0.03, depth=0.1, location=(0, 0, 0.6))
flame = bpy.context.object
flame.name = "Flame"

# --- 5. Add a simple animation (flicker) ---
# We'll just animate the flame's Z-scale over a few frames
flame.scale.y = 1.0 # Initial scale
flame.keyframe_insert(data_path="scale", frame=1)

flame.scale.y = 1.2 # Slightly bigger
flame.keyframe_insert(data_path="scale", frame=10)

flame.scale.y = 0.9 # Slightly smaller
flame.keyframe_insert(data_path="scale", frame=20)

flame.scale.y = 1.0 # Back to normal
flame.keyframe_insert(data_path="scale", frame=30)

# Make the animation cycle
for fcurve in flame.animation_data.action.fcurves:
    fcurve.modifiers.new(type='CYCLES')

# Set scene frame range
bpy.context.scene.frame_end = 30

# --- 6. Export the scene as an animated GLB file ---
output_path = "//animated_candle.glb" # Exports to the same directory as the blend file
bpy.ops.export_scene.gltf(
    filepath=output_path,
    export_format='GLB',
    export_apply=True,
    export_yup=True,
    export_animations=True # Crucial for animation!
)

print(f"Candle model exported to: {output_path}")

Step 2: Running the Python Script

You don’t need to open Blender’s graphical interface to run this! You can execute the script directly from your computer’s **command line** (Terminal on Mac/Linux, Command Prompt/PowerShell on Windows).

First, you need to know the path to your Blender executable.

Navigate to the directory where you saved **create_candle.py** and run the following command. Replace **/path/to/blender** with your actual Blender application path:


/path/to/blender -b -P create_candle.py
    
  • **-b**: This means “background” and runs Blender without the graphical interface-it’s fast!
  • **-P create_candle.py**: This tells Blender to run your Python script.

After running the command, a file named **animated_candle.glb** will appear in the same directory! That’s your animated 3D model.

Showing Your Candle on the Web

Now for the magic trick: displaying your 3D creation in a web browser using **<model-viewer>**. This is an amazing Web Component that handles all the complex 3D rendering for you.

Create a simple HTML file (e.g., **index.html**) and paste the following code. Make sure the **animated_candle.glb** file is in the same folder!

    <script type="module" src="https://ajax.googleapis.com/ajax/libs/model-viewer/3.5.0/model-viewer.min.js"></script>
    <style>
        model-viewer {
            width: 100%;
            height: 400px;
            background-color: #f0f0f0;
        }
    </style>

    <h1>My First Blender Python 3D Web Model!</h1>

    <model-viewer
        src="animated_candle.glb"
        alt="A flickering animated 3D candle"
        auto-rotate
        camera-controls
        ar
        animation-name="Animation"
        autoplay>
    </model-viewer>

Open **index.html** in your browser, and you should see your animated candle spinning! The **autoplay** attribute ensures the flicker animation starts right away.

📸 Screenshots & Screencast

Low poly tensegrity table Python code
Blender Scripting Workspace Displaying Low Poly Animated Candle Python Code

Low poly tensegrity table in Blender
Blender Layout Workspace Displaying Low Poly Animated Candle

Low poly tensegrity table in Web browser
Web Browser Displaying Rendered Low Poly Animated Candle

Screencast For Blender Python API Low Poly Animated Candle

Ready to Learn More?

If you enjoyed automating this 3D creation, you might be interested in taking your **Python** and **Blender Python API** skills to the next level.

I’ve written books to help you on your journey:

You can also enroll in my structured course:

For personalized, in-depth help, I am available for one-on-one online Python tutorials, including specialized sessions on the Blender Python API. You can reach out to me here:

Happy coding and animating! What other 3D objects are you going to automate next? Let me know in the comments!

Recommended Resources:

Disclosure: Some of the links above are referral (affiliate) links. I may earn a commission if you purchase through them - at no extra cost to you.

About Edward

Edward is a software engineer, web developer, and author dedicated to helping people achieve their personal and professional goals through actionable advice and real-world tools.

As the author of impactful books including Learning JavaScript, Learning Python, Learning PHP, Mastering Blender Python API, and fiction The Algorithmic Serpent, Edward writes with a focus on personal growth, entrepreneurship, and practical success strategies. His work is designed to guide, motivate, and empower.

In addition to writing, Edward offers professional "full-stack development," "database design," "1-on-1 tutoring," "consulting sessions,", tailored to help you take the next step. Whether you are launching a business, developing a brand, or leveling up your mindset, Edward will be there to support you.

Edward also offers online courses designed to deepen your learning and accelerate your progress. Explore the programming on languages like JavaScript, Python and PHP to find the perfect fit for your journey.

📚 Explore His Books – Visit the Book Shop to grab your copies today.
💼 Need Support? – Learn more about Services and the ways to benefit from his expertise.
🎓 Ready to Learn? – Check out his Online Courses to turn your ideas into results.

Leave a Reply

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