Skip to content

Commit 160cc5a

Browse files
author
Roberto De Ioris
committed
added preliminary support for level management
1 parent 7874b1c commit 160cc5a

File tree

4 files changed

+117
-2
lines changed

4 files changed

+117
-2
lines changed

Source/UnrealEnginePython/Private/UEPyEditor.cpp

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "FbxMeshUtils.h"
1313
#include "Kismet2/BlueprintEditorUtils.h"
1414
#include "Editor/LevelEditor/Public/LevelEditorActions.h"
15+
#include "Editor/UnrealEd/Public/EditorLevelUtils.h"
1516

1617

1718
PyObject *py_unreal_engine_get_editor_world(PyObject * self, PyObject * args) {
@@ -832,5 +833,107 @@ PyObject *py_unreal_engine_create_material_instance(PyObject * self, PyObject *
832833
return (PyObject *)ret;
833834
}
834835

836+
PyObject *py_ue_factory_create_new(ue_PyUObject *self, PyObject * args) {
837+
838+
ue_py_check(self);
839+
840+
char *name;
841+
if (!PyArg_ParseTuple(args, "s:factory_create_new", &name)) {
842+
return NULL;
843+
}
844+
845+
if (!self->ue_object->IsA<UFactory>())
846+
return PyErr_Format(PyExc_Exception, "uobject is not a Factory");
847+
848+
UPackage *outer = CreatePackage(nullptr, UTF8_TO_TCHAR(name));
849+
if (!outer)
850+
return PyErr_Format(PyExc_Exception, "unable to create package");
851+
852+
UFactory *factory = (UFactory *)self->ue_object;
853+
UClass *u_class = self->ue_object->GetClass();
854+
855+
char *obj_name = strrchr(name, '/') + 1;
856+
if (strlen(obj_name) < 1) {
857+
return PyErr_Format(PyExc_Exception, "invalid object name");
858+
}
859+
860+
UObject *u_object = factory->FactoryCreateNew(u_class, outer, FName(UTF8_TO_TCHAR(obj_name)), RF_Public | RF_Standalone, nullptr, GWarn);
861+
862+
if (!u_object)
863+
return PyErr_Format(PyExc_Exception, "unable to create new object from factory");
864+
865+
FAssetRegistryModule::AssetCreated(u_object);
866+
outer->MarkPackageDirty();
867+
868+
ue_PyUObject *ret = ue_get_python_wrapper(u_object);
869+
if (!ret)
870+
return PyErr_Format(PyExc_Exception, "uobject is in invalid state");
871+
872+
Py_INCREF(ret);
873+
return (PyObject *)ret;
874+
}
875+
876+
PyObject *py_unreal_engine_add_level_to_world(PyObject *self, PyObject * args) {
877+
878+
PyObject *py_world;
879+
char *name;
880+
PyObject *py_bool = nullptr;
881+
if (!PyArg_ParseTuple(args, "Os|O:add_level_to_world", &py_world, &name, &py_bool)) {
882+
return NULL;
883+
}
884+
885+
if (!ue_is_pyuobject(py_world))
886+
return PyErr_Format(PyExc_Exception, "argument is not a UWorld");
887+
888+
ue_PyUObject *py_obj_world = (ue_PyUObject *)py_world;
889+
890+
if (!py_obj_world->ue_object->IsA<UWorld>()) {
891+
return PyErr_Format(PyExc_Exception, "argument is not a UWorld");
892+
}
893+
894+
UWorld *u_world = (UWorld *)py_obj_world->ue_object;
895+
896+
UClass *streaming_mode_class = ULevelStreamingKismet::StaticClass();
897+
if (py_bool && PyObject_IsTrue(py_bool)) {
898+
streaming_mode_class = ULevelStreamingAlwaysLoaded::StaticClass();
899+
}
900+
901+
ULevel *level = EditorLevelUtils::AddLevelToWorld(u_world, UTF8_TO_TCHAR(name), streaming_mode_class);
902+
if (level) {
903+
// TODO: update levels list
904+
}
905+
906+
ue_PyUObject *ret = ue_get_python_wrapper(level);
907+
if (!ret)
908+
return PyErr_Format(PyExc_Exception, "uobject is in invalid state");
909+
910+
Py_INCREF(ret);
911+
return (PyObject *)ret;
912+
}
913+
914+
PyObject *py_unreal_engine_move_selected_actors_to_level(PyObject *self, PyObject * args) {
915+
916+
PyObject *py_level;
917+
if (!PyArg_ParseTuple(args, "OO:move_selected_actors_to_level", &py_level)) {
918+
return NULL;
919+
}
920+
921+
if (!ue_is_pyuobject(py_level))
922+
return PyErr_Format(PyExc_Exception, "argument is not a ULevelStreaming");
923+
924+
ue_PyUObject *py_obj_level = (ue_PyUObject *)py_level;
925+
926+
if (!py_obj_level->ue_object->IsA<ULevel>()) {
927+
return PyErr_Format(PyExc_Exception, "argument is not a ULevelStreaming");
928+
}
929+
930+
ULevel *level = (ULevel *)py_obj_level->ue_object;
931+
932+
GEditor->MoveSelectedActorsToLevel(level);
933+
934+
Py_INCREF(Py_None);
935+
return Py_None;
936+
}
937+
835938
#endif
836939

Source/UnrealEnginePython/Private/UEPyEditor.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ PyObject *py_unreal_engine_editor_on_asset_post_import(PyObject *, PyObject *);
3535
PyObject *py_unreal_engine_editor_command_build(PyObject *, PyObject *);
3636
PyObject *py_unreal_engine_editor_command_build_lighting(PyObject *, PyObject *);
3737

38+
PyObject *py_unreal_engine_add_level_to_world(ue_PyUObject *, PyObject *);
39+
PyObject *py_unreal_engine_move_selected_actors_to_level(ue_PyUObject *, PyObject *);
40+
41+
42+
PyObject *py_ue_factory_create_new(ue_PyUObject *, PyObject *);
43+
3844
// efeng additional functions
3945
PyObject *py_unreal_engine_create_material_instance( PyObject *, PyObject * );
4046

Source/UnrealEnginePython/Private/UEPyModule.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ static PyMethodDef unreal_engine_methods[] = {
165165
{ "slate_box", py_unreal_engine_slate_box, METH_VARARGS, "" },
166166
{ "slate_window", py_unreal_engine_slate_window, METH_VARARGS, "" },
167167
{ "slate_button", py_unreal_engine_slate_button, METH_VARARGS, "" },
168-
{ "get_editor_window", py_unreal_engine_get_editor_window, METH_VARARGS, "" },
168+
169+
170+
{ "add_level_to_world", py_unreal_engine_add_level_to_world, METH_VARARGS, "" },
171+
{ "move_actor_to_level", py_unreal_engine_move_selected_actors_to_level, METH_VARARGS, "" },
169172

170173
{ "editor_on_asset_post_import", py_unreal_engine_editor_on_asset_post_import, METH_VARARGS, "" },
171174
#endif
@@ -269,6 +272,8 @@ static PyMethodDef ue_PyUObject_methods[] = {
269272
{ "save_package", (PyCFunction)py_ue_save_package, METH_VARARGS, "" },
270273
{ "asset_can_reimport", (PyCFunction)py_ue_asset_can_reimport, METH_VARARGS, "" },
271274
{ "asset_reimport", (PyCFunction)py_ue_asset_reimport, METH_VARARGS, "" },
275+
276+
{ "factory_create_new", (PyCFunction)py_ue_factory_create_new, METH_VARARGS, "" },
272277
#endif
273278

274279
{ "is_rooted", (PyCFunction)py_ue_is_rooted, METH_VARARGS, "" },

Source/UnrealEnginePython/Private/UEPyWorld.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ PyObject *py_ue_get_levels(ue_PyUObject * self, PyObject * args) {
188188

189189
PyObject *ret = PyList_New(0);
190190

191-
for (ULevel *level : world->GetLevels() ) {
191+
for (ULevel *level : world->GetLevels()) {
192192
ue_PyUObject *py_obj = ue_get_python_wrapper(level);
193193
if (!py_obj)
194194
continue;
@@ -197,3 +197,4 @@ PyObject *py_ue_get_levels(ue_PyUObject * self, PyObject * args) {
197197
}
198198
return ret;
199199
}
200+

0 commit comments

Comments
 (0)