|
| 1 | +#include "UnrealEnginePythonPrivatePCH.h" |
| 2 | + |
| 3 | +#if WITH_EDITOR |
| 4 | + |
| 5 | +static PyObject *py_ue_fraw_mesh_set_vertex_positions(ue_PyFRawMesh *self, PyObject * args) { |
| 6 | + PyObject *data; |
| 7 | + if (!PyArg_ParseTuple(args, "O:set_vertex_positions", &data)) { |
| 8 | + return nullptr; |
| 9 | + } |
| 10 | + |
| 11 | + PyObject *iter = PyObject_GetIter(data); |
| 12 | + if (!iter) |
| 13 | + return PyErr_Format(PyExc_TypeError, "argument is not an iterable"); |
| 14 | + |
| 15 | + TArray<FVector> vertex; |
| 16 | + |
| 17 | + for (;;) { |
| 18 | + PyObject *item_x = PyIter_Next(iter); |
| 19 | + if (!item_x) |
| 20 | + break; |
| 21 | + if (!PyNumber_Check(item_x)) |
| 22 | + return PyErr_Format(PyExc_Exception, "argument iterable does not contains only numbers"); |
| 23 | + PyObject *py_x = PyNumber_Float(item_x); |
| 24 | + float x = PyFloat_AsDouble(py_x); |
| 25 | + Py_DECREF(py_x); |
| 26 | + |
| 27 | + PyObject *item_y = PyIter_Next(iter); |
| 28 | + if (!item_y) |
| 29 | + break; |
| 30 | + if (!PyNumber_Check(item_y)) |
| 31 | + return PyErr_Format(PyExc_Exception, "argument iterable does not contains only numbers"); |
| 32 | + PyObject *py_y = PyNumber_Float(item_y); |
| 33 | + float y = PyFloat_AsDouble(py_y); |
| 34 | + Py_DECREF(py_y); |
| 35 | + |
| 36 | + PyObject *item_z = PyIter_Next(iter); |
| 37 | + if (!item_z) |
| 38 | + break; |
| 39 | + if (!PyNumber_Check(item_z)) |
| 40 | + return PyErr_Format(PyExc_Exception, "argument iterable does not contains only numbers"); |
| 41 | + PyObject *py_z = PyNumber_Float(item_z); |
| 42 | + float z = PyFloat_AsDouble(py_z); |
| 43 | + Py_DECREF(py_z); |
| 44 | + |
| 45 | + vertex.Add(FVector(x, y, z)); |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + self->raw_mesh->VertexPositions = vertex; |
| 50 | + |
| 51 | + Py_DECREF(iter); |
| 52 | + |
| 53 | + Py_RETURN_NONE; |
| 54 | +} |
| 55 | + |
| 56 | +static PyObject *py_ue_fraw_mesh_set_wedge_tex_coords(ue_PyFRawMesh *self, PyObject * args) { |
| 57 | + PyObject *data; |
| 58 | + int index = 0; |
| 59 | + if (!PyArg_ParseTuple(args, "O|i:set_wedge_tex_coords", &data, &index)) { |
| 60 | + return nullptr; |
| 61 | + } |
| 62 | + |
| 63 | + PyObject *iter = PyObject_GetIter(data); |
| 64 | + if (!iter) |
| 65 | + return PyErr_Format(PyExc_TypeError, "argument is not an iterable"); |
| 66 | + |
| 67 | + TArray<FVector2D> uv; |
| 68 | + |
| 69 | + for (;;) { |
| 70 | + PyObject *item_x = PyIter_Next(iter); |
| 71 | + if (!item_x) |
| 72 | + break; |
| 73 | + if (!PyNumber_Check(item_x)) |
| 74 | + return PyErr_Format(PyExc_Exception, "argument iterable does not contains only numbers"); |
| 75 | + PyObject *py_x = PyNumber_Float(item_x); |
| 76 | + float x = PyFloat_AsDouble(py_x); |
| 77 | + Py_DECREF(py_x); |
| 78 | + |
| 79 | + PyObject *item_y = PyIter_Next(iter); |
| 80 | + if (!item_y) |
| 81 | + break; |
| 82 | + if (!PyNumber_Check(item_y)) |
| 83 | + return PyErr_Format(PyExc_Exception, "argument iterable does not contains only numbers"); |
| 84 | + PyObject *py_y = PyNumber_Float(item_y); |
| 85 | + float y = PyFloat_AsDouble(py_y); |
| 86 | + Py_DECREF(py_y); |
| 87 | + |
| 88 | + uv.Add(FVector2D(x, y)); |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + self->raw_mesh->WedgeTexCoords[index] = uv; |
| 93 | + |
| 94 | + Py_DECREF(iter); |
| 95 | + |
| 96 | + Py_RETURN_NONE; |
| 97 | +} |
| 98 | + |
| 99 | +static PyObject *py_ue_fraw_mesh_set_wedge_indices(ue_PyFRawMesh *self, PyObject * args) { |
| 100 | + PyObject *data; |
| 101 | + if (!PyArg_ParseTuple(args, "O:set_wedge_indices", &data)) { |
| 102 | + return nullptr; |
| 103 | + } |
| 104 | + |
| 105 | + PyObject *iter = PyObject_GetIter(data); |
| 106 | + if (!iter) |
| 107 | + return PyErr_Format(PyExc_TypeError, "argument is not an iterable"); |
| 108 | + |
| 109 | + TArray<uint32> indices; |
| 110 | + |
| 111 | + for (;;) { |
| 112 | + PyObject *item = PyIter_Next(iter); |
| 113 | + if (!item) |
| 114 | + break; |
| 115 | + if (!PyNumber_Check(item)) |
| 116 | + return PyErr_Format(PyExc_Exception, "argument iterable does not contains only numbers"); |
| 117 | + PyObject *py_value = PyNumber_Long(item); |
| 118 | + uint32 i = PyLong_AsUnsignedLong(py_value); |
| 119 | + Py_DECREF(py_value); |
| 120 | + |
| 121 | + indices.Add(i); |
| 122 | + } |
| 123 | + |
| 124 | + |
| 125 | + self->raw_mesh->WedgeIndices = indices; |
| 126 | + |
| 127 | + Py_DECREF(iter); |
| 128 | + |
| 129 | + Py_RETURN_NONE; |
| 130 | +} |
| 131 | + |
| 132 | +static PyObject *py_ue_fraw_mesh_save_to_static_mesh_source_model(ue_PyFRawMesh *self, PyObject * args) { |
| 133 | + PyObject *model; |
| 134 | + if (!PyArg_ParseTuple(args, "O:save_to_static_mesh_source_model", &model)) { |
| 135 | + return nullptr; |
| 136 | + } |
| 137 | + |
| 138 | + FStaticMeshSourceModel *source_model = ue_py_check_struct<FStaticMeshSourceModel>(model); |
| 139 | + if (!source_model) |
| 140 | + return PyErr_Format(PyExc_Exception, "argument is not a FStaticMeshSourceModel"); |
| 141 | + |
| 142 | + source_model->RawMeshBulkData->SaveRawMesh(*self->raw_mesh); |
| 143 | + |
| 144 | + Py_RETURN_NONE; |
| 145 | +} |
| 146 | + |
| 147 | +static PyObject *py_ue_fraw_mesh_get_wedge_position(ue_PyFRawMesh *self, PyObject * args) { |
| 148 | + int index ; |
| 149 | + if (!PyArg_ParseTuple(args, "i:get_wedge_position", &index)) { |
| 150 | + return nullptr; |
| 151 | + } |
| 152 | + |
| 153 | + if (index > self->raw_mesh->WedgeIndices.Num()-1 || index < 0) |
| 154 | + return PyErr_Format(PyExc_IndexError, "wedge index error"); |
| 155 | + |
| 156 | + FVector vec = self->raw_mesh->GetWedgePosition(index); |
| 157 | + |
| 158 | + return py_ue_new_fvector(vec); |
| 159 | +} |
| 160 | + |
| 161 | + |
| 162 | +static PyMethodDef ue_PyFRawMesh_methods[] = { |
| 163 | + { "set_vertex_positions", (PyCFunction)py_ue_fraw_mesh_set_vertex_positions, METH_VARARGS, "" }, |
| 164 | + { "set_wedge_indices", (PyCFunction)py_ue_fraw_mesh_set_wedge_indices, METH_VARARGS, "" }, |
| 165 | + { "set_wedge_tex_coords", (PyCFunction)py_ue_fraw_mesh_set_wedge_tex_coords, METH_VARARGS, "" }, |
| 166 | + { "get_wedge_position", (PyCFunction)py_ue_fraw_mesh_get_wedge_position, METH_VARARGS, "" }, |
| 167 | + { "save_to_static_mesh_source_model", (PyCFunction)py_ue_fraw_mesh_save_to_static_mesh_source_model, METH_VARARGS, "" }, |
| 168 | + { NULL } /* Sentinel */ |
| 169 | +}; |
| 170 | + |
| 171 | +static int ue_py_fraw_mesh_init(ue_PyFRawMesh *self, PyObject *args, PyObject *kwargs) { |
| 172 | + self->raw_mesh = new FRawMesh(); |
| 173 | + return 0; |
| 174 | +} |
| 175 | + |
| 176 | +static void ue_py_fraw_mesh_dealloc(ue_PyFRawMesh *self) { |
| 177 | + if (self->raw_mesh) |
| 178 | + delete(self->raw_mesh); |
| 179 | +#if PY_MAJOR_VERSION < 3 |
| 180 | + self->ob_type->tp_free((PyObject*)self); |
| 181 | +#else |
| 182 | + Py_TYPE(self)->tp_free((PyObject*)self); |
| 183 | +#endif |
| 184 | +} |
| 185 | + |
| 186 | +static PyTypeObject ue_PyFRawMeshType = { |
| 187 | + PyVarObject_HEAD_INIT(NULL, 0) |
| 188 | + "unreal_engine.FRawMesh", /* tp_name */ |
| 189 | + sizeof(ue_PyFRawMesh), /* tp_basicsize */ |
| 190 | + 0, /* tp_itemsize */ |
| 191 | + (destructor)ue_py_fraw_mesh_dealloc, /* tp_dealloc */ |
| 192 | + 0, /* tp_print */ |
| 193 | + 0, /* tp_getattr */ |
| 194 | + 0, /* tp_setattr */ |
| 195 | + 0, /* tp_reserved */ |
| 196 | + 0, /* tp_repr */ |
| 197 | + 0, /* tp_as_number */ |
| 198 | + 0, /* tp_as_sequence */ |
| 199 | + 0, /* tp_as_mapping */ |
| 200 | + 0, /* tp_hash */ |
| 201 | + 0, /* tp_call */ |
| 202 | + 0, /* tp_str */ |
| 203 | + 0, /* tp_getattro */ |
| 204 | + 0, /* tp_setattro */ |
| 205 | + 0, /* tp_as_buffer */ |
| 206 | + Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 207 | + "Unreal Engine FRawMesh", /* tp_doc */ |
| 208 | + 0, /* tp_traverse */ |
| 209 | + 0, /* tp_clear */ |
| 210 | + 0, /* tp_richcompare */ |
| 211 | + 0, /* tp_weaklistoffset */ |
| 212 | + 0, /* tp_iter */ |
| 213 | + 0, /* tp_iternext */ |
| 214 | + ue_PyFRawMesh_methods, /* tp_methods */ |
| 215 | + 0, /* tp_members */ |
| 216 | + 0, /* tp_getset */ |
| 217 | +}; |
| 218 | + |
| 219 | +void ue_python_init_fraw_mesh(PyObject *ue_module) { |
| 220 | + ue_PyFRawMeshType.tp_new = PyType_GenericNew;; |
| 221 | + ue_PyFRawMeshType.tp_init = (initproc)ue_py_fraw_mesh_init; |
| 222 | + if (PyType_Ready(&ue_PyFRawMeshType) < 0) |
| 223 | + return; |
| 224 | + |
| 225 | + Py_INCREF(&ue_PyFRawMeshType); |
| 226 | + PyModule_AddObject(ue_module, "FRawMesh", (PyObject *)&ue_PyFRawMeshType); |
| 227 | +} |
| 228 | + |
| 229 | +#endif |
0 commit comments