Blender V5.3
BPy_SShape.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2004-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BPy_SShape.h"
10
11#include "BPy_BBox.h"
12#include "BPy_Convert.h"
13#include "BPy_Id.h"
16
17#include "BLI_sys_types.hh"
18
19#include "../generic/py_capi_utils.hh"
20
21using namespace Freestyle;
22
24
25//-------------------MODULE INITIALIZATION--------------------------------
26int SShape_Init(PyObject *module)
27{
28 if (module == nullptr) {
29 return -1;
30 }
31
32 if (PyType_Ready(&SShape_Type) < 0) {
33 return -1;
34 }
35 PyModule_AddObjectRef(module, "SShape", (PyObject *)&SShape_Type);
36
37 return 0;
38}
39
40/*----------------------SShape methods ----------------------------*/
41
43 /* Wrap. */
44 SShape_doc,
45 "Class to define a feature shape. It is the gathering of feature\n"
46 "elements from an identified input shape.\n"
47 "\n"
48 ".. method:: __init__(*args)\n"
49 "\n"
50 " Accepted call signatures:\n"
51 "\n"
52 " - ``__init__()``\n"
53 " - ``__init__(brother)``\n"
54 "\n"
55 " Creates a :class:`SShape` class using either a default constructor or copy constructor.\n"
56 "\n"
57 " :param brother: An SShape object.\n"
58 " :type brother: :class:`SShape`\n");
59static int SShape_init(BPy_SShape *self, PyObject *args, PyObject *kwds)
60{
61 static const char *kwlist[] = {"brother", nullptr};
62 PyObject *brother = nullptr;
63
64 if (!PyArg_ParseTupleAndKeywords(args,
65 kwds,
66 "|" /* Optional arguments. */
67 "O!" /* `brother` */
68 ":__init__",
69 (char **)kwlist,
71 &brother))
72 {
73 return -1;
74 }
75 if (!brother) {
76 self->ss = new SShape();
77 }
78 else {
79 self->ss = new SShape(*(((BPy_SShape *)brother)->ss));
80 }
81 self->borrowed = false;
82 return 0;
83}
84
85static void SShape_dealloc(BPy_SShape *self)
86{
87 if (self->ss && !self->borrowed) {
88 delete self->ss;
89 }
90 Py_TYPE(self)->tp_free((PyObject *)self);
91}
92
93static PyObject *SShape_repr(BPy_SShape *self)
94{
95 return PyUnicode_FromFormat("SShape - address: %p", self->ss);
96}
97
98static char SShape_add_edge_doc[] =
99 ".. method:: add_edge(edge)\n"
100 "\n"
101 " Adds an FEdge to the list of FEdges.\n"
102 "\n"
103 " :param edge: An FEdge object.\n"
104 " :type edge: :class:`FEdge`\n";
105
106static PyObject *SShape_add_edge(BPy_SShape *self, PyObject *args, PyObject *kwds)
107{
108 static const char *kwlist[] = {"edge", nullptr};
109 PyObject *py_fe = nullptr;
110
111 if (!PyArg_ParseTupleAndKeywords(args,
112 kwds,
113 "O!" /* `edge` */
114 ":add_edge",
115 (char **)kwlist,
116 &FEdge_Type,
117 &py_fe))
118 {
119 return nullptr;
120 }
121 self->ss->AddEdge(((BPy_FEdge *)py_fe)->fe);
122 Py_RETURN_NONE;
123}
124
126 /* Wrap. */
127 SShape_add_vertex_doc,
128 ".. method:: add_vertex(vertex)\n"
129 "\n"
130 " Adds an SVertex to the list of SVertex of this Shape. The SShape\n"
131 " attribute of the SVertex is also set to this SShape.\n"
132 "\n"
133 " :param vertex: An SVertex object.\n"
134 " :type vertex: :class:`SVertex`\n");
135static PyObject *SShape_add_vertex(BPy_SShape *self, PyObject *args, PyObject *kwds)
136{
137 static const char *kwlist[] = {"edge", nullptr};
138 PyObject *py_sv = nullptr;
139
140 if (!PyArg_ParseTupleAndKeywords(args,
141 kwds,
142 "O!" /* `edge` */
143 ":add_vertex",
144 (char **)kwlist,
146 &py_sv))
147 {
148 return nullptr;
149 }
150 self->ss->AddNewVertex(((BPy_SVertex *)py_sv)->sv);
151 Py_RETURN_NONE;
152}
153
155 /* Wrap. */
156 SShape_compute_bbox_doc,
157 ".. method:: compute_bbox()\n"
158 "\n"
159 " Compute the bbox of the SShape.\n");
160static PyObject *SShape_compute_bbox(BPy_SShape *self)
161{
162 self->ss->ComputeBBox();
163 Py_RETURN_NONE;
164}
165
166// const Material & material (uint i) const
167// const vector< Material > & materials () const
168// void SetMaterials (const vector< Material > &iMaterials)
169
170#ifdef __GNUC__
171# ifdef __clang__
172# pragma clang diagnostic push
173# pragma clang diagnostic ignored "-Wcast-function-type"
174# else
175# pragma GCC diagnostic push
176# pragma GCC diagnostic ignored "-Wcast-function-type"
177# endif
178#endif
179
180static PyMethodDef BPy_SShape_methods[] = {
181 {"add_edge", (PyCFunction)SShape_add_edge, METH_VARARGS | METH_KEYWORDS, SShape_add_edge_doc},
182 {"add_vertex",
183 (PyCFunction)SShape_add_vertex,
184 METH_VARARGS | METH_KEYWORDS,
185 SShape_add_vertex_doc},
186 {"compute_bbox", (PyCFunction)SShape_compute_bbox, METH_NOARGS, SShape_compute_bbox_doc},
187 {nullptr, nullptr, 0, nullptr},
188};
189
190#ifdef __GNUC__
191# ifdef __clang__
192# pragma clang diagnostic pop
193# else
194# pragma GCC diagnostic pop
195# endif
196#endif
197
198/*----------------------SShape get/setters ----------------------------*/
199
201 /* Wrap. */
202 SShape_id_doc,
203 "The Id of this SShape.\n"
204 "\n"
205 ":type: :class:`Id`\n");
206static PyObject *SShape_id_get(BPy_SShape *self, void * /*closure*/)
207{
208 Id id(self->ss->getId());
209 return BPy_Id_from_Id(id); // return a copy
210}
211
212static int SShape_id_set(BPy_SShape *self, PyObject *value, void * /*closure*/)
213{
214 if (!BPy_Id_Check(value)) {
215 PyErr_SetString(PyExc_TypeError, "value must be an Id");
216 return -1;
217 }
218 self->ss->setId(*(((BPy_Id *)value)->id));
219 return 0;
220}
221
223 /* Wrap. */
224 SShape_name_doc,
225 "The name of the SShape.\n"
226 "\n"
227 ":type: str\n");
228static PyObject *SShape_name_get(BPy_SShape *self, void * /*closure*/)
229{
231}
232
233static int SShape_name_set(BPy_SShape *self, PyObject *value, void * /*closure*/)
234{
235 if (!PyUnicode_Check(value)) {
236 PyErr_SetString(PyExc_TypeError, "value must be a string");
237 return -1;
238 }
239 const char *name = PyUnicode_AsUTF8(value);
240 self->ss->setName(name);
241 return 0;
242}
243
245 /* Wrap. */
246 SShape_bbox_doc,
247 "The bounding box of the SShape.\n"
248 "\n"
249 ":type: :class:`BBox`\n");
250static PyObject *SShape_bbox_get(BPy_SShape *self, void * /*closure*/)
251{
252 BBox<Vec3r> bb(self->ss->bbox());
253 return BPy_BBox_from_BBox(bb); // return a copy
254}
255
256static int SShape_bbox_set(BPy_SShape *self, PyObject *value, void * /*closure*/)
257{
258 if (!BPy_BBox_Check(value)) {
259 PyErr_SetString(PyExc_TypeError, "value must be a BBox");
260 return -1;
261 }
262 self->ss->setBBox(*(((BPy_BBox *)value)->bb));
263 return 0;
264}
265
267 /* Wrap. */
268 SShape_vertices_doc,
269 "The list of vertices constituting this SShape.\n"
270 "\n"
271 ":type: list[:class:`SVertex`]\n");
272static PyObject *SShape_vertices_get(BPy_SShape *self, void * /*closure*/)
273{
274
275 vector<SVertex *> vertices = self->ss->getVertexList();
277 PyObject *py_vertices = PyList_New(vertices.size());
278 uint i = 0;
279
280 for (it = vertices.begin(); it != vertices.end(); it++) {
281 PyList_SET_ITEM(py_vertices, i++, BPy_SVertex_from_SVertex(*(*it)));
282 }
283
284 return py_vertices;
285}
286
288 /* Wrap. */
289 SShape_edges_doc,
290 "The list of edges constituting this SShape.\n"
291 "\n"
292 ":type: list[:class:`FEdge`]\n");
293static PyObject *SShape_edges_get(BPy_SShape *self, void * /*closure*/)
294{
295
296 vector<FEdge *> edges = self->ss->getEdgeList();
298 PyObject *py_edges = PyList_New(edges.size());
299 uint i = 0;
300
301 for (it = edges.begin(); it != edges.end(); it++) {
302 PyList_SET_ITEM(py_edges, i++, Any_BPy_FEdge_from_FEdge(*(*it)));
303 }
304
305 return py_edges;
306}
307
308static PyGetSetDef BPy_SShape_getseters[] = {
309 {"id", (getter)SShape_id_get, (setter)SShape_id_set, SShape_id_doc, nullptr},
310 {"name", (getter)SShape_name_get, (setter)SShape_name_set, SShape_name_doc, nullptr},
311 {"bbox", (getter)SShape_bbox_get, (setter)SShape_bbox_set, SShape_bbox_doc, nullptr},
312 {"edges", (getter)SShape_edges_get, (setter) nullptr, SShape_edges_doc, nullptr},
313 {"vertices", (getter)SShape_vertices_get, (setter) nullptr, SShape_vertices_doc, nullptr},
314 {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
315};
316
317/*-----------------------BPy_SShape type definition ------------------------------*/
318
319PyTypeObject SShape_Type = {
320 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
321 /*tp_name*/ "SShape",
322 /*tp_basicsize*/ sizeof(BPy_SShape),
323 /*tp_itemsize*/ 0,
324 /*tp_dealloc*/ (destructor)SShape_dealloc,
325 /*tp_vectorcall_offset*/ 0,
326 /*tp_getattr*/ nullptr,
327 /*tp_setattr*/ nullptr,
328 /*tp_as_async*/ nullptr,
329 /*tp_repr*/ (reprfunc)SShape_repr,
330 /*tp_as_number*/ nullptr,
331 /*tp_as_sequence*/ nullptr,
332 /*tp_as_mapping*/ nullptr,
333 /*tp_hash*/ nullptr,
334 /*tp_call*/ nullptr,
335 /*tp_str*/ nullptr,
336 /*tp_getattro*/ nullptr,
337 /*tp_setattro*/ nullptr,
338 /*tp_as_buffer*/ nullptr,
339 /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
340 /*tp_doc*/ SShape_doc,
341 /*tp_traverse*/ nullptr,
342 /*tp_clear*/ nullptr,
343 /*tp_richcompare*/ nullptr,
344 /*tp_weaklistoffset*/ 0,
345 /*tp_iter*/ nullptr,
346 /*tp_iternext*/ nullptr,
347 /*tp_methods*/ BPy_SShape_methods,
348 /*tp_members*/ nullptr,
349 /*tp_getset*/ BPy_SShape_getseters,
350 /*tp_base*/ nullptr,
351 /*tp_dict*/ nullptr,
352 /*tp_descr_get*/ nullptr,
353 /*tp_descr_set*/ nullptr,
354 /*tp_dictoffset*/ 0,
355 /*tp_init*/ (initproc)SShape_init,
356 /*tp_alloc*/ nullptr,
357 /*tp_new*/ PyType_GenericNew,
358};
359
#define BPy_BBox_Check(v)
Definition BPy_BBox.h:22
PyObject * BPy_Id_from_Id(Id &id)
PyObject * Any_BPy_FEdge_from_FEdge(FEdge &fe)
PyObject * BPy_SVertex_from_SVertex(SVertex &sv)
PyObject * BPy_BBox_from_BBox(const BBox< Vec3r > &bb)
PyTypeObject FEdge_Type
PyDoc_STRVAR(FalseBP1D___doc__, "Class hierarchy: :class:`freestyle.types.BinaryPredicate1D` > :class:`FalseBP1D`\n" "\n" ".. method:: __call__(inter1, inter2)\n" "\n" " Always returns false.\n" "\n" " :param inter1: The first Interface1D object.\n" " :type inter1: :class:`freestyle.types.Interface1D`\n" " :param inter2: The second Interface1D object.\n" " :type inter2: :class:`freestyle.types.Interface1D`\n" " :return: False.\n" " :rtype: bool\n")
#define BPy_Id_Check(v)
Definition BPy_Id.h:23
static char SShape_add_edge_doc[]
static PyObject * SShape_add_edge(BPy_SShape *self, PyObject *args, PyObject *kwds)
static void SShape_dealloc(BPy_SShape *self)
static int SShape_init(BPy_SShape *self, PyObject *args, PyObject *kwds)
static PyGetSetDef BPy_SShape_getseters[]
static PyObject * SShape_compute_bbox(BPy_SShape *self)
static int SShape_bbox_set(BPy_SShape *self, PyObject *value, void *)
int SShape_Init(PyObject *module)
static PyMethodDef BPy_SShape_methods[]
static PyObject * SShape_edges_get(BPy_SShape *self, void *)
static PyObject * SShape_bbox_get(BPy_SShape *self, void *)
static PyObject * SShape_repr(BPy_SShape *self)
static PyObject * SShape_vertices_get(BPy_SShape *self, void *)
static PyObject * SShape_id_get(BPy_SShape *self, void *)
static PyObject * SShape_add_vertex(BPy_SShape *self, PyObject *args, PyObject *kwds)
static int SShape_name_set(BPy_SShape *self, PyObject *value, void *)
static int SShape_id_set(BPy_SShape *self, PyObject *value, void *)
PyTypeObject SShape_Type
static PyObject * SShape_name_get(BPy_SShape *self, void *)
PyTypeObject SVertex_Type
void setId(Id id)
vector< SVertex * > & getVertexList()
const BBox< Vec3r > & bbox()
void setBBox(const BBox< Vec3r > &iBBox)
void AddNewVertex(SVertex *iv)
void setName(const string &name)
vector< FEdge * > & getEdgeList()
const string & getName() const
void AddEdge(FEdge *iEdge)
inherits from class Rep
Definition AppCanvas.cpp:20
PyObject * PyC_UnicodeFromStdStr(const std::string &str)
PyObject_HEAD Freestyle::SShape * ss
Definition BPy_SShape.h:26
bool borrowed
Definition BPy_SShape.h:27