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