Skip to content

Commit 1f505f3

Browse files
author
Roberto De Ioris
committed
improved fbx importer, added factory_import_object, added get_material_graph
1 parent e63d127 commit 1f505f3

File tree

7 files changed

+111
-1
lines changed

7 files changed

+111
-1
lines changed
Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,52 @@
11
#include "PythonConsolePrivatePCH.h"
22

33
#include "PyFbxFactory.h"
4+
#include "FbxMeshUtils.h"
45

56
UPyFbxFactory::UPyFbxFactory(const FObjectInitializer& ObjectInitializer)
67
: Super(ObjectInitializer)
78
{
89
// disable automatic detection of the factory
910
ImportPriority = -1;
11+
1012
}
1113

1214
bool UPyFbxFactory::ConfigureProperties() {
13-
bDetectImportTypeOnImport = true;
15+
bDetectImportTypeOnImport = false;
1416
bShowOption = false;
1517

1618
return true;
1719
}
20+
21+
void UPyFbxFactory::PostInitProperties() {
22+
23+
Super::PostInitProperties();
24+
ImportUI->MeshTypeToImport = FBXIT_MAX;
25+
}
26+
27+
UObject *UPyFbxFactory::FactoryCreateBinary
28+
(
29+
UClass * InClass,
30+
UObject * InParent,
31+
FName InName,
32+
EObjectFlags Flags,
33+
UObject * Context,
34+
const TCHAR * Type,
35+
const uint8 *& Buffer,
36+
const uint8 * BufferEnd,
37+
FFeedbackContext * Warn,
38+
bool & bOutOperationCanceled) {
39+
40+
if (ImportUI->MeshTypeToImport == FBXIT_MAX) {
41+
if (!DetectImportType(UFactory::CurrentFilename)) {
42+
return nullptr;
43+
}
44+
}
45+
46+
FbxMeshUtils::SetImportOption(ImportUI);
47+
48+
// ensure auto-detect is skipped
49+
bDetectImportTypeOnImport = false;
50+
51+
return Super::FactoryCreateBinary(InClass, InParent, InName, Flags, Context, Type, Buffer, BufferEnd, Warn, bOutOperationCanceled);
52+
}

Source/PythonConsole/Public/PyFbxFactory.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ class UPyFbxFactory : public UFbxFactory
1414
UPyFbxFactory(const FObjectInitializer& ObjectInitializer);
1515

1616
virtual bool ConfigureProperties() override;
17+
virtual void PostInitProperties() override;
18+
virtual UObject * FactoryCreateBinary
19+
(
20+
UClass * InClass,
21+
UObject * InParent,
22+
FName InName,
23+
EObjectFlags Flags,
24+
UObject * Context,
25+
const TCHAR * Type,
26+
const uint8 *& Buffer,
27+
const uint8 * BufferEnd,
28+
FFeedbackContext * Warn,
29+
bool & bOutOperationCanceled) override;
1730

1831
};
1932

Source/UnrealEnginePython/Private/UEPyEditor.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,45 @@ PyObject *py_ue_factory_create_new(ue_PyUObject *self, PyObject * args) {
12611261
return (PyObject *)ret;
12621262
}
12631263

1264+
PyObject *py_ue_factory_import_object(ue_PyUObject *self, PyObject * args) {
1265+
1266+
ue_py_check(self);
1267+
1268+
char *filename = nullptr;
1269+
char *name = nullptr;
1270+
if (!PyArg_ParseTuple(args, "ss:factory_import_object", &filename, &name)) {
1271+
return NULL;
1272+
}
1273+
1274+
if (!self->ue_object->IsA<UFactory>())
1275+
return PyErr_Format(PyExc_Exception, "uobject is not a Factory");
1276+
1277+
UFactory *factory = (UFactory *)self->ue_object;
1278+
1279+
FString object_name = ObjectTools::SanitizeObjectName(FPaths::GetBaseFilename(UTF8_TO_TCHAR(filename)));
1280+
FString pkg_name = FString(UTF8_TO_TCHAR(name)) + TEXT("/") + object_name;
1281+
1282+
UPackage *outer = CreatePackage(nullptr, *pkg_name);
1283+
if (!outer)
1284+
return PyErr_Format(PyExc_Exception, "unable to create package");
1285+
1286+
bool canceled = false;
1287+
UObject *u_object = factory->ImportObject(factory->ResolveSupportedClass(), outer, FName(*object_name), RF_Public | RF_Standalone, UTF8_TO_TCHAR(filename), nullptr, canceled);
1288+
1289+
if (!u_object)
1290+
return PyErr_Format(PyExc_Exception, "unable to create new object from factory");
1291+
1292+
FAssetRegistryModule::AssetCreated(u_object);
1293+
outer->MarkPackageDirty();
1294+
1295+
ue_PyUObject *ret = ue_get_python_wrapper(u_object);
1296+
if (!ret)
1297+
return PyErr_Format(PyExc_Exception, "uobject is in invalid state");
1298+
1299+
Py_INCREF(ret);
1300+
return (PyObject *)ret;
1301+
}
1302+
12641303
PyObject *py_unreal_engine_add_level_to_world(PyObject *self, PyObject * args) {
12651304

12661305
PyObject *py_world;

Source/UnrealEnginePython/Private/UEPyEditor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ PyObject *py_unreal_engine_move_selected_actors_to_level(PyObject *, PyObject *)
5656

5757

5858
PyObject *py_ue_factory_create_new(ue_PyUObject *, PyObject *);
59+
PyObject *py_ue_factory_import_object(ue_PyUObject *, PyObject *);
5960

6061
PyObject *py_unreal_engine_editor_take_high_res_screen_shots(PyObject *, PyObject *);
6162

Source/UnrealEnginePython/Private/UEPyMaterial.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,4 +418,21 @@ PyObject *py_ue_static_mesh_set_shadow_for_lod(ue_PyUObject *self, PyObject * ar
418418
return Py_None;
419419
}
420420

421+
PyObject *py_ue_get_material_graph(ue_PyUObject *self, PyObject * args) {
422+
423+
ue_py_check(self);
424+
425+
if (!self->ue_object->IsA<UMaterialInterface>()) {
426+
return PyErr_Format(PyExc_Exception, "uobject is not a UMaterialInterface");
427+
}
428+
429+
UMaterialInterface *material_interface = (UMaterialInterface *)self->ue_object;
430+
431+
ue_PyUObject *ret = ue_get_python_wrapper((UObject *)material_interface->GetMaterial()->MaterialGraph);
432+
if (!ret)
433+
return PyErr_Format(PyExc_Exception, "PyUObject is in invalid state");
434+
Py_INCREF(ret);
435+
return (PyObject *)ret;
436+
}
437+
421438
#endif

Source/UnrealEnginePython/Private/UEPyMaterial.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ PyObject *py_ue_set_material_parent(ue_PyUObject *, PyObject *);
2424
PyObject *py_ue_static_mesh_set_collision_for_lod(ue_PyUObject *, PyObject *);
2525
PyObject *py_ue_static_mesh_set_shadow_for_lod(ue_PyUObject *, PyObject *);
2626

27+
PyObject *py_ue_get_material_graph(ue_PyUObject *, PyObject *);
28+
2729
#endif

Source/UnrealEnginePython/Private/UEPyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ static PyMethodDef ue_PyUObject_methods[] = {
353353
{ "asset_reimport", (PyCFunction)py_ue_asset_reimport, METH_VARARGS, "" },
354354

355355
{ "factory_create_new", (PyCFunction)py_ue_factory_create_new, METH_VARARGS, "" },
356+
{ "factory_import_object", (PyCFunction)py_ue_factory_import_object, METH_VARARGS, "" },
356357

357358
{ "graph_add_node_call_function", (PyCFunction)py_ue_graph_add_node_call_function, METH_VARARGS, "" },
358359
{ "graph_add_node_custom_event", (PyCFunction)py_ue_graph_add_node_custom_event, METH_VARARGS, "" },
@@ -365,6 +366,8 @@ static PyMethodDef ue_PyUObject_methods[] = {
365366

366367
{ "node_pins", (PyCFunction)py_ue_node_pins, METH_VARARGS, "" },
367368
{ "node_find_pin", (PyCFunction)py_ue_node_find_pin, METH_VARARGS, "" },
369+
370+
{ "get_material_graph", (PyCFunction)py_ue_get_material_graph, METH_VARARGS, "" },
368371
#endif
369372

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

0 commit comments

Comments
 (0)