|
| 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 | + |
0 commit comments