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