Update the Terrain Locator Connected to Population Tools

When the layout is updated in the scene where a Golaem simulation exists, you sometimes want to update the Terrain which is used by the Population Tool Locators. Here's a utility script which takes a Terrain Locator name and connects it to all the Population Tool Locators of your scene:

global proc updatePopToolLocatorTerrain(string $terrainName)
{
    // check if the input name is a Terrain Locator
    if (`nodeType $terrainName` != "TerrainLocator")
    {
        error ($terrainName + " is not a TerrainLocator");
        return;
    }

    // update the connection
    string $popTools[] = getObjectsOfType("PopulationToolLocator");
    for ($popTool in $popTools)
    {
        string $previousTerrain = glmGetMapNode($popTool + ".inTerrain");
        if ($previousTerrain != "")
        {
            glmUnmapNode($previousTerrain, "message", ($popTool + ".inTerrain"));
            refresh;
            glmMapNode($terrainName, "message", ($popTool + ".inTerrain"));
            refresh;
        }
    }
}

This function can be called this way:

updatePopToolLocatorTerrain("terrainShape1");