Generate Low-Poly Table With Blender Python API For Website

Build & Display 3D Models in Browser
Build & Display 3D Models in Browser

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

How to Create a Procedural Textured Low Poly Table in Blender using Python and Display it in the Web Browser

In this beginner-friendly tutorial, we will walk through the steps of creating a procedural textured low-poly table using Blender’s Python API. You’ll learn how to generate the model, apply a wood texture, export it as a .glb file, and display it in a web browser using the <model-viewer> tag. This guide is perfect for those starting with 3D modeling in Blender and web development!

Step 1: Create the Low Poly Table in Blender using Python

First, we will create a basic low-poly table using Python and Blender’s scripting interface. Open Blender, navigate to the Scripting tab, and copy-paste the following Python script:

import bpy

# Clear the scene
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)

# Create the table top (a cube)
bpy.ops.mesh.primitive_cube_add(size=2, location=(0, 0, 1))
table_top = bpy.context.object
table_top.scale = (2, 1, 0.1)  # Rectangular shape
table_top.name = "TableTop"

# Create the four table legs (cylinders)
leg_height = 2
leg_radius = 0.1
legs = []

for x, y in [(-1.5, 0.75), (1.5, 0.75), (-1.5, -0.75), (1.5, -0.75)]:
    bpy.ops.mesh.primitive_cylinder_add(radius=leg_radius, depth=leg_height, location=(x, y, leg_height / 2))
    leg = bpy.context.object
    leg.name = f"Leg_{x}_{y}"
    legs.append(leg)

# Create a procedural wood material for the table
wood_material = bpy.data.materials.new(name="WoodMaterial")
wood_material.use_nodes = True
nodes = wood_material.node_tree.nodes
links = wood_material.node_tree.links

# Clear default nodes and set up the texture
for node in nodes:
    nodes.remove(node)

# Add a noise texture for wood grain
noise_texture = nodes.new(type='ShaderNodeTexNoise')
noise_texture.inputs['Scale'].default_value = 5

# Add a color ramp for the texture
color_ramp = nodes.new(type='ShaderNodeValToRGB')
links.new(noise_texture.outputs['Fac'], color_ramp.inputs['Fac'])
color_ramp.color_ramp.elements.new(0.7)
color_ramp.color_ramp.elements[0].color = (0.4, 0.2, 0.1, 1)  # Dark brown
color_ramp.color_ramp.elements[1].color = (0.8, 0.6, 0.3, 1)  # Lighter wood color

# Connect to the diffuse shader
diffuse_shader = nodes.new(type='ShaderNodeBsdfDiffuse')
links.new(color_ramp.outputs['Color'], diffuse_shader.inputs['Color'])

# Output the material
material_output = nodes.new(type='ShaderNodeOutputMaterial')
links.new(diffuse_shader.outputs['BSDF'], material_output.inputs['Surface'])

# Apply the material to the table top
table_top.data.materials.append(wood_material)

# Apply a simple gray material to the table legs
leg_material = bpy.data.materials.new(name="LegMaterial")
leg_material.use_nodes = True
nodes = leg_material.node_tree.nodes
links = leg_material.node_tree.links

for node in nodes:
    nodes.remove(node)

diffuse_shader = nodes.new(type='ShaderNodeBsdfDiffuse')
diffuse_shader.inputs['Color'].default_value = (0.5, 0.5, 0.5, 1)
material_output = nodes.new(type='ShaderNodeOutputMaterial')
links.new(diffuse_shader.outputs['BSDF'], material_output.inputs['Surface'])

# Assign material to legs
for leg in legs:
    leg.data.materials.append(leg_material)

# Apply smooth shading
bpy.ops.object.shade_smooth()

# Export the model as a .glb file
output_path = "/path/to/your/output/table_model.glb"  # Replace with your desired path
bpy.ops.export_scene.gltf(filepath=output_path, export_format='GLB')

print(f"Procedural low-poly table exported to {output_path}")

Step 2: Export the Model as a .glb File

Once you run the script, Blender will automatically export the model as a .glb file. The .glb format is perfect for use on the web because it retains both the geometry and materials in a compact format.

To run the Python script:

  1. Open Blender and switch to the Scripting tab.
  2. Paste the script into the text editor.
  3. Click Run Script.

Make sure to replace "/path/to/your/output/table_model.glb" with the actual path where you want to save the .glb file.

Step 3: Display the Model in a Web Browser Using the <model-viewer> Tag

Once you have your .glb file, it’s easy to display it on any webpage using the <model-viewer> tag. Here’s a simple HTML template that you can use:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>3D Table Display</title>
    <script type="module" src="https://cdn.skypack.dev/@google/model-viewer"></script>
</head>
<body>

<model-viewer src="path/to/your/model.glb" alt="Procedural low-poly table" auto-rotate camera-controls></model-viewer>

</body>
</html>

Make sure to replace path/to/your/model.glb with the actual location of your exported .glb file. When you open the HTML file in a browser, the 3D table will be displayed, and you can interact with it using the mouse.

Screenshots and Screencast

Low poly table Python code
Blender Scripting Workspace Displaying Low Poly Table Python Code

Low poly table in Blender
Blender Layout Workspace Displaying Low Poly Table

Low poly table in Web browser
Web Browser Displaying Rendered Low Poly Table

Screencast For Blender Python API Low Poly Textured Table

Resources

If you want to dive deeper into Python and Blender scripting, here are some great resources to check out:

Final Thoughts

Congratulations! You’ve successfully learned how to create a procedural low-poly table using Blender and Python. You’ve also learned how to export the model and display it in a web browser using the <model-viewer> tag. This is just the beginning of what you can create with Blender, Python, and web development. Keep experimenting, and stay tuned for more tutorials!

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 *