Skip to content

Commit 4d9b1b4

Browse files
author
Roberto De Ioris
committed
added preliminary texture api
1 parent f9b16b9 commit 4d9b1b4

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

Source/UnrealEnginePython/Private/UEPyAudio.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "UnrealEnginePythonPrivatePCH.h"
22

33

4-
54
PyObject *py_ue_play_sound_at_location(ue_PyUObject *self, PyObject * args) {
65

76
ue_py_check(self);

Source/UnrealEnginePython/Private/UEPyModule.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "UEPySlate.h"
2424
#include "UEPyPackage.h"
2525
#include "UEPyAssetUserData.h"
26+
#include "UEPyTexture.h"
2627
#if WITH_EDITOR
2728
#include "UEPyEditor.h"
2829
#endif
@@ -424,6 +425,10 @@ static PyMethodDef ue_PyUObject_methods[] = {
424425
// Timer
425426
{ "set_timer", (PyCFunction)py_ue_set_timer, METH_VARARGS, "" },
426427

428+
// Texture
429+
{ "texture_get_data", (PyCFunction)py_ue_texture_get_data, METH_VARARGS, "" },
430+
{ "texture_set_data", (PyCFunction)py_ue_texture_set_data, METH_VARARGS, "" },
431+
427432
// Sequencer
428433
{ "sequencer_master_tracks", (PyCFunction)py_ue_sequencer_master_tracks, METH_VARARGS, "" },
429434
{ "sequencer_possessable_tracks", (PyCFunction)py_ue_sequencer_possessable_tracks, METH_VARARGS, "" },
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "UnrealEnginePythonPrivatePCH.h"
2+
3+
4+
PyObject *py_ue_texture_get_data(ue_PyUObject *self, PyObject * args) {
5+
6+
ue_py_check(self);
7+
8+
int mipmap = 0;
9+
10+
if (!PyArg_ParseTuple(args, "|i:texture_get_data", &mipmap)) {
11+
return NULL;
12+
}
13+
14+
if (!self->ue_object->IsA<UTexture2D>())
15+
return PyErr_Format(PyExc_Exception, "object is not a Texture2D");
16+
17+
UTexture2D *tex = (UTexture2D *)self->ue_object;
18+
19+
if (mipmap >= tex->GetNumMips())
20+
return PyErr_Format(PyExc_Exception, "invalid mipmap id");
21+
22+
const char *blob = (const char*)tex->PlatformData->Mips[mipmap].BulkData.Lock(LOCK_READ_ONLY);
23+
PyObject *bytes = PyByteArray_FromStringAndSize(blob, (Py_ssize_t)tex->PlatformData->Mips[mipmap].BulkData.GetBulkDataSize());
24+
tex->PlatformData->Mips[mipmap].BulkData.Unlock();
25+
return bytes;
26+
}
27+
28+
PyObject *py_ue_texture_set_data(ue_PyUObject *self, PyObject * args) {
29+
30+
ue_py_check(self);
31+
32+
PyObject *byte_array;
33+
int mipmap = 0;
34+
35+
if (!PyArg_ParseTuple(args, "O|i:texture_get_data", &byte_array, &mipmap)) {
36+
return NULL;
37+
}
38+
39+
if (!self->ue_object->IsA<UTexture2D>())
40+
return PyErr_Format(PyExc_Exception, "object is not a Texture2D");
41+
42+
UTexture2D *tex = (UTexture2D *)self->ue_object;
43+
44+
if (!PyByteArray_Check(byte_array))
45+
return PyErr_Format(PyExc_Exception, "argument is not a bytearray");
46+
47+
if (mipmap >= tex->GetNumMips())
48+
return PyErr_Format(PyExc_Exception, "invalid mipmap id");
49+
50+
char *blob = (char*)tex->PlatformData->Mips[mipmap].BulkData.Lock(LOCK_READ_WRITE);
51+
int32 len = tex->PlatformData->Mips[mipmap].BulkData.GetBulkDataSize();
52+
Py_ssize_t byte_array_size = PyByteArray_Size(byte_array);
53+
// avoid making mess
54+
if (byte_array_size > len)
55+
byte_array_size = len;
56+
char *byte_array_blob = PyByteArray_AsString(byte_array);
57+
FMemory::Memcpy(blob, byte_array_blob, byte_array_size);
58+
tex->PlatformData->Mips[mipmap].BulkData.Unlock();
59+
Py_INCREF(Py_None);
60+
return Py_None;
61+
}
62+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
4+
5+
#include "UnrealEnginePython.h"
6+
7+
PyObject *py_ue_texture_get_data(ue_PyUObject *, PyObject *);
8+
PyObject *py_ue_texture_set_data(ue_PyUObject *, PyObject *);

0 commit comments

Comments
 (0)