Generate Procedural Landscapes With Blender Python API For Website

Code Your Own 3D Terrain
Code Your Own 3D Terrain

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

Procedural Landscapes with Python in Blender: Infinite World Generation Tutorial

Creating vast digital worlds does not require manual sculpting. You can generate infinite procedural landscapes using Blender’s Python API. This method allows you to build unique terrains algorithmically.

Procedural generation uses math to create complex geometry. This script will automate the creation of a terrain mesh. It then applies a noise-based displacement to create mountains.

The Procedural Blender Script

Open the Scripting tab in Blender. Create a new text file and paste this code. This script creates a high-resolution grid and manipulates its height.

import bpy
import bmesh
import random
import math

# Clear existing objects in the scene
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()

# Create a high-resolution grid for the landscape
size = 10
subdiv = 100
bpy.ops.mesh.primitive_grid_add(size=size, x_subdivisions=subdiv, y_subdivisions=subdiv)
obj = bpy.context.active_object

# Access mesh data to manipulate vertices
mesh = obj.data
bm = bmesh.new()
bm.from_mesh(mesh)

# Apply simple procedural noise for mountains
for vert in bm.verts:
    # Use math functions to create organic waves
    noise = math.sin(vert.co.x * 1.5) * math.cos(vert.co.y * 1.5)
    vert.co.z = (noise * 2.0) + (random.uniform(0, 0.3))

# Update mesh and cleanup
bm.to_mesh(mesh)
bm.free()
mesh.update()

# Automatic Export to glTF for Web
export_path = "C:/path/to/your/project/landscape.glb"
bpy.ops.export_scene.gltf(filepath=export_path, export_format='GLB')

Viewing Your World in HTML5

Export your generated landscape as a glTF file. You can then view it in any browser using HTML5. Use the following code to load your landscape with Three.js.

<script type="module">
        import * as THREE from 'https://unpkg.com/three@0.160.0/build/three.module.js';
        import { GLTFLoader } from 'https://unpkg.com/three@0.160.0/examples/jsm/loaders/GLTFLoader.js';

        const scene = new THREE.Scene();
        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        const renderer = new THREE.WebGLRenderer({ antialias: true });
        
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);

        const loader = new GLTFLoader();
        loader.load('landscape.glb', (gltf) => {
            scene.add(gltf.scene);
        });

        const light = new THREE.DirectionalLight(0xffffff, 2);
        light.position.set(2, 5, 5);
        scene.add(light);
        scene.add(new THREE.AmbientLight(0x404040));

        camera.position.set(0, 5, 10);
        camera.lookAt(0, 0, 0);

        function animate() {
            requestAnimationFrame(animate);
            renderer.render(scene, camera);
        }
        animate();
    </script>

Using Python scripts makes world-building fast. You can tweak parameters to generate entirely new terrains. This workflow is perfect for developers building 3D web games.

📸 Screenshots & Screencast

Low poly Procedural Landscape Python code
Blender Scripting Workspace Displaying Low Poly Procedural Landscape Python Code

Low poly Procedural Landscape in Blender
Blender Layout Workspace Displaying Low Poly Procedural Landscape

Low poly Procedural Landscape in Blender Shading
Blender Shading Workspace Displaying Low Poly Procedural Landscape

Low poly Procedural Landscape in Web browser
Web Browser Displaying Rendered Low Poly Procedural Landscape

Screencast For Blender Python API Low Poly Procedural Landscape

Take Your Skills Further

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 *