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