Golden Cube In Blender Using Python Embedded On Website

Golden Cube Via Blender Python API Displayed In Web Browser
Golden Cube Via Blender Python API Displayed In Web Browser

Live stream set for 2025-07-09 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.

Introduction

In this beginner-level tutorial, we’ll walk through the process of creating a golden cube using the Blender Python API and displaying it in a web browser using the <model-viewer> element. This simple process will teach you how to work with 3D models in Blender and embed them in websites with minimal effort.

By the end of this tutorial, you’ll:

  • Generate a golden cube in Blender using Python.
  • Export the cube as a .glb file.
  • Embed the .glb model in a webpage using the <model-viewer> element.

Let’s get started!

Step 1: Create the Golden Cube in Blender Using Python

First, we’ll use the Blender Python API to generate a simple cube and apply a golden material. This is done by writing a Python script inside Blender’s scripting workspace.

Here’s the Python code that creates the cube:

import bpy

# Clear any existing mesh objects
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()

# Create a new cube
bpy.ops.mesh.primitive_cube_add(size=2)
cube = bpy.context.object

# Create a new material for the golden effect
material = bpy.data.materials.new(name="GoldenMaterial")
material.use_nodes = True
bsdf = material.node_tree.nodes["Principled BSDF"]
bsdf.inputs["Base Color"].default_value = (1.0, 0.84, 0.0, 1)  # Gold color
bsdf.inputs["Roughness"].default_value = 0.2  # Slightly shiny

# Assign the material to the cube
cube.data.materials.append(material)

# Save the model as a GLB file
bpy.ops.export_scene.gltf(filepath="/path/to/your/golden_cube.glb", export_format='GLB')

Steps to run the script:

  1. Open Blender and switch to the Scripting workspace.
  2. Paste the code in the text editor.
  3. Click Run Script.
  4. The cube will appear in the scene with a golden material applied.
  5. The script will export the cube as a .glb file to the specified directory.

Step 2: Export the Cube as a GLB File

Once you run the script, Blender will create the golden cube and automatically export it as a .glb file. This file can now be used to display the model in any web project.

Step 3: Display the Golden Cube in Your Web Browser Using <model-viewer>

To display the golden cube in your web browser, we’ll use the <model-viewer> tag. This HTML tag is simple to use and allows you to embed 3D models in your website without needing to write complex WebGL or JavaScript code.

Here’s the basic HTML code to display the cube in your browser:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Golden Cube</title>
    <script type="module">
      import '@google/model-viewer';
    </script>
    <style>
      body, html {
        margin: 0;
        padding: 0;
        overflow: hidden;
        height: 100%;
      }
      model-viewer {
        width: 400px;       /* Fixed size */
        height: 400px;      /* Fixed size */
        display: block;
        margin: 0 auto;
        background-color: #f5f5f5;
        border: 1px solid #ddd;
      }
    </style>
  </head>
  <body>
    <model-viewer 
      src="path/to/your/golden_cube.glb" 
      alt="Golden Cube" 
      auto-rotate 
      camera-controls 
      disable-zoom>
    </model-viewer>
  </body>
</html>

Explanation:

  • src=”path/to/your/golden_cube.glb”: Replace this with the path to your .glb file.
  • auto-rotate: This will make the model rotate automatically.
  • camera-controls: Allows users to interact with the model, rotate it, and zoom in and out.
  • disable-zoom: This stops the model from zooming in and out when users scroll with the mouse.

Step 4: Running the Python Script from the Command Line

You can also run the Blender Python script from the command line to automate the process. Here’s how:

  1. Open your command-line interface (Terminal on macOS/Linux, Command Prompt on Windows).
  2. Navigate to your Blender directory.
  3. Run the following command to execute the script:
blender --background --python /path/to/your/script.py

This command tells Blender to run in the background and execute the Python script, which will create the golden cube and export it as a .glb file.

Step 5: Adding Screenshots and YouTube Live Screencast

Golden cube in Blender viewport
Blender Scripting Tab Displaying Golden Cube In Blender Viewport

Golden cube in Blender scripting
Blender Scripting Tab Displaying Golden Cube In Blender Scripting Workspace

Golden cube in Web browser
Web Browser Displaying Rendered Golden Cube

Screencast For Blender Python API Golden Cube

Learn More About Python and Blender

If you enjoyed this tutorial, be sure to check out my books and course to further your Python programming skills:

I also offer one-on-one online Python tutorials, including Blender-specific lessons. Feel free to reach out via my contact page for personalized help!

Conclusion

Congratulations! You’ve successfully learned how to generate a golden cube in Blender using the Python API and display it in a web browser using the simple <model-viewer> tag. This is just one of the many things you can achieve with Blender and Python. Keep experimenting and creating amazing 3D models!

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 *