⚠️ Important:
Please proofread and understand this script before running it in Blender. You must update the downloads_folder path with your actual Windows username and verify all settings match your system configuration.
Use this Python script in Blender to import your downloaded heightmap as a 3D terrain mesh:
import bpy
import os
import glob
from mathutils import Color
# === CONFIGURATION ===
downloads_folder = os.path.expanduser(r"C:\Users\Reference\Downloads") # <- CHANGE username if needed
filename_pattern = "hex_hmap_*.png"
pixel_size = 1.0 # meters per pixel
max_height = 100.0 # customize this as needed
# === FIND MOST RECENT HEIGHTMAP ===
files = glob.glob(os.path.join(downloads_folder, filename_pattern))
if not files:
raise FileNotFoundError(f"No files matching {filename_pattern} found in {downloads_folder}")
latest_file = max(files, key=os.path.getmtime)
print(f"Using latest heightmap: {latest_file}")
# === LOAD IMAGE ===
img = bpy.data.images.load(latest_file)
width, height = img.size
pixels = list(img.pixels) # flat RGBA sequence
print(f"Image size: {width}x{height}")
# === GENERATE HEIGHT DATA (convert grayscale) ===
def get_height(x, y):
# Each pixel = RGBA (4 values)
index = (y * width + x) * 4
r, g, b = pixels[index:index+3]
grayscale = (r + g + b) / 3.0
return grayscale * max_height
# === CREATE MESH ===
verts = []
faces = []
for y in range(height):
for x in range(width):
z = get_height(x, y)
verts.append((x * pixel_size, y * pixel_size, z))
for y in range(height - 1):
for x in range(width - 1):
v0 = y * width + x
v1 = v0 + 1
v2 = v0 + width + 1
v3 = v0 + width
faces.append((v0, v1, v2, v3))
# === CREATE OBJECT ===
mesh = bpy.data.meshes.new("TerrainMesh")
mesh.from_pydata(verts, [], faces)
mesh.update()
obj = bpy.data.objects.new("Terrain", mesh)
bpy.context.collection.objects.link(obj)
# === OPTIONAL: SHADE SMOOTH & CENTER ===
bpy.context.view_layer.objects.active = obj
bpy.ops.object.shade_smooth()
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY', center='BOUNDS')
# Move terrain so bottom is at Z=0
min_z = min(v[2] for v in verts)
obj.location.z -= min_z
print("Terrain successfully created from:", latest_file)
Instructions:
- Download a heightmap using "Save Heightmap PNG" button
- Open Blender and go to Scripting workspace
- Create a new script and paste this code
- Update the
downloads_folder path to match your system
- Adjust
pixel_size and max_height as needed
- Run the script (Alt+P or click Run)