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