wiki:Scripting/Example1

Version 2 (modified by admin, 7 years ago) (diff)

--

WorldPainter Scripting Example 1

A complete example which creates a world from a height map, sets the default terrain and applies the Frost layer according to altitude, and applies a River layer from a mask, saving the result as a new WorldPainter world which you can edit and export in WorldPainter.

This example script expects three files to exist in the current directory:

heightmap.png - a height map for creating the world. For a good demonstration the water level should be around 62 and there should be a mountain on it somewhere at least 120 high
River.layer - a layer exported from WorldPainter which will create a river where painted (for instance a Custom Ground Cover layer with a negative thickness and Water as the material)
rivermask.png - a black and white mask with the same dimensions as heightmap.png, which is white where the river should be painted

// Load the height map from disk
var heightMap = wp.getHeightMap().fromFile('heightmap.png').go();

// Create a new WorldPainter world from the height map
var world = wp.createWorld().fromHeightMap(heightMap).go();

// Use the same height map to set the default terrain. For the terrain indices
// to use, see: https://www.worldpainter.net/trac/wiki/Scripting/TerrainTypeValues
wp.applyHeightMap(heightMap)
        .toWorld(world)
        .applyToTerrain()
        .fromLevels(0, 64).toTerrain(36) // Beaches (also makes a good ocean floor)
        .fromLevels(65, 96).toTerrain(0) // Grass
        .fromLevels(97, 112).toTerrain(3) // Permadirt
        .fromLevels(113, 255).toTerrain(29) // Rock
        .go();

// Apply the Frost layer above 120, again using the same height map as input
var frostLayer = wp.getLayer().name('Frost').go();
wp.applyHeightMap(heightMap)
        .toWorld(world)
        .applyToLayer(frostLayer)
        .fromLevels(0, 119).toLevel(0)   // Make sure to remove any Frost the
        .fromLevels(120, 255).toLevel(1) // height map import might have added
        .go();

// Load the mask for creating the rivers
var riverMask = wp.getHeightMap().fromFile('rivermask.png').go();

// Load the actual river layer
var riverLayer = wp.getLayer().fromFile('River.layer').go();

// Paint the river layer onto the world using the mask. Map anything that is
// not completely black to apply the layer
wp.applyHeightMap(riverMask)
        .toWorld(world)
        .applyToLayer(riverLayer)
        .fromLevel(0).toLevel(0)
        .fromLevels(1, 255).toLevel(1)
        .go();

// Save the result to disk
wp.saveWorld(world)
        .toFile('output.world')
        .go();

Save this to a file called example1.js in the same directory that contains the height map, etc., and execute the script with the following command from inside that directory:

wpscript example1.js