Skip to content

Commit 7824830

Browse files
author
Roberto De Ioris
committed
added object duplicate() method
1 parent e14d6b6 commit 7824830

7 files changed

Lines changed: 97 additions & 8 deletions

File tree

‎Source/UnrealEnginePython/Private/UEPyEditor.cpp‎

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -411,21 +411,40 @@ PyObject *py_unreal_engine_message_dialog_open(PyObject * self, PyObject * args)
411411

412412
int app_msg_type;
413413
char *text;
414-
char *title = nullptr;
415-
if (!PyArg_ParseTuple(args, "is|s:message_dialog_open", &app_msg_type, &text, &title)) {
414+
415+
if (!PyArg_ParseTuple(args, "is:message_dialog_open", &app_msg_type, &text)) {
416416
return NULL;
417417
}
418418

419-
FText *f_title = nullptr;
419+
EAppReturnType::Type ret = FMessageDialog::Open((EAppMsgType::Type) app_msg_type, FText::FromString(UTF8_TO_TCHAR(text)));
420+
421+
return PyLong_FromLong(ret);
422+
}
423+
424+
PyObject *py_unreal_engine_create_modal_save_asset_dialog(PyObject * self, PyObject * args) {
420425

421-
if (title) {
422-
FText f_title_value = FText::FromString(UTF8_TO_TCHAR(title));
423-
f_title = &f_title_value;
426+
char *title = (char *)"";
427+
char *path = (char*)"";
428+
char *default_name = (char*)"";
429+
430+
if (!PyArg_ParseTuple(args, "|sss:create_modal_save_asset_dialog", &title, &path, &default_name)) {
431+
return nullptr;
424432
}
425433

426-
EAppReturnType::Type ret = FMessageDialog::Open((EAppMsgType::Type) app_msg_type, FText::FromString(UTF8_TO_TCHAR(text)), f_title);
434+
FSaveAssetDialogConfig config;
435+
config.DialogTitleOverride = FText::FromString(FString(UTF8_TO_TCHAR(title)));
436+
config.DefaultPath = FString(UTF8_TO_TCHAR(path));
437+
config.DefaultAssetName = FString(UTF8_TO_TCHAR(default_name));
438+
config.ExistingAssetPolicy = ESaveAssetDialogExistingAssetPolicy::AllowButWarn;
427439

428-
return PyLong_FromLong(ret);
440+
FContentBrowserModule &ContentBrowserModule = FModuleManager::LoadModuleChecked<FContentBrowserModule>("ContentBrowser");
441+
FString ret = ContentBrowserModule.Get().CreateModalSaveAssetDialog(config);
442+
443+
if (ret.IsEmpty()) {
444+
Py_RETURN_NONE;
445+
}
446+
447+
return PyUnicode_FromString(TCHAR_TO_UTF8(*ret));
429448
}
430449

431450
PyObject *py_unreal_engine_get_asset(PyObject * self, PyObject * args) {

‎Source/UnrealEnginePython/Private/UEPyEditor.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ PyObject *py_unreal_engine_get_assets_by_class(PyObject *, PyObject *);
2121
PyObject *py_unreal_engine_get_assets_by_filter(PyObject *, PyObject *, PyObject *);
2222
PyObject *py_unreal_engine_set_fbx_import_option(PyObject *, PyObject *);
2323

24+
PyObject *py_unreal_engine_create_modal_save_asset_dialog(PyObject *, PyObject *);
25+
2426
PyObject *py_unreal_engine_console_exec(PyObject *, PyObject * args);
2527

2628
PyObject *py_unreal_engine_editor_tick(PyObject *, PyObject *);

‎Source/UnrealEnginePython/Private/UEPyEngine.cpp‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,30 @@ PyObject *py_unreal_engine_convert_relative_path_to_full(PyObject * self, PyObje
143143
return PyUnicode_FromString(TCHAR_TO_UTF8(*FPaths::ConvertRelativePathToFull(UTF8_TO_TCHAR(path))));
144144
}
145145

146+
PyObject *py_unreal_engine_object_path_to_package_name(PyObject * self, PyObject * args) {
147+
char *path;
148+
if (!PyArg_ParseTuple(args, "s:object_path_to_package_name", &path)) {
149+
return NULL;
150+
}
151+
return PyUnicode_FromString(TCHAR_TO_UTF8(*FPackageName::ObjectPathToPackageName(UTF8_TO_TCHAR(path))));
152+
}
153+
154+
PyObject *py_unreal_engine_get_path(PyObject * self, PyObject * args) {
155+
char *path;
156+
if (!PyArg_ParseTuple(args, "s:get_path", &path)) {
157+
return NULL;
158+
}
159+
return PyUnicode_FromString(TCHAR_TO_UTF8(*FPaths::GetPath(UTF8_TO_TCHAR(path))));
160+
}
161+
162+
PyObject *py_unreal_engine_get_base_filename(PyObject * self, PyObject * args) {
163+
char *path;
164+
if (!PyArg_ParseTuple(args, "s:get_base_filename", &path)) {
165+
return NULL;
166+
}
167+
return PyUnicode_FromString(TCHAR_TO_UTF8(*FPaths::GetBaseFilename(UTF8_TO_TCHAR(path))));
168+
}
169+
146170
PyObject *py_unreal_engine_create_world(PyObject * self, PyObject * args) {
147171
int world_type = 0;
148172
if (!PyArg_ParseTuple(args, "|i:create_world", &world_type)) {

‎Source/UnrealEnginePython/Private/UEPyEngine.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ PyObject *py_unreal_engine_get_viewport_size(PyObject *, PyObject *);
4848

4949
PyObject *py_unreal_engine_create_world(PyObject *, PyObject *);
5050

51+
PyObject *py_unreal_engine_object_path_to_package_name(PyObject *, PyObject *);
52+
PyObject *py_unreal_engine_get_path(PyObject *, PyObject *);
53+
PyObject *py_unreal_engine_get_base_filename(PyObject *, PyObject *);
54+
5155

5256
#if WITH_EDITOR
5357
PyObject *py_unreal_engine_editor_get_active_viewport_screenshot(PyObject *, PyObject *);

‎Source/UnrealEnginePython/Private/UEPyModule.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ static PyMethodDef unreal_engine_methods[] = {
157157
{ "get_content_dir", py_unreal_engine_get_content_dir, METH_VARARGS, "" },
158158
{ "convert_relative_path_to_full", py_unreal_engine_convert_relative_path_to_full, METH_VARARGS, "" },
159159

160+
{ "get_path", py_unreal_engine_get_path, METH_VARARGS, "" },
161+
{ "get_base_filename", py_unreal_engine_get_base_filename, METH_VARARGS, "" },
162+
{ "object_path_to_package_name", py_unreal_engine_object_path_to_package_name, METH_VARARGS, "" },
163+
160164
{ "compress_image_array", py_unreal_engine_compress_image_array, METH_VARARGS, "" },
161165
{ "create_checkerboard_texture", py_unreal_engine_create_checkerboard_texture, METH_VARARGS, "" },
162166
{ "create_transient_texture", py_unreal_engine_create_transient_texture, METH_VARARGS, "" },
@@ -237,6 +241,7 @@ static PyMethodDef unreal_engine_methods[] = {
237241
{ "add_component_to_blueprint", py_unreal_engine_add_component_to_blueprint, METH_VARARGS, "" },
238242
{ "create_material_instance", py_unreal_engine_create_material_instance, METH_VARARGS, "" },
239243
{ "message_dialog_open", py_unreal_engine_message_dialog_open, METH_VARARGS, "" },
244+
{ "create_modal_save_asset_dialog", py_unreal_engine_create_modal_save_asset_dialog, METH_VARARGS, "" },
240245
{ "set_fbx_import_option", py_unreal_engine_set_fbx_import_option, METH_VARARGS, "" },
241246

242247
{ "create_new_graph", py_unreal_engine_create_new_graph, METH_VARARGS, "" },
@@ -418,6 +423,7 @@ static PyMethodDef ue_PyUObject_methods[] = {
418423

419424
{ "find_actor_by_label", (PyCFunction)py_ue_find_actor_by_label, METH_VARARGS, "" },
420425
{ "save_package", (PyCFunction)py_ue_save_package, METH_VARARGS, "" },
426+
{ "duplicate", (PyCFunction)py_ue_duplicate, METH_VARARGS, "" },
421427
{ "asset_can_reimport", (PyCFunction)py_ue_asset_can_reimport, METH_VARARGS, "" },
422428
{ "asset_reimport", (PyCFunction)py_ue_asset_reimport, METH_VARARGS, "" },
423429

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#if WITH_EDITOR
77
#include "Runtime/AssetRegistry/Public/AssetRegistryModule.h"
8+
#include "ObjectTools.h"
89
#include "UnrealEd.h"
910
#endif
1011

@@ -1240,5 +1241,37 @@ PyObject *py_ue_asset_reimport(ue_PyUObject * self, PyObject * args) {
12401241
Py_INCREF(Py_False);
12411242
return Py_False;
12421243
}
1244+
1245+
PyObject *py_ue_duplicate(ue_PyUObject * self, PyObject * args) {
1246+
1247+
ue_py_check(self);
1248+
1249+
char *package_name;
1250+
char *object_name;
1251+
PyObject *py_overwrite = nullptr;
1252+
1253+
if (!PyArg_ParseTuple(args, "ss|O:duplicate", &package_name, &object_name, &py_overwrite))
1254+
return nullptr;
1255+
1256+
ObjectTools::FPackageGroupName pgn;
1257+
pgn.ObjectName = UTF8_TO_TCHAR(object_name);
1258+
pgn.GroupName = FString("");
1259+
pgn.PackageName = UTF8_TO_TCHAR(package_name);
1260+
1261+
TSet<UPackage *> refused;
1262+
1263+
#if ENGINE_MINOR_VERSION < 14
1264+
UObject *new_asset = ObjectTools::DuplicateSingleObject(self->ue_object, pgn, refused);
1265+
#else
1266+
UObject *new_asset = ObjectTools::DuplicateSingleObject(self->ue_object, pgn, refused, (py_overwrite && PyObject_IsTrue(py_overwrite)));
1267+
#endif
1268+
1269+
ue_PyUObject *ret = ue_get_python_wrapper(new_asset);
1270+
if (!ret)
1271+
return PyErr_Format(PyExc_Exception, "uobject is in invalid state");
1272+
Py_INCREF(ret);
1273+
return (PyObject *)ret;
1274+
1275+
}
12431276
#endif
12441277

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ PyObject *py_ue_modify(ue_PyUObject *, PyObject *);
5959

6060
#if WITH_EDITOR
6161
PyObject *py_ue_save_package(ue_PyUObject *, PyObject *);
62+
PyObject *py_ue_duplicate(ue_PyUObject *, PyObject *);
6263
PyObject *py_ue_asset_can_reimport(ue_PyUObject *, PyObject *);
6364
PyObject *py_ue_asset_reimport(ue_PyUObject *, PyObject *);
6465

0 commit comments

Comments
 (0)