Skip to content

Commit 9ad931f

Browse files
author
Roberto De Ioris
committed
improved heightmap and landscape api
1 parent 404a4a3 commit 9ad931f

5 files changed

Lines changed: 83 additions & 3 deletions

File tree

‎Source/UnrealEnginePython/Private/UEPyEditor.cpp‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "Toolkits/AssetEditorManager.h"
2323
#include "LevelEditor.h"
2424
#include "Editor/LandscapeEditor/Public/LandscapeEditorUtils.h"
25+
#include "Editor/LandscapeEditor/Public/LandscapeEditorModule.h"
26+
#include "Editor/LandscapeEditor/Public/LandscapeFileFormatInterface.h"
2527

2628

2729
PyObject *py_unreal_engine_editor_play_in_viewport(PyObject * self, PyObject * args)
@@ -2145,5 +2147,59 @@ PyObject *py_unreal_engine_heightmap_expand(PyObject * self, PyObject * args)
21452147

21462148
}
21472149

2150+
PyObject *py_unreal_engine_heightmap_import(PyObject * self, PyObject * args)
2151+
{
2152+
char *filename;
2153+
int width = 0;
2154+
int height = 0;
2155+
if (!PyArg_ParseTuple(args, "s|ii:heightmap_import", &filename, &width, &height))
2156+
return nullptr;
2157+
2158+
ILandscapeEditorModule & LandscapeModule = FModuleManager::GetModuleChecked<ILandscapeEditorModule>("LandscapeEditor");
2159+
const ILandscapeHeightmapFileFormat *format = LandscapeModule.GetHeightmapFormatByExtension(*FPaths::GetExtension(UTF8_TO_TCHAR(filename), true));
2160+
2161+
if (!format)
2162+
{
2163+
return PyErr_Format(PyExc_Exception, "invalid heightmap format");
2164+
}
2165+
2166+
FLandscapeFileResolution resolution = { width, height };
2167+
2168+
if (width <= 0 || height <= 0)
2169+
{
2170+
FLandscapeHeightmapInfo info = format->Validate(UTF8_TO_TCHAR(filename));
2171+
if (info.ResultCode == ELandscapeImportResult::Error)
2172+
{
2173+
return PyErr_Format(PyExc_Exception, "unable to import heightmap: %s", TCHAR_TO_UTF8(*info.ErrorMessage.ToString()));
2174+
}
2175+
2176+
if (info.ResultCode == ELandscapeImportResult::Warning)
2177+
{
2178+
UE_LOG(LogPython, Warning, TEXT("%s"), *info.ErrorMessage.ToString());
2179+
}
2180+
2181+
if (info.PossibleResolutions.Num() < 1)
2182+
{
2183+
return PyErr_Format(PyExc_Exception, "unable to retrieve heightmap resolution");
2184+
}
2185+
2186+
resolution = info.PossibleResolutions[0];
2187+
}
2188+
2189+
FLandscapeHeightmapImportData imported = format->Import(UTF8_TO_TCHAR(filename), resolution);
2190+
2191+
if (imported.ResultCode == ELandscapeImportResult::Error)
2192+
{
2193+
return PyErr_Format(PyExc_Exception, "unable to import heightmap: %s", TCHAR_TO_UTF8(*imported.ErrorMessage.ToString()));
2194+
}
2195+
2196+
if (imported.ResultCode == ELandscapeImportResult::Warning)
2197+
{
2198+
UE_LOG(LogPython, Warning, TEXT("%s"), *imported.ErrorMessage.ToString());
2199+
}
2200+
2201+
return Py_BuildValue((char *)"Oii", PyByteArray_FromStringAndSize((char *)imported.Data.GetData(), imported.Data.Num() * sizeof(uint16)), resolution.Width, resolution.Height);
2202+
}
2203+
21482204
#endif
21492205

‎Source/UnrealEnginePython/Private/UEPyEditor.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,5 @@ PyObject *py_unreal_engine_editor_redo(PyObject *, PyObject *);
105105
PyObject *py_unreal_engine_transactions(PyObject *, PyObject *);
106106

107107
PyObject *py_unreal_engine_heightmap_expand(PyObject *, PyObject *);
108-
108+
PyObject *py_unreal_engine_heightmap_import(PyObject *, PyObject *);
109109
#endif

‎Source/UnrealEnginePython/Private/UEPyModule.cpp‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ static PyMethodDef unreal_engine_methods[] = {
255255
{ "new_guid", py_unreal_engine_new_guid, METH_VARARGS, "" },
256256
#if WITH_EDITOR
257257
{ "heightmap_expand", py_unreal_engine_heightmap_expand, METH_VARARGS, "" },
258+
{ "heightmap_import", py_unreal_engine_heightmap_import, METH_VARARGS, "" },
258259
#endif
259260
#pragma warning(suppress: 4191)
260261
{ "get_assets_by_filter", (PyCFunction)py_unreal_engine_get_assets_by_filter, METH_VARARGS | METH_KEYWORDS, "" },
@@ -706,6 +707,7 @@ static PyMethodDef ue_PyUObject_methods[] = {
706707
{ "create_landscape_info", (PyCFunction)py_ue_create_landscape_info, METH_VARARGS, "" },
707708
{ "get_landscape_info", (PyCFunction)py_ue_get_landscape_info, METH_VARARGS, "" },
708709
{ "landscape_import", (PyCFunction)py_ue_landscape_import, METH_VARARGS, "" },
710+
{ "landscape_export_to_raw_mesh", (PyCFunction)py_ue_landscape_export_to_raw_mesh, METH_VARARGS, "" },
709711
#endif
710712

711713
// Player
@@ -894,8 +896,8 @@ void ue_pydelegates_cleanup(ue_PyUObject *self)
894896
UE_LOG(LogPython, Warning, TEXT("Removing UPythonDelegate %p from ue_PyUObject %p mapped to UObject %p"), py_delegate, self, self->ue_object);
895897
#endif
896898
py_delegate->RemoveFromRoot();
899+
}
897900
}
898-
}
899901
self->python_delegates_gc->clear();
900902
delete self->python_delegates_gc;
901903
self->python_delegates_gc = nullptr;
@@ -2040,7 +2042,7 @@ void unreal_engine_py_log_error()
20402042
}
20412043

20422044
PyErr_Clear();
2043-
}
2045+
}
20442046

20452047
// retrieve a UWorld from a generic UObject (if possible)
20462048
UWorld *ue_get_uworld(ue_PyUObject *py_obj)

‎Source/UnrealEnginePython/Private/UObject/UEPyLandscape.cpp‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,25 @@ PyObject *py_ue_landscape_import(ue_PyUObject *self, PyObject * args)
7373

7474
Py_RETURN_NONE;
7575
}
76+
77+
PyObject *py_ue_landscape_export_to_raw_mesh(ue_PyUObject *self, PyObject * args)
78+
{
79+
80+
ue_py_check(self);
81+
82+
int lod = 0;
83+
84+
if (!PyArg_ParseTuple(args, "|i:landscape_import", &lod))
85+
return nullptr;
86+
87+
ALandscapeProxy *landscape = ue_py_check_type<ALandscapeProxy>(self);
88+
if (!landscape)
89+
return PyErr_Format(PyExc_Exception, "uobject is not a ULandscapeProxy");
90+
91+
FRawMesh raw_mesh;
92+
if (!landscape->ExportToRawMesh(lod, raw_mesh))
93+
return PyErr_Format(PyExc_Exception, "unable to export landscape to FRawMesh");
94+
95+
return py_ue_new_fraw_mesh(raw_mesh);
96+
}
7697
#endif

‎Source/UnrealEnginePython/Private/UObject/UEPyLandscape.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
PyObject *py_ue_create_landscape_info(ue_PyUObject *self, PyObject *);
99
PyObject *py_ue_get_landscape_info(ue_PyUObject *self, PyObject *);
1010
PyObject *py_ue_landscape_import(ue_PyUObject *self, PyObject *);
11+
PyObject *py_ue_landscape_export_to_raw_mesh(ue_PyUObject *self, PyObject *);
1112
#endif

0 commit comments

Comments
 (0)