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

Code a 3D Model with Blender Python!
Code a 3D Model with Blender Python!

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

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='SELECT')
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='DESELECT')
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='GlassMaterial')
mat.use_nodes = True
bsdf = mat.node_tree.nodes.get('Principled BSDF')
bsdf.inputs['Transmission Weight'].default_value = 1.0
bsdf.inputs['Roughness'].default_value = 0.05
bsdf.inputs['IOR'].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 = "Cork"

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

nodes = cork_mat.node_tree.nodes
bsdf = nodes.get('Principled BSDF')

# Set cork-like brown color
bsdf.inputs['Base Color'].default_value = (0.6, 0.4, 0.1, 1)  # brownish
bsdf.inputs['Roughness'].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='glass_bottle.glb', export_format='GLB')

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:

<script type="module" src="https://unpkg.com/@google/model-viewer/dist/model-viewer.min.js"></script>

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

📸 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 (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 *