Blender V4.3
BPy_SShape.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2004-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include "BPy_SShape.h"
10
11#include "BPy_BBox.h"
12#include "BPy_Convert.h"
13#include "BPy_Id.h"
16
17#include "BLI_sys_types.h"
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23using namespace Freestyle;
24
26
27//-------------------MODULE INITIALIZATION--------------------------------
28int SShape_Init(PyObject *module)
29{
30 if (module == nullptr) {
31 return -1;
32 }
33
34 if (PyType_Ready(&SShape_Type) < 0) {
35 return -1;
36 }
37 PyModule_AddObjectRef(module, "SShape", (PyObject *)&SShape_Type);
38
39 return 0;
40}
41
42/*----------------------SShape methods ----------------------------*/
43
45 /* Wrap. */
46 SShape_doc,
47 "Class to define a feature shape. It is the gathering of feature\n"
48 "elements from an identified input shape.\n"
49 "\n"
50 ".. method:: __init__()\n"
51 " __init__(brother)\n"
52 "\n"
53 " Creates a :class:`SShape` class using either a default constructor or copy constructor.\n"
54 "\n"
55 " :arg brother: An SShape object.\n"
56 " :type brother: :class:`SShape`");
57
58static int SShape_init(BPy_SShape *self, PyObject *args, PyObject *kwds)
59{
60 static const char *kwlist[] = {"brother", nullptr};
61 PyObject *brother = nullptr;
62
63 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O!", (char **)kwlist, &SShape_Type, &brother)) {
64 return -1;
65 }
66 if (!brother) {
67 self->ss = new SShape();
68 }
69 else {
70 self->ss = new SShape(*(((BPy_SShape *)brother)->ss));
71 }
72 self->borrowed = false;
73 return 0;
74}
75
77{
78 if (self->ss && !self->borrowed) {
79 delete self->ss;
80 }
81 Py_TYPE(self)->tp_free((PyObject *)self);
82}
83
84static PyObject *SShape_repr(BPy_SShape *self)
85{
86 return PyUnicode_FromFormat("SShape - address: %p", self->ss);
87}
88
89static char SShape_add_edge_doc[] =
90 ".. method:: add_edge(edge)\n"
91 "\n"
92 " Adds an FEdge to the list of FEdges.\n"
93 "\n"
94 " :arg edge: An FEdge object.\n"
95 " :type edge: :class:`FEdge`\n";
96
97static PyObject *SShape_add_edge(BPy_SShape *self, PyObject *args, PyObject *kwds)
98{
99 static const char *kwlist[] = {"edge", nullptr};
100 PyObject *py_fe = nullptr;
101
102 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!", (char **)kwlist, &FEdge_Type, &py_fe)) {
103 return nullptr;
104 }
105 self->ss->AddEdge(((BPy_FEdge *)py_fe)->fe);
106 Py_RETURN_NONE;
107}
108
110 /* Wrap. */
111 SShape_add_vertex_doc,
112 ".. method:: add_vertex(vertex)\n"
113 "\n"
114 " Adds an SVertex to the list of SVertex of this Shape. The SShape\n"
115 " attribute of the SVertex is also set to this SShape.\n"
116 "\n"
117 " :arg vertex: An SVertex object.\n"
118 " :type vertex: :class:`SVertex`");
119
120static PyObject *SShape_add_vertex(BPy_SShape *self, PyObject *args, PyObject *kwds)
121{
122 static const char *kwlist[] = {"edge", nullptr};
123 PyObject *py_sv = nullptr;
124
125 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!", (char **)kwlist, &SVertex_Type, &py_sv)) {
126 return nullptr;
127 }
128 self->ss->AddNewVertex(((BPy_SVertex *)py_sv)->sv);
129 Py_RETURN_NONE;
130}
131
133 /* Wrap. */
134 SShape_compute_bbox_doc,
135 ".. method:: compute_bbox()\n"
136 "\n"
137 " Compute the bbox of the SShape.");
138
140{
141 self->ss->ComputeBBox();
142 Py_RETURN_NONE;
143}
144
145// const Material & material (uint i) const
146// const vector< Material > & materials () const
147// void SetMaterials (const vector< Material > &iMaterials)
148
149static PyMethodDef BPy_SShape_methods[] = {
150 {"add_edge", (PyCFunction)SShape_add_edge, METH_VARARGS | METH_KEYWORDS, SShape_add_edge_doc},
151 {"add_vertex",
152 (PyCFunction)SShape_add_vertex,
153 METH_VARARGS | METH_KEYWORDS,
154 SShape_add_vertex_doc},
155 {"compute_bbox", (PyCFunction)SShape_compute_bbox, METH_NOARGS, SShape_compute_bbox_doc},
156 {nullptr, nullptr, 0, nullptr},
157};
158
159/*----------------------SShape get/setters ----------------------------*/
160
162 /* Wrap. */
163 SShape_id_doc,
164 "The Id of this SShape.\n"
165 "\n"
166 ":type: :class:`Id`");
167
168static PyObject *SShape_id_get(BPy_SShape *self, void * /*closure*/)
169{
170 Id id(self->ss->getId());
171 return BPy_Id_from_Id(id); // return a copy
172}
173
174static int SShape_id_set(BPy_SShape *self, PyObject *value, void * /*closure*/)
175{
176 if (!BPy_Id_Check(value)) {
177 PyErr_SetString(PyExc_TypeError, "value must be an Id");
178 return -1;
179 }
180 self->ss->setId(*(((BPy_Id *)value)->id));
181 return 0;
182}
183
185 /* Wrap. */
186 SShape_name_doc,
187 "The name of the SShape.\n"
188 "\n"
189 ":type: str");
190
191static PyObject *SShape_name_get(BPy_SShape *self, void * /*closure*/)
192{
193 return PyUnicode_FromString(self->ss->getName().c_str());
194}
195
196static int SShape_name_set(BPy_SShape *self, PyObject *value, void * /*closure*/)
197{
198 if (!PyUnicode_Check(value)) {
199 PyErr_SetString(PyExc_TypeError, "value must be a string");
200 return -1;
201 }
202 const char *name = PyUnicode_AsUTF8(value);
203 self->ss->setName(name);
204 return 0;
205}
206
208 /* Wrap. */
209 SShape_bbox_doc,
210 "The bounding box of the SShape.\n"
211 "\n"
212 ":type: :class:`BBox`");
213
214static PyObject *SShape_bbox_get(BPy_SShape *self, void * /*closure*/)
215{
216 BBox<Vec3r> bb(self->ss->bbox());
217 return BPy_BBox_from_BBox(bb); // return a copy
218}
219
220static int SShape_bbox_set(BPy_SShape *self, PyObject *value, void * /*closure*/)
221{
222 if (!BPy_BBox_Check(value)) {
223 PyErr_SetString(PyExc_TypeError, "value must be a BBox");
224 return -1;
225 }
226 self->ss->setBBox(*(((BPy_BBox *)value)->bb));
227 return 0;
228}
229
231 /* Wrap. */
232 SShape_vertices_doc,
233 "The list of vertices constituting this SShape.\n"
234 "\n"
235 ":type: List of :class:`SVertex`");
236
237static PyObject *SShape_vertices_get(BPy_SShape *self, void * /*closure*/)
238{
239
240 vector<SVertex *> vertices = self->ss->getVertexList();
242 PyObject *py_vertices = PyList_New(vertices.size());
243 uint i = 0;
244
245 for (it = vertices.begin(); it != vertices.end(); it++) {
246 PyList_SET_ITEM(py_vertices, i++, BPy_SVertex_from_SVertex(*(*it)));
247 }
248
249 return py_vertices;
250}
251
253 /* Wrap. */
254 SShape_edges_doc,
255 "The list of edges constituting this SShape.\n"
256 "\n"
257 ":type: List of :class:`FEdge`");
258
259static PyObject *SShape_edges_get(BPy_SShape *self, void * /*closure*/)
260{
261
262 vector<FEdge *> edges = self->ss->getEdgeList();
264 PyObject *py_edges = PyList_New(edges.size());
265 uint i = 0;
266
267 for (it = edges.begin(); it != edges.end(); it++) {
268 PyList_SET_ITEM(py_edges, i++, Any_BPy_FEdge_from_FEdge(*(*it)));
269 }
270
271 return py_edges;
272}
273
274static PyGetSetDef BPy_SShape_getseters[] = {
275 {"id", (getter)SShape_id_get, (setter)SShape_id_set, SShape_id_doc, nullptr},
276 {"name", (getter)SShape_name_get, (setter)SShape_name_set, SShape_name_doc, nullptr},
277 {"bbox", (getter)SShape_bbox_get, (setter)SShape_bbox_set, SShape_bbox_doc, nullptr},
278 {"edges", (getter)SShape_edges_get, (setter) nullptr, SShape_edges_doc, nullptr},
279 {"vertices", (getter)SShape_vertices_get, (setter) nullptr, SShape_vertices_doc, nullptr},
280 {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
281};
282
283/*-----------------------BPy_SShape type definition ------------------------------*/
284
285PyTypeObject SShape_Type = {
286 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
287 /*tp_name*/ "SShape",
288 /*tp_basicsize*/ sizeof(BPy_SShape),
289 /*tp_itemsize*/ 0,
290 /*tp_dealloc*/ (destructor)SShape_dealloc,
291 /*tp_vectorcall_offset*/ 0,
292 /*tp_getattr*/ nullptr,
293 /*tp_setattr*/ nullptr,
294 /*tp_as_async*/ nullptr,
295 /*tp_repr*/ (reprfunc)SShape_repr,
296 /*tp_as_number*/ nullptr,
297 /*tp_as_sequence*/ nullptr,
298 /*tp_as_mapping*/ nullptr,
299 /*tp_hash*/ nullptr,
300 /*tp_call*/ nullptr,
301 /*tp_str*/ nullptr,
302 /*tp_getattro*/ nullptr,
303 /*tp_setattro*/ nullptr,
304 /*tp_as_buffer*/ nullptr,
305 /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
306 /*tp_doc*/ SShape_doc,
307 /*tp_traverse*/ nullptr,
308 /*tp_clear*/ nullptr,
309 /*tp_richcompare*/ nullptr,
310 /*tp_weaklistoffset*/ 0,
311 /*tp_iter*/ nullptr,
312 /*tp_iternext*/ nullptr,
313 /*tp_methods*/ BPy_SShape_methods,
314 /*tp_members*/ nullptr,
315 /*tp_getset*/ BPy_SShape_getseters,
316 /*tp_base*/ nullptr,
317 /*tp_dict*/ nullptr,
318 /*tp_descr_get*/ nullptr,
319 /*tp_descr_set*/ nullptr,
320 /*tp_dictoffset*/ 0,
321 /*tp_init*/ (initproc)SShape_init,
322 /*tp_alloc*/ nullptr,
323 /*tp_new*/ PyType_GenericNew,
324};
325
327
328#ifdef __cplusplus
329}
330#endif
unsigned int uint
#define BPy_BBox_Check(v)
Definition BPy_BBox.h:26
PyObject * BPy_Id_from_Id(Id &id)
PyObject * Any_BPy_FEdge_from_FEdge(FEdge &fe)
PyObject * BPy_SVertex_from_SVertex(SVertex &sv)
PyObject * BPy_BBox_from_BBox(const BBox< Vec3r > &bb)
PyTypeObject FEdge_Type
#define BPy_Id_Check(v)
Definition BPy_Id.h:27
PyDoc_STRVAR(SShape_doc, "Class to define a feature shape. It is the gathering of feature\n" "elements from an identified input shape.\n" "\n" ".. method:: __init__()\n" " __init__(brother)\n" "\n" " Creates a :class:`SShape` class using either a default constructor or copy constructor.\n" "\n" " :arg brother: An SShape object.\n" " :type brother: :class:`SShape`")
static char SShape_add_edge_doc[]
static PyObject * SShape_add_edge(BPy_SShape *self, PyObject *args, PyObject *kwds)
static void SShape_dealloc(BPy_SShape *self)
static int SShape_init(BPy_SShape *self, PyObject *args, PyObject *kwds)
static PyGetSetDef BPy_SShape_getseters[]
static PyObject * SShape_compute_bbox(BPy_SShape *self)
static int SShape_bbox_set(BPy_SShape *self, PyObject *value, void *)
int SShape_Init(PyObject *module)
static PyMethodDef BPy_SShape_methods[]
static PyObject * SShape_edges_get(BPy_SShape *self, void *)
static PyObject * SShape_bbox_get(BPy_SShape *self, void *)
static PyObject * SShape_repr(BPy_SShape *self)
static PyObject * SShape_vertices_get(BPy_SShape *self, void *)
static PyObject * SShape_id_get(BPy_SShape *self, void *)
static PyObject * SShape_add_vertex(BPy_SShape *self, PyObject *args, PyObject *kwds)
static int SShape_name_set(BPy_SShape *self, PyObject *value, void *)
static int SShape_id_set(BPy_SShape *self, PyObject *value, void *)
PyTypeObject SShape_Type
static PyObject * SShape_name_get(BPy_SShape *self, void *)
PyTypeObject SVertex_Type
PyObject * self
inherits from class Rep
Definition AppCanvas.cpp:20
static struct PyModuleDef module
Definition python.cpp:991