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