Skip to content

Commit 9d15b6b

Browse files
author
Roberto De Ioris
committed
improved texture creation and added get_or_create_package()
1 parent 83c73c1 commit 9d15b6b

File tree

6 files changed

+48
-23
lines changed

6 files changed

+48
-23
lines changed

Source/PythonEditor/Private/PythonProject.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,4 @@
66
UPythonProject::UPythonProject(const FObjectInitializer& ObjectInitializer)
77
: Super(ObjectInitializer)
88
{
9-
FUnrealEnginePythonModule &PythonModule = FModuleManager::GetModuleChecked<FUnrealEnginePythonModule>("UnrealEnginePython");
10-
Path = PythonModule.ScriptsPath;
11-
if (!FPaths::DirectoryExists(Path)) {
12-
IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
13-
PlatformFile.CreateDirectory(*Path);
14-
}
159
}

Source/UnrealEnginePython/Private/UEPyEngine.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,33 @@ PyObject *py_unreal_engine_create_package(PyObject *self, PyObject * args) {
811811
return (PyObject *)ret;
812812
}
813813

814+
PyObject *py_unreal_engine_get_or_create_package(PyObject *self, PyObject * args) {
815+
816+
char *name;
817+
818+
if (!PyArg_ParseTuple(args, "s:get_or_create_package", &name)) {
819+
return nullptr;
820+
}
821+
822+
UPackage *u_package = (UPackage *)StaticFindObject(nullptr, ANY_PACKAGE, UTF8_TO_TCHAR(name), true);
823+
// create a new package if it does not exist
824+
if (!u_package) {
825+
u_package = CreatePackage(nullptr, UTF8_TO_TCHAR(name));
826+
if (!u_package)
827+
return PyErr_Format(PyExc_Exception, "unable to create package");
828+
u_package->FileName = *FPackageName::LongPackageNameToFilename(UTF8_TO_TCHAR(name), FPackageName::GetAssetPackageExtension());
829+
830+
u_package->FullyLoad();
831+
u_package->MarkPackageDirty();
832+
}
833+
834+
ue_PyUObject *ret = ue_get_python_wrapper(u_package);
835+
if (!ret)
836+
return PyErr_Format(PyExc_Exception, "uobject is in invalid state");
837+
Py_INCREF(ret);
838+
return (PyObject *)ret;
839+
}
840+
814841
PyObject *py_unreal_engine_get_transient_package(PyObject *self, PyObject * args) {
815842

816843
ue_PyUObject *ret = ue_get_python_wrapper(GetTransientPackage());

Source/UnrealEnginePython/Private/UEPyEngine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ PyObject *py_unreal_engine_get_viewport_size(PyObject *, PyObject *);
5151

5252
PyObject *py_unreal_engine_create_world(PyObject *, PyObject *);
5353
PyObject *py_unreal_engine_create_package(PyObject *, PyObject *);
54+
PyObject *py_unreal_engine_get_or_create_package(PyObject *, PyObject *);
5455
PyObject *py_unreal_engine_get_transient_package(PyObject *, PyObject *);
5556

5657
PyObject *py_unreal_engine_object_path_to_package_name(PyObject *, PyObject *);

Source/UnrealEnginePython/Private/UEPyModule.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ static PyMethodDef unreal_engine_methods[] = {
173173

174174
// package
175175
{ "create_package", (PyCFunction)py_unreal_engine_create_package, METH_VARARGS, "" },
176+
{ "get_or_create_package", (PyCFunction)py_unreal_engine_create_package, METH_VARARGS, "" },
176177
{ "get_transient_package", (PyCFunction)py_unreal_engine_get_transient_package, METH_VARARGS, "" },
177178

178179
// slate

Source/UnrealEnginePython/Private/UObject/UEPyTexture.cpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,9 @@ PyObject *py_unreal_engine_create_texture(PyObject * self, PyObject * args) {
232232
char *name;
233233
int width;
234234
int height;
235-
int format = PF_B8G8R8A8;
235+
Py_buffer py_buf;
236236

237-
if (!PyArg_ParseTuple(args, "Osii|i:create_texture", &py_package, &name, &width, &height, &format)) {
237+
if (!PyArg_ParseTuple(args, "Osiiz*:create_texture", &py_package, &name, &width, &height, &py_buf)) {
238238
return nullptr;
239239
}
240240

@@ -249,24 +249,21 @@ PyObject *py_unreal_engine_create_texture(PyObject * self, PyObject * args) {
249249
}
250250
}
251251

252-
UTexture2D *texture = NewObject<UTexture2D>(u_package, UTF8_TO_TCHAR(name), RF_Public | RF_Standalone);
252+
SIZE_T wanted_len = width * height * 4;
253+
254+
TArray<FColor> colors;
255+
colors.AddZeroed(wanted_len);
256+
FCreateTexture2DParameters params;
257+
258+
if ((SIZE_T)py_buf.len < wanted_len)
259+
wanted_len = py_buf.len;
260+
261+
FMemory::Memcpy(colors.GetData(), py_buf.buf, wanted_len);
262+
263+
UTexture2D *texture = FImageUtils::CreateTexture2D(width, height, colors, u_package, UTF8_TO_TCHAR(name), RF_Public | RF_Standalone, params);
253264
if (!texture)
254265
return PyErr_Format(PyExc_Exception, "unable to create texture");
255266

256-
texture->PlatformData = new FTexturePlatformData();
257-
texture->PlatformData->SizeX = width;
258-
texture->PlatformData->SizeY = height;
259-
texture->PlatformData->PixelFormat = (EPixelFormat)format;
260-
261-
int32 blocksX = width / GPixelFormats[(EPixelFormat)format].BlockSizeX;
262-
int32 blocksY = width / GPixelFormats[(EPixelFormat)format].BlockSizeY;
263-
FTexture2DMipMap *mip = new(texture->PlatformData->Mips) FTexture2DMipMap();
264-
mip->SizeX = width;
265-
mip->SizeY = height;
266-
mip->BulkData.Lock(LOCK_READ_WRITE);
267-
mip->BulkData.Realloc(blocksX * blocksY * GPixelFormats[(EPixelFormat)format].BlockBytes);
268-
mip->BulkData.Unlock();
269-
270267
ue_PyUObject *ret = ue_get_python_wrapper(texture);
271268
if (!ret)
272269
return PyErr_Format(PyExc_Exception, "uobject is in invalid state");

Source/UnrealEnginePython/Private/UnrealEnginePython.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,11 @@ void FUnrealEnginePythonModule::StartupModule()
224224
ZipPath = FPaths::Combine(*FPaths::GameContentDir(), UTF8_TO_TCHAR("ue_python.zip"));
225225
}
226226

227+
if (!FPaths::DirectoryExists(ScriptsPath)) {
228+
IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
229+
PlatformFile.CreateDirectory(*ScriptsPath);
230+
}
231+
227232
Py_Initialize();
228233

229234
PyEval_InitThreads();

0 commit comments

Comments
 (0)