Random Terrain generation script, simplified. (With video tutorial and Blend)

(Smoking_mirror) #1

I previously started work on a complex random terrain generation system for my game. It used PIL, the Python image library and worked by mixing images and then using them to modify the vertices of a plane.

I decided to simplify the whole script, and ditched the idea of using images, instead turning to simple blending of values and the use of arrays, instead of images.
Here is the video tutorial on how to use the script:

Here is the Blend:


simple_terrain.blend (581 KB)

(HG1) #2

Works very good and much faster now.

When you change from print “error” to print (“error”) in the line 103 then it is working in Blender 2.5x to.

(Sackadoo) #3


(Smoking_mirror) #4

That’s good. I’m aiming for speed in generation.
I may add some more functions later, such as node type textures, and other things.
Right now I’m working on some code for quick and dirty terracing, to multiply the height of the array if it is above a certain threshold.

(Smoking_mirror) #5

For those who want more functions I’ve updated the blend with terrace erosion (which creates a kind of step effect by boosting mountain core size) and also a function to mark slopes.

I’ve also separated the function for blending vert height and the one for blending colours.

For now I’m just using the colours to mark important areas, such as valleys, mountain tops and slopes. This will allow me to distribute the actual vertex colours to targeted location later, and will also allow me to use the data held in the array to do things like placing villages only in valleys, and avoiding placing trees on slopes.

Check the properties tab, it now has entries for:
t_smooth = how much the terrace effect is smoothed after performing the function
t_amount = how much the mountain area is boosted (1.0+)
t _point = at what height (from 0.0 = all to 1.0 = none) the terrace effect begins.
threshold = how much is marked as slopes. The threshold is the difference in height between a vertex and its neighbours. You will have to experiment with this a little to get the result you want.

Right now mountain effected by terracing are blue, valleys are green, red areas are slopes.

Have fun, and feel free to develop the process and modify the script as much as you like.


simple_terrain2.blend (141 KB)

(tangent) #6

This is a very useful script. My friend is working on a game using Blender, I’ll certainly show him this script. :slight_smile:
Thank you so much, great job.

(imthewolfman) #7

Looks neat, but please update the demo to work on Blender 2.5x

(John_tgh) #8

^^ Neat! Keep Blending!

(Smoking_mirror) #9

I don’t use 2.5 at the moment, as I’m working on a long term project using 2.49.
When I upgrade to 2.5, I will upgrade the script.
Or if someone wants to do it before then… as I said, please feel free to post any updates or additions to the script here. This is a community resource.

As a not for any one trying to understand the script when converting, the first property “ini” was not used in the script. I was saving that for later developments, it’s always useful when a process takes multiple ticks to process, however in this case the whole process takes just one tick.

(mrn) #10

works just fine in 2.6 for me

(mrn) #11

Is there any way to make it so that every time it makes a down or up say every “hill step” it goes by the power of 2 and every valley or down step it goes by a power of -2?

(Raven IIC) #12

Very useful tool. Thanks!

How do you remove the color of valleys or hills (green)?

(juancarlospaco) #13

from random import randint

# This is for GRASS like Ground (all Green random tones)
vert.color = (float(randint(0, 255)), float(randint(0, 128)), 255.0, 1.0)

# This is for SEA or UNDERWATER like Ground (all Blue random tones)
vert.color = (255.0, float(randint(0, 255)), float(randint(0, 128)), 1.0)

# This is for LAVA like ground (all Red random tones)
#vert.color = (float(randint(0, 250)), 0.0, 255.0, 1.0)

Add these lines on line 36 of the Python Script “testing.py”, comment the line " #vert.color = m[x][y][1] ",
add at the beggining of the file at the top " from random import randint ",
des / comment in or out to get one of the 3 group of colors, use just 1 of these each case,
it generates a randomly texture on the floor, so it looks like Grass, Sea, or Lava.
I wonder why he implement rand() function?


if x < own['verts'] and y < own['verts']:
	vert.XYZ = [pos[0],pos[1],m[x][y][0]]

	# This is for GRASS like Ground
	vert.color = (float(randint(0, 255)), float(randint(0, 128)), 255.0, 1.0)

	# This is for SEA or UNDERWATER like Ground
	#vert.color = (255.0, float(randint(0, 255)), float(randint(0, 128)), 1.0)

	# This is for LAVA like ground
	#vert.color = (float(randint(0, 250)), 0.0, 255.0, 1.0)

	#vert.color = m[x][y][1]


(Raven IIC) #14

interesting! It works very well; can come in use for my game experiments

Thank you :slight_smile:

(Merrolin) #15

Two questions : How do I put Generated trees on it, and other stuff pre-made ? And also, How can I give it a cubic style and green color ?

(patrickcook) #16

I updated this script for Blender 2.78, thought I’d share.

[ATTACH]473057. Patrick C.


testing_2.78.zip (2.09 KB)

(BluePrintRandom) #17

I have created a generator that uses many many copies of a single splat mapped tile instance,

The tile is 10x10 vertex

I used kdtree to find all the tiles vertex that share a xy position (border tiles)
This allows you to average the normalz across tiles.

With mesh batching they all essentially become 1 mesh,
And 1 drawcall

If one could create an array, and store the vertex lookup table in it… One could manipulate this data in game.

I intend to do this for a in game map editor for wrectified, somewhere between sim city, and fallout 4 settlements building.

(patrickcook) #18

I uploaded the wrong version of the updated script, sorry. Please use this one.
testing_2.78_ver2.zip (2.11 KB). Patrick C.

(Nicholas_A) #19

Wow what a surprise…

(patrickcook) #20

I updated this script for Blender 2.78.

Patrick C.

testing_2.78_ver2.zip (2.11 KB)