# Creating a Lanscape using C++ or the Python API

**URL:** <https://forums.unrealengine.com/t/creating-a-lanscape-using-c-or-the-python-api/504997>\
**Category:** Editor Scripting\
**Tags:** Python, unreal-engine, editor, UE4, question, UE4-27\
**Created:** [March 8, 2022, 12:27pm UTC](https://forums.unrealengine.com/t/creating-a-lanscape-using-c-or-the-python-api/504997 "2022-03-08T12:27:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![s\_zervos](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/s_zervos/32/915276_2.png) [@s\_zervos](https://forums.unrealengine.com/u/s_zervos)\
**Post date:** [March 8, 2022, 12:27pm UTC](https://forums.unrealengine.com/t/creating-a-lanscape-using-c-or-the-python-api/504997/1 "2022-03-08T12:27:14Z")

</div>

Hello, I am trying to create and spawn a Landscape through code using C++ or the python API of UE4.27.

_ **For C++** _, with `GetWorld()->SpawnActor<ALandscape>` , `ALandscapeProxy::Import(...)` and `LandscapeEditorDetailCustomization_NewLandscape::OnCreateButtonClicked()` methods as a learning ground, I couldn’t produce a working result.

_ **For Python API** _, I followed the approach of using `unreal.EditorLevelLibrary.spawn_actor_from_class()`  
to spawn a `LandscapeProxy` actor and then alter its heightmap using the `landscape_import_heightmap_from_render_target()` of the [LandscapeProxy](https://docs.unrealengine.com/4.26/en-US/PythonAPI/class/LandscapeProxy.html?highlight=landscapeproxy#unreal.LandscapeProxy) class.  
_Unfortunately_ the spawned actor is of class [LandscapePlaceholder](https://docs.unrealengine.com/4.26/en-US/PythonAPI/class/LandscapePlaceholder.html?highlight=landscape#unreal.LandscapePlaceholder) which does not support any heightmap operations and due to no examples and the lacking documentation of the python unreal API, I can’t seem to find another solution.

Is there a way to make/spawn a Landscape using either C++ or Python. Examples and guides are very much appreciated!

---

<div class="post-metadata">

**Author:** ![chpsemail](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chpsemail/32/496464_2.png) [@chpsemail](https://forums.unrealengine.com/u/chpsemail)\
**Post date:** [March 9, 2022, 9:38am UTC](https://forums.unrealengine.com/t/creating-a-lanscape-using-c-or-the-python-api/504997/2 "2022-03-09T09:38:30Z")

</div>

I extented the landscape python API in my plugin, and lots of others editor API

> **[PythonLandscapeLib](https://www.tacolor.xyz/pages/PythonEditorLib/PythonLandscapeLib.html#set_heightmap_data)**
>
> Python Interface of Extended Editor: PythonLandscapeLib | UE Plugin TAPython documentation

Below is my tool’s snippet using the API.

```auto
    def ui_on_button_ImportHeight_click(self):
        selected_tiles = self.current_selected_tiles()
        taskSteps = len(selected_tiles)

        with unreal.ScopedSlowTask(taskSteps, "Starting Import Landscape Heights") as slow_task:
            slow_task.make_dialog(True)
            for x, y in selected_tiles:
                unreal.EditorLevelLibrary.load_level(self.get_tile_level_path(x, y, self.lod_option))
                slow_task.enter_progress_frame(1, "Importing landscape height {}_{}_LOD{}".format(x, y, self.lod_option))
                landscape = self.get_landcapeproxy(x, y, self.lod_option)
                if landscape:
                    heighmapPath = self.get_heightmap_source_path(x, y, self.lod_option)
                    height_data = self.read_heigh_data(heighmapPath)
                    print("type: {} length: {} elementType: {}".format(type(height_data), len(height_data), type(height_data[0])))
                    unreal.PythonLandscapeLib.set_heightmap_data(landscape, height_data=height_data)
                    landscape.modify()
                    unreal.EditorLevelLibrary.save_current_level()

```

---

<div class="post-metadata">

**Author:** ![s\_zervos](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/s_zervos/32/915276_2.png) [@s\_zervos](https://forums.unrealengine.com/u/s_zervos)\
**Post date:** [March 10, 2022, 2:00pm UTC](https://forums.unrealengine.com/t/creating-a-lanscape-using-c-or-the-python-api/504997/3 "2022-03-10T14:00:54Z")

</div>

I still have not checked your plugin, but I studied your methods and I am interested in your implementation of them.

In your [`PythonLandscapeLib.create_landscape()`](https://www.tacolor.xyz/pages/PythonEditorLib/PythonLandscapeLib.html#create_landscape) method specifically, you create a landscape using `spawn_actor_from_class()` and set the components, sections, etc. using other methods?

---

<div class="post-metadata">

**Author:** ![chpsemail](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chpsemail/32/496464_2.png) [@chpsemail](https://forums.unrealengine.com/u/chpsemail)\
**Post date:** [March 11, 2022, 3:26am UTC](https://forums.unrealengine.com/t/creating-a-lanscape-using-c-or-the-python-api/504997/4 "2022-03-11T03:26:24Z")

</div>

Hi s\_zervos,  
yes, call World-\>SpawnActor\<ALandscape\>(), and set other info, like material layer, HeightData and so on.

I learn it from FDatasmithLandscapeImporter::ImportLandscapeActor and FLandscapeEditorDetailCustomization\_NewLandscape::OnCreateButtonClicked, FYI.

If create a big world, **create\_landscape\_proxy\_with\_guid** is much useful than create\_landscape, which can make each part of the big landscape one by one. It’s SpawnActor\<ALandscapeStreamingProxy\> instead of ALandscape. then assign the same guid.

below is the code of UPythonLandscapeLib::CreateLandscape

```auto
ALandscape * UPythonLandscapeLib::CreateLandscape(const FTransform& LandscapeTransform, const int32& SectionSize, const int32& SectionsPerComponent, const int32& ComponentCountX, const int32& ComponentCountY)
{
	int32 QuadsPerComponent = SectionSize * SectionsPerComponent;

	int32 SizeX = ComponentCountX * QuadsPerComponent + 1;
	int32 SizeY = ComponentCountY * QuadsPerComponent + 1;

	TArray<FLandscapeImportLayerInfo> MaterialImportLayers;
	MaterialImportLayers.Reserve(0);

	TMap<FGuid, TArray<uint16>> HeightDataPerLayers;
	TMap<FGuid, TArray<FLandscapeImportLayerInfo>> MaterialLayerDataPerLayers;

	TArray<uint16> HeightData;
	HeightData.SetNum(SizeX * SizeY);
	for (int32 i = 0; i < HeightData.Num(); i++)
	{
		HeightData[i] = 32768;
	}

	HeightDataPerLayers.Add(FGuid(), MoveTemp(HeightData)); /*ENewLandscapePreviewMode.NewLandscape*/
	// ComputeHeightData will also modify/expand material layers data, which is why we create MaterialLayerDataPerLayers after calling ComputeHeightData
	MaterialLayerDataPerLayers.Add(FGuid(), MoveTemp(MaterialImportLayers));

	//FScopedTransaction Transaction(TEXT("Undo", "Creating New Landscape"));

	UWorld* World = nullptr;
	{
		// We want to create the landscape in the landscape editor mode's world
		FWorldContext& EditorWorldContext = GEditor->GetEditorWorldContext();
		World = EditorWorldContext.World();
	}

	ALandscape* Landscape = World->SpawnActor<ALandscape>();
	Landscape->bCanHaveLayersContent = false;
	Landscape->LandscapeMaterial = nullptr;

	Landscape->SetActorTransform(LandscapeTransform);

	// automatically calculate a lighting LOD that won't crash lightmass (hopefully)
	// < 2048x2048 -> LOD0
	// >=2048x2048 -> LOD1
	// >= 4096x4096 -> LOD2
	// >= 8192x8192 -> LOD3

	//const FGuid& InGuid, int32 InMinX, int32 InMinY, int32 InMaxX, int32 InMaxY, int32 InNumSubsections, int32 InSubsectionSizeQuads, const TMap<FGuid, TArray<uint16>>& InImportHeightData,
	//	const TCHAR* const InHeightmapFileName, const TMap<FGuid, TArray<FLandscapeImportLayerInfo>>& InImportMaterialLayerInfos, ELandscapeImportAlphamapType InImportMaterialLayerType, const TArray<struct FLandscapeLayer>* InImportLayers = nullptr

	Landscape->Import(FGuid::NewGuid(), 0, 0, SizeX - 1, SizeY - 1, SectionsPerComponent, QuadsPerComponent, HeightDataPerLayers, nullptr, MaterialLayerDataPerLayers, ELandscapeImportAlphamapType::Additive);

	Landscape->StaticLightingLOD = FMath::DivideAndRoundUp(FMath::CeilLogTwo((SizeX * SizeY) / (2048 * 2048) + 1), (uint32)2);
	// Register all the landscape components
	ULandscapeInfo* LandscapeInfo = Landscape->GetLandscapeInfo();

	LandscapeInfo->UpdateLayerInfoMap(Landscape);

	Landscape->RegisterAllComponents();

	// Need to explicitly call PostEditChange on the LandscapeMaterial property or the landscape proxy won't update its material
	FPropertyChangedEvent MaterialPropertyChangedEvent(FindFieldChecked< FProperty >(Landscape->GetClass(), FName("LandscapeMaterial")));
	Landscape->PostEditChangeProperty(MaterialPropertyChangedEvent);
	Landscape->PostEditChange();

	return Landscape;
}

```

---

<div class="post-metadata">

**Author:** ![s\_zervos](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/s_zervos/32/915276_2.png) [@s\_zervos](https://forums.unrealengine.com/u/s_zervos)\
**Post date:** [March 11, 2022, 9:32am UTC](https://forums.unrealengine.com/t/creating-a-lanscape-using-c-or-the-python-api/504997/5 "2022-03-11T09:32:36Z")

</div>

Thank you very much for your answers they really helped a lot! Both your python plugin and C++ code were very helpful. I will follow your logic for importing a height/weight map. Thanks again!

---

<div class="post-metadata">

**Author:** ![nande](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/nande/32/1057698_2.png) [@nande](https://forums.unrealengine.com/u/nande)\
**Post date:** [December 4, 2022, 10:02pm UTC](https://forums.unrealengine.com/t/creating-a-lanscape-using-c-or-the-python-api/504997/6 "2022-12-04T22:02:35Z")

</div>

In order to load a material layer weightmap from png you can use

```auto
	FLandscapeImportLayerInfo LayInfoGrass;
	LayInfoGrass.LayerData.Reset();
	LayInfoGrass.LayerData.AddUninitialized(TotalSize);
	LayInfoGrass.LayerName = "Grass";
	LayInfoGrass.SourceFilePath = "path/to/weightmap.png";
	// Important to have a layer info, without it, it will not show at all, and crash.
	LayInfoGrass.LayerInfo = CreateLandscapeLayerInfo(LayInfoGrass.LayerName, false); 
	ReadWeightmapFile(LayInfoGrass.LayerData, LayInfoGrass.SourceFilePath, LayInfoGrass.LayerName, SizeX, SizeY);
	MaterialImportLayers.Add(LayInfoGrass);

.........
MaterialLayerDataPerLayers.Add(BaseLayer.Guid, MaterialImportLayers);
.........

Landscape->Import(FGuid::NewGuid(), 0, 0, SizeX - 1, SizeY - 1,
	                  SectionsPerComponent, QuadsPerSection, HeightDataPerLayers,
	                  *ReimportHeightmapFilePath, MaterialLayerDataPerLayers, ELandscapeImportAlphamapType::Additive, &ImportLayers);
.....

```

CreateLandscapeLayerInfo is a copy of // Engine\UE5\Source\Editor\LandscapeEditor\Private\LandscapeEditorDetailCustomization\_ImportLayers.cpp:322

ReadWeightmapFile is a copy of // Engine\UE5\Source\Editor\WorldBrowser\Private\Tiles\WorldTileCollectionModel.cpp:1504

---

<div class="post-metadata">

**Author:** ![robeche](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@robeche](https://forums.unrealengine.com/u/robeche)\
**Post date:** [March 30, 2023, 3:10pm UTC](https://forums.unrealengine.com/t/creating-a-lanscape-using-c-or-the-python-api/504997/7 "2023-03-30T15:10:10Z")

</div>

Hi there,

I’ve been searching on how to create a new landscape in unreal using python and apply a heightmap texture.

Finally i found your library which seems to do the job but to be honest i’m having issues on how to start with it…

I have just created a landscape with:

```auto
tr1=unreal.Transform(unreal.Vector(0,0,0),unreal.Rotator(0,0,0),unreal.Vector(1,1,1))
unreal.PythonLandscapeLib.create_landscape(tr1,63,2,32,32)

```

But… how do i apply a heightmap to that landscape? I see the function “set\_heightmap\_data” but i don’t know how should i define the “height\_data” required…

Can you provide an example?

Kind regards

Roberto

---

<div class="post-metadata">

**Author:** ![d4wr0s](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/d4wr0s/32/774479_2.png) [@d4wr0s](https://forums.unrealengine.com/u/d4wr0s)\
**Post date:** [November 10, 2023, 5:36pm UTC](https://forums.unrealengine.com/t/creating-a-lanscape-using-c-or-the-python-api/504997/8 "2023-11-10T17:36:14Z")

</div>

Can you provide an example of **create\_landscape\_proxy\_with\_guid** in C++ I can create LandscapeStremingProxy and Landscape separately  
but I don’t understand how to merge them. Thank you.
