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