Generate Low-Poly Glass Bottle With Blender Python API For Website

Code a 3D Model with Blender Python!
On 3 min, 21 sec read

Create a Glass Bottle in Blender Using Python and Display It in the Browser

In this beginner-friendly tutorial, you will learn how to:

  • Generate a 3D glass bottle in Blender using the Blender Python API
  • Export it as a .glb file (GLTF Binary format)
  • Display the model in a web browser using <model-viewer>

If you are just starting out with Python, this is a great way to combine code with visual creativity.

What You Will Need

  • Blender (any version above 3.0)
  • Python (comes bundled with Blender)
  • Basic knowledge of the command line
  • A text editor like VS Code or Blender’s built-in text editor
  • A browser that supports WebGL (Chrome, Firefox, Edge, etc.)

Step 1: Generate the Glass Bottle with Blender Python

Create a new Python script called glass_bottle.py and paste the following code:





import bpy

# Clean up existing objects
bpy.ops.object.select_all(action=&#039;SELECT&#039;)
bpy.ops.object.delete(use_global=False)

# Create bottom (body) with a UV sphere (scaled)
bpy.ops.mesh.primitive_uv_sphere_add(radius=1.0, location=(0, 0, 1))
body = bpy.context.active_object
body.scale[2] = 1.8  # elongate vertically

# Create neck with a cylinder
bpy.ops.mesh.primitive_cylinder_add(radius=0.3, depth=1.0, location=(0, 0, 3))
neck = bpy.context.active_object

# Join neck and body into one mesh
bpy.ops.object.select_all(action=&#039;DESELECT&#039;)
body.select_set(True)
neck.select_set(True)
bpy.context.view_layer.objects.active = body
bpy.ops.object.join()

# Shade smooth
bpy.ops.object.shade_smooth()

# Add glass material
mat = bpy.data.materials.new(name=&#039;GlassMaterial&#039;)
mat.use_nodes = True
bsdf = mat.node_tree.nodes.get(&#039;Principled BSDF&#039;)
bsdf.inputs[&#039;Transmission Weight&#039;].default_value = 1.0
bsdf.inputs[&#039;Roughness&#039;].default_value = 0.05
bsdf.inputs[&#039;IOR&#039;].default_value = 1.45

body.data.materials.append(mat)

# Add cork mesh: a cylinder
bpy.ops.mesh.primitive_cylinder_add(radius=0.18, depth=0.3, location=(0, 0, 3.55))
cork = bpy.context.active_object
cork.name = &quot;Cork&quot;

# Create cork material
cork_mat = bpy.data.materials.new(name=&#039;CorkMaterial&#039;)
cork_mat.use_nodes = True

nodes = cork_mat.node_tree.nodes
bsdf = nodes.get(&#039;Principled BSDF&#039;)

# Set cork-like brown color
bsdf.inputs[&#039;Base Color&#039;].default_value = (0.6, 0.4, 0.1, 1)  # brownish
bsdf.inputs[&#039;Roughness&#039;].default_value = 0.7

# Assign material to cork
cork.data.materials.append(cork_mat)

# Smooth shading
cork.select_set(True)
bpy.context.view_layer.objects.active = cork
bpy.ops.object.shade_smooth()


# Export as GLB
bpy.ops.export_scene.gltf(filepath=&#039;glass_bottle.glb&#039;, export_format=&#039;GLB&#039;)

Step 2: Run the Script in Blender via Command Line

To execute this script without opening Blender’s GUI:

blender --background --python glass_bottle.py

This will generate a file called glass_bottle.glb in your working directory.

Step 3: Display in Browser Using <model-viewer>

You can now display the 3D bottle in any modern browser using the HTML snippet below:

&lt;script type="module" src="https://unpkg.com/@google/model-viewer/dist/model-viewer.min.js"&gt;&lt;/script&gt;

&lt;model-viewer 
  src="glass_bottle.glb"
  alt="A glass bottle"
  auto-rotate 
  camera-controls 
  ar 
  style="width: 600px; height: 600px;"&gt;
&lt;/model-viewer&gt;

📸 Screenshots & Screencast

Low poly glass bottle Python code
Blender Scripting Workspace Displaying Low Poly Glass Bottle Python Code

Low poly glass bottle in Blender
Blender Layout Workspace Displaying Low Poly Glass Bottle

Low poly glass bottle in Web browser
Web Browser Displaying Rendered Low Poly Glass Bottle

Screencast For Blender Python API Low Poly Glass Bottle

Related Learning Resources

If you would like to go deeper, check out my books and course:

Need Help? Book a 1-on-1 Session

I am available for personalized online Python tutorials, including Blender scripting:


Contact Me Here

Thanks for following along. Let me know in the comments how your 3D bottle turned out, or what you would like to build next.

🚀 Recommended Resources


Disclosure: Some of the links above are referral links. I may earn a commission if you make a purchase at no extra cost to you.

About Edward

Edward is a software engineer, author, and designer dedicated to providing the actionable blueprints and real-world tools needed to navigate a shifting economic landscape.

With a provocative focus on the evolution of technology—boldly declaring that “programming is dead”—Edward’s latest work, The Recession Business Blueprint, serves as a strategic guide for modern entrepreneurship. His bibliography also includes Mastering Blender Python API and The Algorithmic Serpent.

Beyond the page, Edward produces open-source tool review videos and provides practical resources for the “build it yourself” movement.

📚 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.

🔨 Build it Yourself – Download Free Plans for Backyard Structures, Small Living, and Woodworking.