Blender V5.0
BPy_ViewShape.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2004-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BPy_ViewShape.h"
10
11#include "BPy_Convert.h"
12#include "BPy_SShape.h"
15
16#include "BLI_sys_types.h"
17
18#include "../generic/py_capi_utils.hh"
19
20using namespace Freestyle;
21
23
24//-------------------MODULE INITIALIZATION--------------------------------
25
26int ViewShape_Init(PyObject *module)
27{
28 if (module == nullptr) {
29 return -1;
30 }
31
32 if (PyType_Ready(&ViewShape_Type) < 0) {
33 return -1;
34 }
35 PyModule_AddObjectRef(module, "ViewShape", (PyObject *)&ViewShape_Type);
36
37 return 0;
38}
39
40/*----------------------ViewShape methods ----------------------------*/
41
43 /* Wrap. */
44 ViewShape_doc,
45 "Class gathering the elements of the ViewMap (i.e., :class:`ViewVertex`\n"
46 "and :class:`ViewEdge`) that are issued from the same input shape.\n"
47 "\n"
48 ".. method:: __init__()\n"
49 " __init__(brother)\n"
50 " __init__(sshape)\n"
51 "\n"
52 " Builds a :class:`ViewShape` using the default constructor,\n"
53 " copy constructor, or from a :class:`SShape`.\n"
54 "\n"
55 " :arg brother: A ViewShape object.\n"
56 " :type brother: :class:`ViewShape`\n"
57 " :arg sshape: An SShape object.\n"
58 " :type sshape: :class:`SShape`\n");
59static int ViewShape_init(BPy_ViewShape *self, PyObject *args, PyObject *kwds)
60{
61 static const char *kwlist_1[] = {"brother", nullptr};
62 static const char *kwlist_2[] = {"sshape", nullptr};
63 PyObject *obj = nullptr;
64
65 if (PyArg_ParseTupleAndKeywords(args, kwds, "|O!", (char **)kwlist_1, &ViewShape_Type, &obj)) {
66 if (!obj) {
67 self->vs = new ViewShape();
68 self->py_ss = nullptr;
69 }
70 else {
71 self->vs = new ViewShape(*(((BPy_ViewShape *)obj)->vs));
72 self->py_ss = ((BPy_ViewShape *)obj)->py_ss;
73 }
74 }
75 else if ((void)PyErr_Clear(),
76 PyArg_ParseTupleAndKeywords(args, kwds, "O!", (char **)kwlist_2, &SShape_Type, &obj))
77 {
78 BPy_SShape *py_ss = (BPy_SShape *)obj;
79 self->vs = new ViewShape(py_ss->ss);
80 self->py_ss = (!py_ss->borrowed) ? py_ss : nullptr;
81 }
82 else {
83 PyErr_SetString(PyExc_TypeError, "invalid argument(s)");
84 return -1;
85 }
86 self->borrowed = false;
87 Py_XINCREF(self->py_ss);
88 return 0;
89}
90
92{
93 if (self->py_ss) {
94 self->vs->setSShape((SShape *)nullptr);
95 Py_DECREF(self->py_ss);
96 }
97 if (self->vs && !self->borrowed) {
98 delete self->vs;
99 }
100 Py_TYPE(self)->tp_free((PyObject *)self);
101}
102
104{
105 return PyUnicode_FromFormat("ViewShape - address: %p", self->vs);
106}
107
109 /* Wrap. */
110 ViewShape_add_edge_doc,
111 ".. method:: add_edge(edge)\n"
112 "\n"
113 " Adds a ViewEdge to the list of ViewEdge objects.\n"
114 "\n"
115 " :arg edge: A ViewEdge object.\n"
116 " :type edge: :class:`ViewEdge`\n");
117static PyObject *ViewShape_add_edge(BPy_ViewShape *self, PyObject *args, PyObject *kwds)
118{
119 static const char *kwlist[] = {"edge", nullptr};
120 PyObject *py_ve = nullptr;
121
122 if (PyArg_ParseTupleAndKeywords(args, kwds, "O!", (char **)kwlist, &ViewEdge_Type, &py_ve)) {
123 return nullptr;
124 }
125 self->vs->AddEdge(((BPy_ViewEdge *)py_ve)->ve);
126 Py_RETURN_NONE;
127}
128
130 /* Wrap. */
131 ViewShape_add_vertex_doc,
132 ".. method:: add_vertex(vertex)\n"
133 "\n"
134 " Adds a ViewVertex to the list of the ViewVertex objects.\n"
135 "\n"
136 " :arg vertex: A ViewVertex object.\n"
137 " :type vertex: :class:`ViewVertex`\n");
138static PyObject *ViewShape_add_vertex(BPy_ViewShape *self, PyObject *args, PyObject *kwds)
139{
140 static const char *kwlist[] = {"vertex", nullptr};
141 PyObject *py_vv = nullptr;
142
143 if (PyArg_ParseTupleAndKeywords(args, kwds, "O!", (char **)kwlist, &ViewVertex_Type, &py_vv)) {
144 return nullptr;
145 }
146 self->vs->AddVertex(((BPy_ViewVertex *)py_vv)->vv);
147 Py_RETURN_NONE;
148}
149
150// virtual ViewShape *duplicate()
151
152#ifdef __GNUC__
153# ifdef __clang__
154# pragma clang diagnostic push
155# pragma clang diagnostic ignored "-Wcast-function-type"
156# else
157# pragma GCC diagnostic push
158# pragma GCC diagnostic ignored "-Wcast-function-type"
159# endif
160#endif
161
162static PyMethodDef BPy_ViewShape_methods[] = {
163 {"add_edge",
164 (PyCFunction)ViewShape_add_edge,
165 METH_VARARGS | METH_KEYWORDS,
166 ViewShape_add_edge_doc},
167 {"add_vertex",
168 (PyCFunction)ViewShape_add_vertex,
169 METH_VARARGS | METH_KEYWORDS,
170 ViewShape_add_vertex_doc},
171 {nullptr, nullptr, 0, nullptr},
172};
173
174#ifdef __GNUC__
175# ifdef __clang__
176# pragma clang diagnostic pop
177# else
178# pragma GCC diagnostic pop
179# endif
180#endif
181
182/*----------------------ViewShape get/setters ----------------------------*/
183
185 /* Wrap. */
186 ViewShape_sshape_doc,
187 "The SShape on top of which this ViewShape is built.\n"
188 "\n"
189 ":type: :class:`SShape`\n");
190static PyObject *ViewShape_sshape_get(BPy_ViewShape *self, void * /*closure*/)
191{
192 SShape *ss = self->vs->sshape();
193 if (!ss) {
194 Py_RETURN_NONE;
195 }
196 return BPy_SShape_from_SShape(*ss);
197}
198
199static int ViewShape_sshape_set(BPy_ViewShape *self, PyObject *value, void * /*closure*/)
200{
201 if (!BPy_SShape_Check(value)) {
202 PyErr_SetString(PyExc_TypeError, "value must be an SShape");
203 return -1;
204 }
205 BPy_SShape *py_ss = (BPy_SShape *)value;
206 self->vs->setSShape(py_ss->ss);
207 if (self->py_ss) {
208 Py_DECREF(self->py_ss);
209 }
210 if (!py_ss->borrowed) {
211 self->py_ss = py_ss;
212 Py_INCREF(self->py_ss);
213 }
214 return 0;
215}
216
218 /* Wrap. */
219 ViewShape_vertices_doc,
220 "The list of ViewVertex objects contained in this ViewShape.\n"
221 "\n"
222 ":type: List of :class:`ViewVertex`\n");
223static PyObject *ViewShape_vertices_get(BPy_ViewShape *self, void * /*closure*/)
224{
225 vector<ViewVertex *> vertices = self->vs->vertices();
227 PyObject *py_vertices = PyList_New(vertices.size());
228 uint i = 0;
229
230 for (it = vertices.begin(); it != vertices.end(); it++) {
231 PyList_SET_ITEM(py_vertices, i++, Any_BPy_ViewVertex_from_ViewVertex(*(*it)));
232 }
233 return py_vertices;
234}
235
236static int ViewShape_vertices_set(BPy_ViewShape *self, PyObject *value, void * /*closure*/)
237{
238 PyObject *item;
240
241 if (!PyList_Check(value)) {
242 PyErr_SetString(PyExc_TypeError, "value must be a list of ViewVertex objects");
243 return -1;
244 }
245
246 v.reserve(PyList_GET_SIZE(value));
247 for (uint i = 0; i < PyList_GET_SIZE(value); i++) {
248 item = PyList_GET_ITEM(value, i);
249 if (BPy_ViewVertex_Check(item)) {
250 v.push_back(((BPy_ViewVertex *)item)->vv);
251 }
252 else {
253 PyErr_SetString(PyExc_TypeError, "value must be a list of ViewVertex objects");
254 return -1;
255 }
256 }
257 self->vs->setVertices(v);
258 return 0;
259}
260
262 /* Wrap. */
263 ViewShape_edges_doc,
264 "The list of ViewEdge objects contained in this ViewShape.\n"
265 "\n"
266 ":type: List of :class:`ViewEdge`\n");
267static PyObject *ViewShape_edges_get(BPy_ViewShape *self, void * /*closure*/)
268{
269 vector<ViewEdge *> edges = self->vs->edges();
271 PyObject *py_edges = PyList_New(edges.size());
272 uint i = 0;
273
274 for (it = edges.begin(); it != edges.end(); it++) {
275 PyList_SET_ITEM(py_edges, i++, BPy_ViewEdge_from_ViewEdge(*(*it)));
276 }
277 return py_edges;
278}
279
280static int ViewShape_edges_set(BPy_ViewShape *self, PyObject *value, void * /*closure*/)
281{
282 PyObject *item;
284
285 if (!PyList_Check(value)) {
286 PyErr_SetString(PyExc_TypeError, "value must be a list of ViewEdge objects");
287 return -1;
288 }
289
290 v.reserve(PyList_GET_SIZE(value));
291 for (int i = 0; i < PyList_GET_SIZE(value); i++) {
292 item = PyList_GET_ITEM(value, i);
293 if (BPy_ViewEdge_Check(item)) {
294 v.push_back(((BPy_ViewEdge *)item)->ve);
295 }
296 else {
297 PyErr_SetString(PyExc_TypeError, "argument must be list of ViewEdge objects");
298 return -1;
299 }
300 }
301 self->vs->setEdges(v);
302 return 0;
303}
304
306 /* Wrap. */
307 ViewShape_name_doc,
308 "The name of the ViewShape.\n"
309 "\n"
310 ":type: str\n");
311static PyObject *ViewShape_name_get(BPy_ViewShape *self, void * /*closure*/)
312{
313 return PyC_UnicodeFromStdStr(self->vs->getName());
314}
315
317 /* Wrap. */
318 ViewShape_library_path_doc,
319 "The library path of the ViewShape.\n"
320 "\n"
321 ":type: str, or None if the ViewShape is not part of a library.\n");
322static PyObject *ViewShape_library_path_get(BPy_ViewShape *self, void * /*closure*/)
323{
324 return PyC_UnicodeFromStdStr(self->vs->getLibraryPath());
325}
326
328 /* Wrap. */
329 ViewShape_id_doc,
330 "The Id of this ViewShape.\n"
331 "\n"
332 ":type: :class:`Id`\n");
333static PyObject *ViewShape_id_get(BPy_ViewShape *self, void * /*closure*/)
334{
335 Id id(self->vs->getId());
336 return BPy_Id_from_Id(id); // return a copy
337}
338
339static PyGetSetDef BPy_ViewShape_getseters[] = {
340 {"sshape",
341 (getter)ViewShape_sshape_get,
342 (setter)ViewShape_sshape_set,
343 ViewShape_sshape_doc,
344 nullptr},
345 {"vertices",
348 ViewShape_vertices_doc,
349 nullptr},
350 {"edges",
351 (getter)ViewShape_edges_get,
352 (setter)ViewShape_edges_set,
353 ViewShape_edges_doc,
354 nullptr},
355 {"name", (getter)ViewShape_name_get, (setter) nullptr, ViewShape_name_doc, nullptr},
356 {"library_path",
358 (setter) nullptr,
359 ViewShape_library_path_doc,
360 nullptr},
361 {"id", (getter)ViewShape_id_get, (setter) nullptr, ViewShape_id_doc, nullptr},
362 {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
363};
364
365/*-----------------------BPy_ViewShape type definition ------------------------------*/
366
367PyTypeObject ViewShape_Type = {
368 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
369 /*tp_name*/ "ViewShape",
370 /*tp_basicsize*/ sizeof(BPy_ViewShape),
371 /*tp_itemsize*/ 0,
372 /*tp_dealloc*/ (destructor)ViewShape_dealloc,
373 /*tp_vectorcall_offset*/ 0,
374 /*tp_getattr*/ nullptr,
375 /*tp_setattr*/ nullptr,
376 /*tp_as_async*/ nullptr,
377 /*tp_repr*/ (reprfunc)ViewShape_repr,
378 /*tp_as_number*/ nullptr,
379 /*tp_as_sequence*/ nullptr,
380 /*tp_as_mapping*/ nullptr,
381 /*tp_hash*/ nullptr,
382 /*tp_call*/ nullptr,
383 /*tp_str*/ nullptr,
384 /*tp_getattro*/ nullptr,
385 /*tp_setattro*/ nullptr,
386 /*tp_as_buffer*/ nullptr,
387 /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
388 /*tp_doc*/ ViewShape_doc,
389 /*tp_traverse*/ nullptr,
390 /*tp_clear*/ nullptr,
391 /*tp_richcompare*/ nullptr,
392 /*tp_weaklistoffset*/ 0,
393 /*tp_iter*/ nullptr,
394 /*tp_iternext*/ nullptr,
395 /*tp_methods*/ BPy_ViewShape_methods,
396 /*tp_members*/ nullptr,
397 /*tp_getset*/ BPy_ViewShape_getseters,
398 /*tp_base*/ nullptr,
399 /*tp_dict*/ nullptr,
400 /*tp_descr_get*/ nullptr,
401 /*tp_descr_set*/ nullptr,
402 /*tp_dictoffset*/ 0,
403 /*tp_init*/ (initproc)ViewShape_init,
404 /*tp_alloc*/ nullptr,
405 /*tp_new*/ PyType_GenericNew,
406};
407
unsigned int uint
PyObject * BPy_Id_from_Id(Id &id)
PyObject * BPy_SShape_from_SShape(SShape &ss)
PyObject * Any_BPy_ViewVertex_from_ViewVertex(ViewVertex &vv)
PyObject * BPy_ViewEdge_from_ViewEdge(ViewEdge &ve)
PyTypeObject SShape_Type
#define BPy_SShape_Check(v)
Definition BPy_SShape.h:21
PyTypeObject ViewEdge_Type
#define BPy_ViewEdge_Check(v)
static PyObject * ViewShape_add_edge(BPy_ViewShape *self, PyObject *args, PyObject *kwds)
PyTypeObject ViewShape_Type
static PyObject * ViewShape_add_vertex(BPy_ViewShape *self, PyObject *args, PyObject *kwds)
static PyObject * ViewShape_repr(BPy_ViewShape *self)
static PyObject * ViewShape_sshape_get(BPy_ViewShape *self, void *)
static PyObject * ViewShape_edges_get(BPy_ViewShape *self, void *)
static int ViewShape_init(BPy_ViewShape *self, PyObject *args, PyObject *kwds)
static PyObject * ViewShape_id_get(BPy_ViewShape *self, void *)
static PyObject * ViewShape_library_path_get(BPy_ViewShape *self, void *)
static int ViewShape_vertices_set(BPy_ViewShape *self, PyObject *value, void *)
static void ViewShape_dealloc(BPy_ViewShape *self)
PyDoc_STRVAR(ViewShape_doc, "Class gathering the elements of the ViewMap (i.e., :class:`ViewVertex`\n" "and :class:`ViewEdge`) that are issued from the same input shape.\n" "\n" ".. method:: __init__()\n" " __init__(brother)\n" " __init__(sshape)\n" "\n" " Builds a :class:`ViewShape` using the default constructor,\n" " copy constructor, or from a :class:`SShape`.\n" "\n" " :arg brother: A ViewShape object.\n" " :type brother: :class:`ViewShape`\n" " :arg sshape: An SShape object.\n" " :type sshape: :class:`SShape`\n")
static PyMethodDef BPy_ViewShape_methods[]
int ViewShape_Init(PyObject *module)
static PyObject * ViewShape_vertices_get(BPy_ViewShape *self, void *)
static int ViewShape_edges_set(BPy_ViewShape *self, PyObject *value, void *)
static PyObject * ViewShape_name_get(BPy_ViewShape *self, void *)
static PyGetSetDef BPy_ViewShape_getseters[]
static int ViewShape_sshape_set(BPy_ViewShape *self, PyObject *value, void *)
PyTypeObject ViewVertex_Type
#define BPy_ViewVertex_Check(v)
ATTR_WARN_UNUSED_RESULT const BMVert * v
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
Py_DECREF(oname)
PyObject_HEAD Freestyle::SShape * ss
Definition BPy_SShape.h:26
bool borrowed
Definition BPy_SShape.h:27
i
Definition text_draw.cc:230