diff --git a/Source/UnrealEnginePython/Private/UEPyEditor.cpp b/Source/UnrealEnginePython/Private/UEPyEditor.cpp index 1e8768847..6f95ecba0 100644 --- a/Source/UnrealEnginePython/Private/UEPyEditor.cpp +++ b/Source/UnrealEnginePython/Private/UEPyEditor.cpp @@ -12,6 +12,7 @@ #include "FbxMeshUtils.h" #include "Kismet2/BlueprintEditorUtils.h" #include "Editor/LevelEditor/Public/LevelEditorActions.h" +#include "Editor/UnrealEd/Public/EditorLevelUtils.h" 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 * return (PyObject *)ret; } +PyObject *py_ue_factory_create_new(ue_PyUObject *self, PyObject * args) { + + ue_py_check(self); + + char *name; + if (!PyArg_ParseTuple(args, "s:factory_create_new", &name)) { + return NULL; + } + + if (!self->ue_object->IsA()) + return PyErr_Format(PyExc_Exception, "uobject is not a Factory"); + + UPackage *outer = CreatePackage(nullptr, UTF8_TO_TCHAR(name)); + if (!outer) + return PyErr_Format(PyExc_Exception, "unable to create package"); + + UFactory *factory = (UFactory *)self->ue_object; + UClass *u_class = self->ue_object->GetClass(); + + char *obj_name = strrchr(name, '/') + 1; + if (strlen(obj_name) < 1) { + return PyErr_Format(PyExc_Exception, "invalid object name"); + } + + UObject *u_object = factory->FactoryCreateNew(u_class, outer, FName(UTF8_TO_TCHAR(obj_name)), RF_Public | RF_Standalone, nullptr, GWarn); + + if (!u_object) + return PyErr_Format(PyExc_Exception, "unable to create new object from factory"); + + FAssetRegistryModule::AssetCreated(u_object); + outer->MarkPackageDirty(); + + ue_PyUObject *ret = ue_get_python_wrapper(u_object); + if (!ret) + return PyErr_Format(PyExc_Exception, "uobject is in invalid state"); + + Py_INCREF(ret); + return (PyObject *)ret; +} + +PyObject *py_unreal_engine_add_level_to_world(PyObject *self, PyObject * args) { + + PyObject *py_world; + char *name; + PyObject *py_bool = nullptr; + if (!PyArg_ParseTuple(args, "Os|O:add_level_to_world", &py_world, &name, &py_bool)) { + return NULL; + } + + if (!ue_is_pyuobject(py_world)) + return PyErr_Format(PyExc_Exception, "argument is not a UWorld"); + + ue_PyUObject *py_obj_world = (ue_PyUObject *)py_world; + + if (!py_obj_world->ue_object->IsA()) { + return PyErr_Format(PyExc_Exception, "argument is not a UWorld"); + } + + UWorld *u_world = (UWorld *)py_obj_world->ue_object; + + UClass *streaming_mode_class = ULevelStreamingKismet::StaticClass(); + if (py_bool && PyObject_IsTrue(py_bool)) { + streaming_mode_class = ULevelStreamingAlwaysLoaded::StaticClass(); + } + + ULevel *level = EditorLevelUtils::AddLevelToWorld(u_world, UTF8_TO_TCHAR(name), streaming_mode_class); + if (level) { + // TODO: update levels list + } + + ue_PyUObject *ret = ue_get_python_wrapper(level); + if (!ret) + return PyErr_Format(PyExc_Exception, "uobject is in invalid state"); + + Py_INCREF(ret); + return (PyObject *)ret; +} + +PyObject *py_unreal_engine_move_selected_actors_to_level(PyObject *self, PyObject * args) { + + PyObject *py_level; + if (!PyArg_ParseTuple(args, "OO:move_selected_actors_to_level", &py_level)) { + return NULL; + } + + if (!ue_is_pyuobject(py_level)) + return PyErr_Format(PyExc_Exception, "argument is not a ULevelStreaming"); + + ue_PyUObject *py_obj_level = (ue_PyUObject *)py_level; + + if (!py_obj_level->ue_object->IsA()) { + return PyErr_Format(PyExc_Exception, "argument is not a ULevelStreaming"); + } + + ULevel *level = (ULevel *)py_obj_level->ue_object; + + GEditor->MoveSelectedActorsToLevel(level); + + Py_INCREF(Py_None); + return Py_None; +} + #endif diff --git a/Source/UnrealEnginePython/Private/UEPyEditor.h b/Source/UnrealEnginePython/Private/UEPyEditor.h index 48b757542..89a8bf09e 100644 --- a/Source/UnrealEnginePython/Private/UEPyEditor.h +++ b/Source/UnrealEnginePython/Private/UEPyEditor.h @@ -35,6 +35,12 @@ PyObject *py_unreal_engine_editor_on_asset_post_import(PyObject *, PyObject *); PyObject *py_unreal_engine_editor_command_build(PyObject *, PyObject *); PyObject *py_unreal_engine_editor_command_build_lighting(PyObject *, PyObject *); +PyObject *py_unreal_engine_add_level_to_world(ue_PyUObject *, PyObject *); +PyObject *py_unreal_engine_move_selected_actors_to_level(ue_PyUObject *, PyObject *); + + +PyObject *py_ue_factory_create_new(ue_PyUObject *, PyObject *); + // efeng additional functions PyObject *py_unreal_engine_create_material_instance( PyObject *, PyObject * ); diff --git a/Source/UnrealEnginePython/Private/UEPyModule.cpp b/Source/UnrealEnginePython/Private/UEPyModule.cpp index 13868e12e..00302417f 100644 --- a/Source/UnrealEnginePython/Private/UEPyModule.cpp +++ b/Source/UnrealEnginePython/Private/UEPyModule.cpp @@ -165,7 +165,10 @@ static PyMethodDef unreal_engine_methods[] = { { "slate_box", py_unreal_engine_slate_box, METH_VARARGS, "" }, { "slate_window", py_unreal_engine_slate_window, METH_VARARGS, "" }, { "slate_button", py_unreal_engine_slate_button, METH_VARARGS, "" }, - { "get_editor_window", py_unreal_engine_get_editor_window, METH_VARARGS, "" }, + + + { "add_level_to_world", py_unreal_engine_add_level_to_world, METH_VARARGS, "" }, + { "move_actor_to_level", py_unreal_engine_move_selected_actors_to_level, METH_VARARGS, "" }, { "editor_on_asset_post_import", py_unreal_engine_editor_on_asset_post_import, METH_VARARGS, "" }, #endif @@ -269,6 +272,8 @@ static PyMethodDef ue_PyUObject_methods[] = { { "save_package", (PyCFunction)py_ue_save_package, METH_VARARGS, "" }, { "asset_can_reimport", (PyCFunction)py_ue_asset_can_reimport, METH_VARARGS, "" }, { "asset_reimport", (PyCFunction)py_ue_asset_reimport, METH_VARARGS, "" }, + + { "factory_create_new", (PyCFunction)py_ue_factory_create_new, METH_VARARGS, "" }, #endif { "is_rooted", (PyCFunction)py_ue_is_rooted, METH_VARARGS, "" }, diff --git a/Source/UnrealEnginePython/Private/UEPyWorld.cpp b/Source/UnrealEnginePython/Private/UEPyWorld.cpp index 449ec92e1..8461809ad 100644 --- a/Source/UnrealEnginePython/Private/UEPyWorld.cpp +++ b/Source/UnrealEnginePython/Private/UEPyWorld.cpp @@ -188,7 +188,7 @@ PyObject *py_ue_get_levels(ue_PyUObject * self, PyObject * args) { PyObject *ret = PyList_New(0); - for (ULevel *level : world->GetLevels() ) { + for (ULevel *level : world->GetLevels()) { ue_PyUObject *py_obj = ue_get_python_wrapper(level); if (!py_obj) continue; @@ -197,3 +197,4 @@ PyObject *py_ue_get_levels(ue_PyUObject * self, PyObject * args) { } return ret; } +