Generate Low-Poly Rubber Duck With Blender Python API For Website

Create a 3D Rubber Duck with Python!
On 4 min, 30 sec read

Create a Cute Rubber Duck in Blender Using Python (No GUI) and Display It on Your Website

If you’re new to Blender and Python, this tutorial will guide you through creating a simple, stylized rubber duck entirely via Blender’s Python API – all from the command line without opening the Blender interface. Then, you’ll learn how to display the 3D duck model on your website using the <model-viewer> web component.

Step 1: Running Blender Python Script Without GUI

You can write a Blender Python script that generates the rubber duck model automatically. Instead of manually working in Blender’s GUI, you run Blender in background mode to execute your Python script.

Use this command in your terminal or command prompt (replace your_duck_script.py with your script’s filename):

blender --background --python your_duck_script.py

This runs Blender without the graphical interface, executes your script, and you can export the model directly in the script as a .glb or .gltf file.

Step 2: Export the Model Automatically

Include export code inside your Python script like this:




import bpy
import bmesh
import math
import os

# --- Clear existing objects ---
bpy.ops.object.select_all(action=&#039;SELECT&#039;)
bpy.ops.object.delete()

# --- Plastic Material Function ---
def plastic_mat(name, color):
    mat = bpy.data.materials.new(name)
    mat.use_nodes = True
    nodes = mat.node_tree.nodes
    links = mat.node_tree.links
    nodes.clear()

    output = nodes.new(type=&quot;ShaderNodeOutputMaterial&quot;)
    bsdf = nodes.new(type=&quot;ShaderNodeBsdfPrincipled&quot;)
    bsdf.inputs[&quot;Base Color&quot;].default_value = color
    bsdf.inputs[&quot;Roughness&quot;].default_value = 0.2  # Slightly glossy

    links.new(bsdf.outputs[&quot;BSDF&quot;], output.inputs[&quot;Surface&quot;])
    return mat

# --- Materials ---
mat_body = plastic_mat(&quot;DuckBodyMat&quot;, (1.0, 0.9, 0.2, 1))     # Yellow
mat_beak = plastic_mat(&quot;DuckBeakMat&quot;, (1.0, 0.4, 0.05, 1))    # Orange
mat_eye = plastic_mat(&quot;DuckEyeMat&quot;, (0.02, 0.02, 0.02, 1))    # Black

# --- Helper: Create UV Sphere ---
def add_uv_sphere(name, loc, scale, mat):
    bpy.ops.mesh.primitive_uv_sphere_add(segments=32, ring_count=16, radius=1, location=loc)
    obj = bpy.context.active_object
    obj.name = name
    obj.scale = scale
    obj.data.materials.append(mat)
    bpy.ops.object.shade_smooth()
    return obj

# --- Create Body ---
body = add_uv_sphere(&quot;Duck_Body&quot;, (0, 0, 0.5), (1.0, 1.2, 0.9), mat_body)

# --- Create Head ---
head = add_uv_sphere(&quot;Duck_Head&quot;, (1.2, 0, 1.25), (0.5, 0.5, 0.5), mat_body)

# --- Create Duck Lips (Upper + Lower Bills) ---
def create_duck_beak():
    # Upper bill
    bpy.ops.mesh.primitive_uv_sphere_add(segments=32, ring_count=16, location=(1.55, 0, 1.15))
    upper = bpy.context.active_object
    upper.name = &quot;Duck_UpperBeak&quot;
    upper.scale = (0.35, 0.25, 0.1)
    upper.data.materials.append(mat_beak)
    bpy.ops.object.shade_smooth()

    # Lower bill
    bpy.ops.mesh.primitive_uv_sphere_add(segments=32, ring_count=16, location=(1.55, 0, 1.05))
    lower = bpy.context.active_object
    lower.name = &quot;Duck_LowerBeak&quot;
    lower.scale = (0.3, 0.22, 0.08)
    lower.data.materials.append(mat_beak)
    bpy.ops.object.shade_smooth()

    return upper, lower

upper_beak, lower_beak = create_duck_beak()

# --- Create Eyes ---
eye_L = add_uv_sphere(&quot;Duck_Eye_L&quot;, (1.65, 0.15, 1.35), (0.07, 0.07, 0.07), mat_eye)
eye_R = add_uv_sphere(&quot;Duck_Eye_R&quot;, (1.65, -0.15, 1.35), (0.07, 0.07, 0.07), mat_eye)

# --- Create Wings ---
wing_R = add_uv_sphere(&quot;Duck_Wing_R&quot;, (0.2, -0.8, 0.8), (0.6, 0.2, 0.4), mat_body)
wing_R.rotation_euler = (0, 0, math.radians(-25))

wing_L = add_uv_sphere(&quot;Duck_Wing_L&quot;, (0.2, 0.8, 0.8), (0.6, 0.2, 0.4), mat_body)
wing_L.rotation_euler = (0, 0, math.radians(25))

# --- Subdivision for Smoothness ---
for obj in [body, head, upper_beak, lower_beak, wing_L, wing_R]:
    mod = obj.modifiers.new(name=&quot;Subsurf&quot;, type=&#039;SUBSURF&#039;)
    mod.levels = 2
    mod.render_levels = 2

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

This way, when you run Blender headless, the model is generated and exported automatically.

Step 3: Embedding Your 3D Rubber Duck on a Website

To display the 3D duck on a webpage, use the <model-viewer> component – a simple, interactive viewer for 3D models in modern browsers.

Here is example HTML to embed in your site:




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

&lt;model-viewer src=&quot;path/to/rubber_duck.glb&quot;  
              alt=&quot;Cute Rubber Duck&quot;  
              auto-rotate camera-controls  
              style=&quot;width: 400px; height: 400px;&quot;&gt;
&lt;/model-viewer&gt;

Make sure to upload your exported rubber_duck.glb to your web host, and update the src attribute accordingly.

📸 Screenshots & Screencast

Low poly rubber duck Python code
Blender Scripting Workspace Displaying Low Poly Rubber Duck Python Code

Low poly rubber duck in Blender
Blender Layout Workspace Displaying Low Poly Rubber Duck

Low poly rubber duck in Web browser
Web Browser Displaying Rendered Low Poly Rubber Duck

Screencast For Blender Python API Low Poly Rubber Duck

Additional Learning Resources

Expand your Python and Blender scripting skills with my recommended books and courses:

Personalized Python & Blender Tutorials

I also offer one-on-one online tutorials for Python programming and Blender scripting. Reach out if you want personalized guidance.

Contact me for tutorials

Feel free to leave questions or share your duck creations in the comments!

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