Blender  V3.3
BPy_StrokeVertex.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include "BPy_StrokeVertex.h"
8 
9 #include "../../BPy_Convert.h"
10 #include "../../BPy_Freestyle.h"
11 #include "../../BPy_StrokeAttribute.h"
12 #include "../../Interface0D/BPy_SVertex.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 using namespace Freestyle;
19 
21 
22 //------------------------INSTANCE METHODS ----------------------------------
23 
25  StrokeVertex_doc,
26  "Class hierarchy: :class:`Interface0D` > :class:`CurvePoint` > :class:`StrokeVertex`\n"
27  "\n"
28  "Class to define a stroke vertex.\n"
29  "\n"
30  ".. method:: __init__()\n"
31  " __init__(brother)\n"
32  " __init__(first_vertex, second_vertex, t3d)\n"
33  " __init__(point)\n"
34  " __init__(svertex)\n"
35  " __init__(svertex, attribute)\n"
36  "\n"
37  " Builds a :class:`StrokeVertex` using the default constructor,\n"
38  " copy constructor, from 2 :class:`StrokeVertex` and an interpolation parameter,\n"
39  " from a CurvePoint, from a SVertex, or a :class:`SVertex`"
40  " and a :class:`StrokeAttribute` object.\n"
41  "\n"
42  " :arg brother: A StrokeVertex object.\n"
43  " :type brother: :class:`StrokeVertex`\n"
44  " :arg first_vertex: The first StrokeVertex.\n"
45  " :type first_vertex: :class:`StrokeVertex`\n"
46  " :arg second_vertex: The second StrokeVertex.\n"
47  " :type second_vertex: :class:`StrokeVertex`\n"
48  " :arg t3d: An interpolation parameter.\n"
49  " :type t3d: float\n"
50  " :arg point: A CurvePoint object.\n"
51  " :type point: :class:`CurvePoint`\n"
52  " :arg svertex: An SVertex object.\n"
53  " :type svertex: :class:`SVertex`\n"
54  " :arg svertex: An SVertex object.\n"
55  " :type svertex: :class:`SVertex`\n"
56  " :arg attribute: A StrokeAttribute object.\n"
57  " :type attribute: :class:`StrokeAttribute`");
58 
59 static int StrokeVertex_init(BPy_StrokeVertex *self, PyObject *args, PyObject *kwds)
60 {
61  static const char *kwlist_1[] = {"brother", nullptr};
62  static const char *kwlist_2[] = {"first_vertex", "second_vertex", "t3d", nullptr};
63  static const char *kwlist_3[] = {"point", nullptr};
64  static const char *kwlist_4[] = {"svertex", "attribute", nullptr};
65  PyObject *obj1 = nullptr, *obj2 = nullptr;
66  float t3d;
67 
68  if (PyArg_ParseTupleAndKeywords(
69  args, kwds, "|O!", (char **)kwlist_1, &StrokeVertex_Type, &obj1)) {
70  if (!obj1) {
71  self->sv = new StrokeVertex();
72  }
73  else {
74  if (!((BPy_StrokeVertex *)obj1)->sv) {
75  PyErr_SetString(PyExc_TypeError, "argument 1 is an invalid StrokeVertex object");
76  return -1;
77  }
78  self->sv = new StrokeVertex(*(((BPy_StrokeVertex *)obj1)->sv));
79  }
80  }
81  else if ((void)PyErr_Clear(),
82  PyArg_ParseTupleAndKeywords(args,
83  kwds,
84  "O!O!f",
85  (char **)kwlist_2,
87  &obj1,
89  &obj2,
90  &t3d)) {
91  StrokeVertex *sv1 = ((BPy_StrokeVertex *)obj1)->sv;
92  StrokeVertex *sv2 = ((BPy_StrokeVertex *)obj2)->sv;
93  if (!sv1 || (sv1->A() == nullptr && sv1->B() == nullptr)) {
94  PyErr_SetString(PyExc_TypeError, "argument 1 is an invalid StrokeVertex object");
95  return -1;
96  }
97  if (!sv2 || (sv2->A() == nullptr && sv2->B() == nullptr)) {
98  PyErr_SetString(PyExc_TypeError, "argument 2 is an invalid StrokeVertex object");
99  return -1;
100  }
101  self->sv = new StrokeVertex(sv1, sv2, t3d);
102  }
103  else if ((void)PyErr_Clear(),
104  PyArg_ParseTupleAndKeywords(
105  args, kwds, "O!", (char **)kwlist_3, &CurvePoint_Type, &obj1)) {
106  CurvePoint *cp = ((BPy_CurvePoint *)obj1)->cp;
107  if (!cp || cp->A() == nullptr || cp->B() == nullptr) {
108  PyErr_SetString(PyExc_TypeError, "argument 1 is an invalid CurvePoint object");
109  return -1;
110  }
111  self->sv = new StrokeVertex(cp);
112  }
113  else if ((void)PyErr_Clear(),
114  (void)(obj2 = nullptr),
115  PyArg_ParseTupleAndKeywords(args,
116  kwds,
117  "O!|O!",
118  (char **)kwlist_4,
119  &SVertex_Type,
120  &obj1,
122  &obj2)) {
123  if (!obj2) {
124  self->sv = new StrokeVertex(((BPy_SVertex *)obj1)->sv);
125  }
126  else {
127  self->sv = new StrokeVertex(((BPy_SVertex *)obj1)->sv, *(((BPy_StrokeAttribute *)obj2)->sa));
128  }
129  }
130  else {
131  PyErr_SetString(PyExc_TypeError, "invalid argument(s)");
132  return -1;
133  }
134  self->py_cp.cp = self->sv;
135  self->py_cp.py_if0D.if0D = self->sv;
136  self->py_cp.py_if0D.borrowed = false;
137  return 0;
138 }
139 
140 // real operator[] (const int i) const
141 // real & operator[] (const int i)
142 
143 /*----------------------mathutils callbacks ----------------------------*/
144 
146 {
147  if (!BPy_StrokeVertex_Check(bmo->cb_user)) {
148  return -1;
149  }
150  return 0;
151 }
152 
153 static int StrokeVertex_mathutils_get(BaseMathObject *bmo, int /*subtype*/)
154 {
155  BPy_StrokeVertex *self = (BPy_StrokeVertex *)bmo->cb_user;
156  bmo->data[0] = (float)self->sv->x();
157  bmo->data[1] = (float)self->sv->y();
158  return 0;
159 }
160 
161 static int StrokeVertex_mathutils_set(BaseMathObject *bmo, int /*subtype*/)
162 {
163  BPy_StrokeVertex *self = (BPy_StrokeVertex *)bmo->cb_user;
164  self->sv->setX((real)bmo->data[0]);
165  self->sv->setY((real)bmo->data[1]);
166  return 0;
167 }
168 
169 static int StrokeVertex_mathutils_get_index(BaseMathObject *bmo, int /*subtype*/, int index)
170 {
171  BPy_StrokeVertex *self = (BPy_StrokeVertex *)bmo->cb_user;
172  switch (index) {
173  case 0:
174  bmo->data[0] = (float)self->sv->x();
175  break;
176  case 1:
177  bmo->data[1] = (float)self->sv->y();
178  break;
179  default:
180  return -1;
181  }
182  return 0;
183 }
184 
185 static int StrokeVertex_mathutils_set_index(BaseMathObject *bmo, int /*subtype*/, int index)
186 {
187  BPy_StrokeVertex *self = (BPy_StrokeVertex *)bmo->cb_user;
188  switch (index) {
189  case 0:
190  self->sv->setX((real)bmo->data[0]);
191  break;
192  case 1:
193  self->sv->setY((real)bmo->data[1]);
194  break;
195  default:
196  return -1;
197  }
198  return 0;
199 }
200 
207 };
208 
209 static unsigned char StrokeVertex_mathutils_cb_index = -1;
210 
212 {
214 }
215 
216 /*----------------------StrokeVertex get/setters ----------------------------*/
217 
218 PyDoc_STRVAR(StrokeVertex_attribute_doc,
219  "StrokeAttribute for this StrokeVertex.\n"
220  "\n"
221  ":type: :class:`StrokeAttribute`");
222 
223 static PyObject *StrokeVertex_attribute_get(BPy_StrokeVertex *self, void *UNUSED(closure))
224 {
225  return BPy_StrokeAttribute_from_StrokeAttribute(self->sv->attribute());
226 }
227 
229  PyObject *value,
230  void *UNUSED(closure))
231 {
232  if (!BPy_StrokeAttribute_Check(value)) {
233  PyErr_SetString(PyExc_TypeError, "value must be a StrokeAttribute object");
234  return -1;
235  }
236  self->sv->setAttribute(*(((BPy_StrokeAttribute *)value)->sa));
237  return 0;
238 }
239 
240 PyDoc_STRVAR(StrokeVertex_curvilinear_abscissa_doc,
241  "Curvilinear abscissa of this StrokeVertex in the Stroke.\n"
242  "\n"
243  ":type: float");
244 
246  void *UNUSED(closure))
247 {
248  return PyFloat_FromDouble(self->sv->curvilinearAbscissa());
249 }
250 
252  PyObject *value,
253  void *UNUSED(closure))
254 {
255  float scalar;
256  if ((scalar = PyFloat_AsDouble(value)) == -1.0f && PyErr_Occurred()) {
257  /* parsed item not a number */
258  PyErr_SetString(PyExc_TypeError, "value must be a number");
259  return -1;
260  }
261  self->sv->setCurvilinearAbscissa(scalar);
262  return 0;
263 }
264 
265 PyDoc_STRVAR(StrokeVertex_point_doc,
266  "2D point coordinates.\n"
267  "\n"
268  ":type: :class:`mathutils.Vector`");
269 
270 static PyObject *StrokeVertex_point_get(BPy_StrokeVertex *self, void *UNUSED(closure))
271 {
272  return Vector_CreatePyObject_cb((PyObject *)self, 2, StrokeVertex_mathutils_cb_index, 0);
273 }
274 
275 static int StrokeVertex_point_set(BPy_StrokeVertex *self, PyObject *value, void *UNUSED(closure))
276 {
277  float v[2];
278  if (mathutils_array_parse(v, 2, 2, value, "value must be a 2-dimensional vector") == -1) {
279  return -1;
280  }
281  self->sv->setX(v[0]);
282  self->sv->setY(v[1]);
283  return 0;
284 }
285 
286 PyDoc_STRVAR(StrokeVertex_stroke_length_doc,
287  "Stroke length (it is only a value retained by the StrokeVertex,\n"
288  "and it won't change the real stroke length).\n"
289  "\n"
290  ":type: float");
291 
292 static PyObject *StrokeVertex_stroke_length_get(BPy_StrokeVertex *self, void *UNUSED(closure))
293 {
294  return PyFloat_FromDouble(self->sv->strokeLength());
295 }
296 
298  PyObject *value,
299  void *UNUSED(closure))
300 {
301  float scalar;
302  if ((scalar = PyFloat_AsDouble(value)) == -1.0f && PyErr_Occurred()) {
303  /* parsed item not a number */
304  PyErr_SetString(PyExc_TypeError, "value must be a number");
305  return -1;
306  }
307  self->sv->setStrokeLength(scalar);
308  return 0;
309 }
310 
311 PyDoc_STRVAR(StrokeVertex_u_doc,
312  "Curvilinear abscissa of this StrokeVertex in the Stroke.\n"
313  "\n"
314  ":type: float");
315 
316 static PyObject *StrokeVertex_u_get(BPy_StrokeVertex *self, void *UNUSED(closure))
317 {
318  return PyFloat_FromDouble(self->sv->u());
319 }
320 
321 static PyGetSetDef BPy_StrokeVertex_getseters[] = {
322  {"attribute",
325  StrokeVertex_attribute_doc,
326  nullptr},
327  {"curvilinear_abscissa",
330  StrokeVertex_curvilinear_abscissa_doc,
331  nullptr},
332  {"point",
333  (getter)StrokeVertex_point_get,
334  (setter)StrokeVertex_point_set,
335  StrokeVertex_point_doc,
336  nullptr},
337  {"stroke_length",
340  StrokeVertex_stroke_length_doc,
341  nullptr},
342  {"u", (getter)StrokeVertex_u_get, (setter) nullptr, StrokeVertex_u_doc, nullptr},
343  {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
344 };
345 
346 /*-----------------------BPy_StrokeVertex type definition ------------------------------*/
347 PyTypeObject StrokeVertex_Type = {
348  PyVarObject_HEAD_INIT(nullptr, 0) "StrokeVertex", /* tp_name */
349  sizeof(BPy_StrokeVertex), /* tp_basicsize */
350  0, /* tp_itemsize */
351  nullptr, /* tp_dealloc */
352  0, /* tp_vectorcall_offset */
353  nullptr, /* tp_getattr */
354  nullptr, /* tp_setattr */
355  nullptr, /* tp_reserved */
356  nullptr, /* tp_repr */
357  nullptr, /* tp_as_number */
358  nullptr, /* tp_as_sequence */
359  nullptr, /* tp_as_mapping */
360  nullptr, /* tp_hash */
361  nullptr, /* tp_call */
362  nullptr, /* tp_str */
363  nullptr, /* tp_getattro */
364  nullptr, /* tp_setattro */
365  nullptr, /* tp_as_buffer */
366  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
367  StrokeVertex_doc, /* tp_doc */
368  nullptr, /* tp_traverse */
369  nullptr, /* tp_clear */
370  nullptr, /* tp_richcompare */
371  0, /* tp_weaklistoffset */
372  nullptr, /* tp_iter */
373  nullptr, /* tp_iternext */
374  nullptr, /* tp_methods */
375  nullptr, /* tp_members */
376  BPy_StrokeVertex_getseters, /* tp_getset */
377  &CurvePoint_Type, /* tp_base */
378  nullptr, /* tp_dict */
379  nullptr, /* tp_descr_get */
380  nullptr, /* tp_descr_set */
381  0, /* tp_dictoffset */
382  (initproc)StrokeVertex_init, /* tp_init */
383  nullptr, /* tp_alloc */
384  nullptr, /* tp_new */
385 };
386 
388 
389 #ifdef __cplusplus
390 }
391 #endif
typedef float(TangentPoint)[2]
#define UNUSED(x)
PyObject * BPy_StrokeAttribute_from_StrokeAttribute(StrokeAttribute &sa)
PyTypeObject CurvePoint_Type
PyTypeObject SVertex_Type
PyTypeObject StrokeAttribute_Type
#define BPy_StrokeAttribute_Check(v)
static int StrokeVertex_point_set(BPy_StrokeVertex *self, PyObject *value, void *UNUSED(closure))
static PyGetSetDef BPy_StrokeVertex_getseters[]
static PyObject * StrokeVertex_attribute_get(BPy_StrokeVertex *self, void *UNUSED(closure))
static Mathutils_Callback StrokeVertex_mathutils_cb
static int StrokeVertex_mathutils_check(BaseMathObject *bmo)
static int StrokeVertex_stroke_length_set(BPy_StrokeVertex *self, PyObject *value, void *UNUSED(closure))
static PyObject * StrokeVertex_u_get(BPy_StrokeVertex *self, void *UNUSED(closure))
static int StrokeVertex_mathutils_get(BaseMathObject *bmo, int)
static int StrokeVertex_mathutils_set_index(BaseMathObject *bmo, int, int index)
static int StrokeVertex_init(BPy_StrokeVertex *self, PyObject *args, PyObject *kwds)
static PyObject * StrokeVertex_curvilinear_abscissa_get(BPy_StrokeVertex *self, void *UNUSED(closure))
static int StrokeVertex_attribute_set(BPy_StrokeVertex *self, PyObject *value, void *UNUSED(closure))
static int StrokeVertex_mathutils_get_index(BaseMathObject *bmo, int, int index)
static int StrokeVertex_mathutils_set(BaseMathObject *bmo, int)
void StrokeVertex_mathutils_register_callback()
static PyObject * StrokeVertex_stroke_length_get(BPy_StrokeVertex *self, void *UNUSED(closure))
static int StrokeVertex_curvilinear_abscissa_set(BPy_StrokeVertex *self, PyObject *value, void *UNUSED(closure))
PyTypeObject StrokeVertex_Type
PyDoc_STRVAR(StrokeVertex_doc, "Class hierarchy: :class:`Interface0D` > :class:`CurvePoint` > :class:`StrokeVertex`\n" "\n" "Class to define a stroke vertex.\n" "\n" ".. method:: __init__()\n" " __init__(brother)\n" " __init__(first_vertex, second_vertex, t3d)\n" " __init__(point)\n" " __init__(svertex)\n" " __init__(svertex, attribute)\n" "\n" " Builds a :class:`StrokeVertex` using the default constructor,\n" " copy constructor, from 2 :class:`StrokeVertex` and an interpolation parameter,\n" " from a CurvePoint, from a SVertex, or a :class:`SVertex`" " and a :class:`StrokeAttribute` object.\n" "\n" " :arg brother: A StrokeVertex object.\n" " :type brother: :class:`StrokeVertex`\n" " :arg first_vertex: The first StrokeVertex.\n" " :type first_vertex: :class:`StrokeVertex`\n" " :arg second_vertex: The second StrokeVertex.\n" " :type second_vertex: :class:`StrokeVertex`\n" " :arg t3d: An interpolation parameter.\n" " :type t3d: float\n" " :arg point: A CurvePoint object.\n" " :type point: :class:`CurvePoint`\n" " :arg svertex: An SVertex object.\n" " :type svertex: :class:`SVertex`\n" " :arg svertex: An SVertex object.\n" " :type svertex: :class:`SVertex`\n" " :arg attribute: A StrokeAttribute object.\n" " :type attribute: :class:`StrokeAttribute`")
static unsigned char StrokeVertex_mathutils_cb_index
static PyObject * StrokeVertex_point_get(BPy_StrokeVertex *self, void *UNUSED(closure))
#define BPy_StrokeVertex_Check(v)
ATTR_WARN_UNUSED_RESULT const BMVert * v
PyObject * self
Definition: bpy_driver.c:165
SVertex * B()
Definition: Curve.h:241
SVertex * A()
Definition: Curve.h:235
int mathutils_array_parse(float *array, int array_num_min, int array_num_max, PyObject *value, const char *error_prefix)
Definition: mathutils.c:98
uchar Mathutils_RegisterCallback(Mathutils_Callback *cb)
Definition: mathutils.c:551
PyObject * Vector_CreatePyObject_cb(PyObject *cb_user, int vec_num, uchar cb_type, uchar cb_subtype)
inherits from class Rep
Definition: AppCanvas.cpp:18
double real
Definition: Precision.h:12