Procedural Galaxy Generation From Blender To Threejs

Procedural 3D for Beginners
Procedural 3D for Beginners

Live stream set for 2026-02-04 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.

Introduction

Building 3D galaxies is easy with Python scripts. Start by opening Blender on your Fedora system.

Procedural generation creates complex assets with code. This method saves time during the design phase.

Using Blender Python API for Geometry

Open the Scripting tab inside Blender 5.0 now. You will use the bmesh module for speed.

The script runs a loop to create vertices. It uses math functions to calculate star positions.

import bpy
import bmesh
import math
import random

def create_galaxy(stars=5000):
    mesh = bpy.data.meshes.new("Galaxy")
    obj = bpy.data.objects.new("Galaxy", mesh)
    bpy.context.collection.objects.link(obj)
    bm = bmesh.new()
    for i in range(stars):
        angle = random.uniform(0, 12.0)
        dist = random.uniform(0.2, 5.0)
        x = dist * math.cos(angle + dist) + random.gauss(0, 0.1)
        y = dist * math.sin(angle + dist) + random.gauss(0, 0.1)
        z = random.gauss(0, 0.05)
        bm.verts.new((x, y, z))
    bm.to_mesh(mesh)
    bm.free()

create_galaxy()

Applying Logarithmic Spiral Math

A logarithmic spiral formula creates the galaxy arms. This math places points along a curved path.

Use the random module to add organic noise. This makes the star distribution look very natural.

Vertices are stored in a single mesh object. This keeps the file size small for web.

Exporting to Web Optimized Formats

Export your finished galaxy as a GLB file. Choose the include custom properties option during export.

The GLB format contains geometry and material data. It is the standard for modern web 3D.

Displaying with Threejs

Create a basic HTML file for your project. Include the Threejs library using a script tag.

import * as THREE from "three";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";

const scene = new THREE.Scene();
const loader = new GLTFLoader();
loader.load("galaxy.glb", function(gltf) {
    scene.add(gltf.scene);
});

Use the GLTFLoader class to import your model. Add a perspective camera to view the scene.

Set up a basic animation loop for rotation. The galaxy will spin slowly in the browser.

You have now mastered the 3D web workflow. Create more complex shapes with this same logic.

📸 Screenshots & Screencast

Procedural Galaxy Python code
Blender Scripting Workspace Displaying Procedural Galaxy Python Code

Procedural Galaxy in Blender
Blender Layout Workspace Displaying Procedural Galaxy

Procedural Galaxy in Blender Shading
Blender Shading Workspace Displaying Procedural Galaxy

Procedural Galaxy in Web browser
Web Browser Displaying Rendered Procedural Galaxy

Screencast For Blender Python API Procedural Galaxy

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 *