Skip to content

Commit

Permalink
added preliminary support for level management
Browse files Browse the repository at this point in the history
  • Loading branch information
Roberto De Ioris committed Dec 17, 2016
1 parent 7874b1c commit 160cc5a
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 2 deletions.
103 changes: 103 additions & 0 deletions Source/UnrealEnginePython/Private/UEPyEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<UFactory>())
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<UWorld>()) {
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<ULevel>()) {
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

6 changes: 6 additions & 0 deletions Source/UnrealEnginePython/Private/UEPyEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 * );

Expand Down
7 changes: 6 additions & 1 deletion Source/UnrealEnginePython/Private/UEPyModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, "" },
Expand Down
3 changes: 2 additions & 1 deletion Source/UnrealEnginePython/Private/UEPyWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -197,3 +197,4 @@ PyObject *py_ue_get_levels(ue_PyUObject * self, PyObject * args) {
}
return ret;
}

0 comments on commit 160cc5a

Please sign in to comment.