Blender V4.3
BPy_Convert.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include "BPy_Convert.h"
10
11#include "BPy_BBox.h"
12#include "BPy_FrsMaterial.h"
13#include "BPy_Id.h"
14#include "BPy_IntegrationType.h"
15#include "BPy_Interface0D.h"
16#include "BPy_Interface1D.h"
17#include "BPy_MediumType.h"
18#include "BPy_Nature.h"
19#include "BPy_SShape.h"
20#include "BPy_StrokeAttribute.h"
21#include "BPy_ViewShape.h"
34
45
46#include "../stroke/StrokeRep.h"
47
48using namespace Freestyle;
49using namespace Freestyle::Geometry;
50
52
53//==============================
54// C++ => Python
55//==============================
56
57PyObject *PyBool_from_bool(bool b)
58{
59 return PyBool_FromLong(b ? 1 : 0);
60}
61
62PyObject *PyLong_subtype_new(PyTypeObject *ty, long value)
63{
64 BLI_assert(ty->tp_basicsize == sizeof(PyLongObject));
65 PyLongObject *result = PyObject_NewVar(PyLongObject, ty, 1);
66#if PY_VERSION_HEX >= 0x030c0000
67 {
68 /* Account for change in `PyLongObject` in Python 3.12+.
69 * The values of longs are no longer accessible via public API's, copy the value instead. */
70 PyLongObject *value_py = (PyLongObject *)PyLong_FromLong(value);
71 memcpy(&result->long_value, &value_py->long_value, sizeof(result->long_value));
72 Py_DECREF(value_py);
73 }
74#else
75 result->ob_digit[0] = value;
76#endif
77 return (PyObject *)result;
78}
79
80void PyLong_subtype_add_to_dict(PyObject *dict, PyTypeObject *ty, const char *attr, long value)
81{
82 PyObject *result = PyLong_subtype_new(ty, value);
83 PyDict_SetItemString(dict, attr, result);
84 /* Owned by the dictionary. */
85 Py_DECREF(result);
86}
87
88PyObject *Vector_from_Vec2f(Vec2f &vec)
89{
90 float vec_data[2]; // because vec->_coord is protected
91 vec_data[0] = vec.x();
92 vec_data[1] = vec.y();
93 return Vector_CreatePyObject(vec_data, 2, nullptr);
94}
95
96PyObject *Vector_from_Vec3f(Vec3f &vec)
97{
98 float vec_data[3]; // because vec->_coord is protected
99 vec_data[0] = vec.x();
100 vec_data[1] = vec.y();
101 vec_data[2] = vec.z();
102 return Vector_CreatePyObject(vec_data, 3, nullptr);
103}
104
106{
107 float vec_data[3]; // because vec->_coord is protected
108 vec_data[0] = vec.x();
109 vec_data[1] = vec.y();
110 vec_data[2] = vec.z();
111 return Vector_CreatePyObject(vec_data, 3, nullptr);
112}
113
114PyObject *BPy_Id_from_Id(Id &id)
115{
116 PyObject *py_id = Id_Type.tp_new(&Id_Type, nullptr, nullptr);
117 ((BPy_Id *)py_id)->id = new Id(id.getFirst(), id.getSecond());
118 return py_id;
119}
120
122{
123 if (typeid(if0D) == typeid(CurvePoint)) {
124 return BPy_CurvePoint_from_CurvePoint(dynamic_cast<CurvePoint &>(if0D));
125 }
126 if (typeid(if0D) == typeid(StrokeVertex)) {
127 return BPy_StrokeVertex_from_StrokeVertex(dynamic_cast<StrokeVertex &>(if0D));
128 }
129 if (typeid(if0D) == typeid(SVertex)) {
130 return BPy_SVertex_from_SVertex(dynamic_cast<SVertex &>(if0D));
131 }
132 if (typeid(if0D) == typeid(ViewVertex)) {
133 return BPy_ViewVertex_from_ViewVertex(dynamic_cast<ViewVertex &>(if0D));
134 }
135 if (typeid(if0D) == typeid(NonTVertex)) {
136 return BPy_NonTVertex_from_NonTVertex(dynamic_cast<NonTVertex &>(if0D));
137 }
138 if (typeid(if0D) == typeid(TVertex)) {
139 return BPy_TVertex_from_TVertex(dynamic_cast<TVertex &>(if0D));
140 }
141 if (typeid(if0D) == typeid(Interface0D)) {
143 }
144 string msg("unexpected type: " + if0D.getExactTypeName());
145 PyErr_SetString(PyExc_TypeError, msg.c_str());
146 return nullptr;
147}
148
150{
151 if (typeid(if1D) == typeid(ViewEdge)) {
152 return BPy_ViewEdge_from_ViewEdge(dynamic_cast<ViewEdge &>(if1D));
153 }
154 if (typeid(if1D) == typeid(Chain)) {
155 return BPy_Chain_from_Chain(dynamic_cast<Chain &>(if1D));
156 }
157 if (typeid(if1D) == typeid(Stroke)) {
158 return BPy_Stroke_from_Stroke(dynamic_cast<Stroke &>(if1D));
159 }
160 if (typeid(if1D) == typeid(FEdgeSharp)) {
161 return BPy_FEdgeSharp_from_FEdgeSharp(dynamic_cast<FEdgeSharp &>(if1D));
162 }
163 if (typeid(if1D) == typeid(FEdgeSmooth)) {
164 return BPy_FEdgeSmooth_from_FEdgeSmooth(dynamic_cast<FEdgeSmooth &>(if1D));
165 }
166 if (typeid(if1D) == typeid(FEdge)) {
167 return BPy_FEdge_from_FEdge(dynamic_cast<FEdge &>(if1D));
168 }
169 if (typeid(if1D) == typeid(Interface1D)) {
171 }
172 string msg("unexpected type: " + if1D.getExactTypeName());
173 PyErr_SetString(PyExc_TypeError, msg.c_str());
174 return nullptr;
175}
176
178{
179 if (typeid(fe) == typeid(FEdgeSharp)) {
180 return BPy_FEdgeSharp_from_FEdgeSharp(dynamic_cast<FEdgeSharp &>(fe));
181 }
182 if (typeid(fe) == typeid(FEdgeSmooth)) {
183 return BPy_FEdgeSmooth_from_FEdgeSmooth(dynamic_cast<FEdgeSmooth &>(fe));
184 }
185 if (typeid(fe) == typeid(FEdge)) {
186 return BPy_FEdge_from_FEdge(fe);
187 }
188 string msg("unexpected type: " + fe.getExactTypeName());
189 PyErr_SetString(PyExc_TypeError, msg.c_str());
190 return nullptr;
191}
192
194{
195 if (typeid(vv) == typeid(NonTVertex)) {
196 return BPy_NonTVertex_from_NonTVertex(dynamic_cast<NonTVertex &>(vv));
197 }
198 if (typeid(vv) == typeid(TVertex)) {
199 return BPy_TVertex_from_TVertex(dynamic_cast<TVertex &>(vv));
200 }
201 if (typeid(vv) == typeid(ViewVertex)) {
203 }
204 string msg("unexpected type: " + vv.getExactTypeName());
205 PyErr_SetString(PyExc_TypeError, msg.c_str());
206 return nullptr;
207}
208
210{
211 PyObject *py_if0D = Interface0D_Type.tp_new(&Interface0D_Type, nullptr, nullptr);
212 ((BPy_Interface0D *)py_if0D)->if0D = &if0D;
213 ((BPy_Interface0D *)py_if0D)->borrowed = true;
214 return py_if0D;
215}
216
218{
219 PyObject *py_if1D = Interface1D_Type.tp_new(&Interface1D_Type, nullptr, nullptr);
220 ((BPy_Interface1D *)py_if1D)->if1D = &if1D;
221 ((BPy_Interface1D *)py_if1D)->borrowed = true;
222 return py_if1D;
223}
224
226{
227 PyObject *py_sv = SVertex_Type.tp_new(&SVertex_Type, nullptr, nullptr);
228 ((BPy_SVertex *)py_sv)->sv = &sv;
229 ((BPy_SVertex *)py_sv)->py_if0D.if0D = ((BPy_SVertex *)py_sv)->sv;
230 ((BPy_SVertex *)py_sv)->py_if0D.borrowed = true;
231 return py_sv;
232}
233
235{
236 PyObject *py_fe = FEdgeSharp_Type.tp_new(&FEdgeSharp_Type, nullptr, nullptr);
237 ((BPy_FEdgeSharp *)py_fe)->fes = &fes;
238 ((BPy_FEdgeSharp *)py_fe)->py_fe.fe = ((BPy_FEdgeSharp *)py_fe)->fes;
239 ((BPy_FEdgeSharp *)py_fe)->py_fe.py_if1D.if1D = ((BPy_FEdgeSharp *)py_fe)->fes;
240 ((BPy_FEdgeSharp *)py_fe)->py_fe.py_if1D.borrowed = true;
241 return py_fe;
242}
243
245{
246 PyObject *py_fe = FEdgeSmooth_Type.tp_new(&FEdgeSmooth_Type, nullptr, nullptr);
247 ((BPy_FEdgeSmooth *)py_fe)->fes = &fes;
248 ((BPy_FEdgeSmooth *)py_fe)->py_fe.fe = ((BPy_FEdgeSmooth *)py_fe)->fes;
249 ((BPy_FEdgeSmooth *)py_fe)->py_fe.py_if1D.if1D = ((BPy_FEdgeSmooth *)py_fe)->fes;
250 ((BPy_FEdgeSmooth *)py_fe)->py_fe.py_if1D.borrowed = true;
251 return py_fe;
252}
253
255{
256 PyObject *py_fe = FEdge_Type.tp_new(&FEdge_Type, nullptr, nullptr);
257 ((BPy_FEdge *)py_fe)->fe = &fe;
258 ((BPy_FEdge *)py_fe)->py_if1D.if1D = ((BPy_FEdge *)py_fe)->fe;
259 ((BPy_FEdge *)py_fe)->py_if1D.borrowed = true;
260 return py_fe;
261}
262
264{
265 PyObject *args = PyTuple_New(1);
266 PyTuple_SET_ITEM(args, 0, PyLong_FromLong(n));
267 PyObject *py_n = Nature_Type.tp_new(&Nature_Type, args, nullptr);
268 Py_DECREF(args);
269 return py_n;
270}
271
273{
274 PyObject *py_s = Stroke_Type.tp_new(&Stroke_Type, nullptr, nullptr);
275 ((BPy_Stroke *)py_s)->s = &s;
276 ((BPy_Stroke *)py_s)->py_if1D.if1D = ((BPy_Stroke *)py_s)->s;
277 ((BPy_Stroke *)py_s)->py_if1D.borrowed = true;
278 return py_s;
279}
280
282{
283 PyObject *py_sa = StrokeAttribute_Type.tp_new(&StrokeAttribute_Type, nullptr, nullptr);
284 ((BPy_StrokeAttribute *)py_sa)->sa = &sa;
285 ((BPy_StrokeAttribute *)py_sa)->borrowed = true;
286 return py_sa;
287}
288
290{
291 PyObject *args = PyTuple_New(1);
292 PyTuple_SET_ITEM(args, 0, PyLong_FromLong(n));
293 PyObject *py_mt = MediumType_Type.tp_new(&MediumType_Type, args, nullptr);
294 Py_DECREF(args);
295 return py_mt;
296}
297
299{
300 PyObject *py_sv = StrokeVertex_Type.tp_new(&StrokeVertex_Type, nullptr, nullptr);
301 ((BPy_StrokeVertex *)py_sv)->sv = &sv;
302 ((BPy_StrokeVertex *)py_sv)->py_cp.cp = ((BPy_StrokeVertex *)py_sv)->sv;
303 ((BPy_StrokeVertex *)py_sv)->py_cp.py_if0D.if0D = ((BPy_StrokeVertex *)py_sv)->sv;
304 ((BPy_StrokeVertex *)py_sv)->py_cp.py_if0D.borrowed = true;
305 return py_sv;
306}
307
309{
310 PyObject *py_vv = ViewVertex_Type.tp_new(&ViewVertex_Type, nullptr, nullptr);
311 ((BPy_ViewVertex *)py_vv)->vv = &vv;
312 ((BPy_ViewVertex *)py_vv)->py_if0D.if0D = ((BPy_ViewVertex *)py_vv)->vv;
313 ((BPy_ViewVertex *)py_vv)->py_if0D.borrowed = true;
314 return py_vv;
315}
316
318{
319 PyObject *py_ntv = NonTVertex_Type.tp_new(&NonTVertex_Type, nullptr, nullptr);
320 ((BPy_NonTVertex *)py_ntv)->ntv = &ntv;
321 ((BPy_NonTVertex *)py_ntv)->py_vv.vv = ((BPy_NonTVertex *)py_ntv)->ntv;
322 ((BPy_NonTVertex *)py_ntv)->py_vv.py_if0D.if0D = ((BPy_NonTVertex *)py_ntv)->ntv;
323 ((BPy_NonTVertex *)py_ntv)->py_vv.py_if0D.borrowed = true;
324 return py_ntv;
325}
326
328{
329 PyObject *py_tv = TVertex_Type.tp_new(&TVertex_Type, nullptr, nullptr);
330 ((BPy_TVertex *)py_tv)->tv = &tv;
331 ((BPy_TVertex *)py_tv)->py_vv.vv = ((BPy_TVertex *)py_tv)->tv;
332 ((BPy_TVertex *)py_tv)->py_vv.py_if0D.if0D = ((BPy_TVertex *)py_tv)->tv;
333 ((BPy_TVertex *)py_tv)->py_vv.py_if0D.borrowed = true;
334 return py_tv;
335}
336
337PyObject *BPy_BBox_from_BBox(const BBox<Vec3r> &bb)
338{
339 PyObject *py_bb = BBox_Type.tp_new(&BBox_Type, nullptr, nullptr);
340 ((BPy_BBox *)py_bb)->bb = new BBox<Vec3r>(bb);
341 return py_bb;
342}
343
345{
346 PyObject *py_ve = ViewEdge_Type.tp_new(&ViewEdge_Type, nullptr, nullptr);
347 ((BPy_ViewEdge *)py_ve)->ve = &ve;
348 ((BPy_ViewEdge *)py_ve)->py_if1D.if1D = ((BPy_ViewEdge *)py_ve)->ve;
349 ((BPy_ViewEdge *)py_ve)->py_if1D.borrowed = true;
350 return py_ve;
351}
352
354{
355 PyObject *py_c = Chain_Type.tp_new(&Chain_Type, nullptr, nullptr);
356 ((BPy_Chain *)py_c)->c = &c;
357 ((BPy_Chain *)py_c)->py_c.c = ((BPy_Chain *)py_c)->c;
358 ((BPy_Chain *)py_c)->py_c.py_if1D.if1D = ((BPy_Chain *)py_c)->c;
359 ((BPy_Chain *)py_c)->py_c.py_if1D.borrowed = true;
360 return py_c;
361}
362
364{
365 PyObject *py_ss = SShape_Type.tp_new(&SShape_Type, nullptr, nullptr);
366 ((BPy_SShape *)py_ss)->ss = &ss;
367 ((BPy_SShape *)py_ss)->borrowed = true;
368 return py_ss;
369}
370
372{
373 PyObject *py_vs = ViewShape_Type.tp_new(&ViewShape_Type, nullptr, nullptr);
374 ((BPy_ViewShape *)py_vs)->vs = &vs;
375 ((BPy_ViewShape *)py_vs)->borrowed = true;
376 ((BPy_ViewShape *)py_vs)->py_ss = nullptr;
377 return py_vs;
378}
379
381{
382 PyObject *py_m = FrsMaterial_Type.tp_new(&FrsMaterial_Type, nullptr, nullptr);
383 ((BPy_FrsMaterial *)py_m)->m = new FrsMaterial(m);
384 return py_m;
385}
386
388{
389 PyObject *args = PyTuple_New(1);
390 PyTuple_SET_ITEM(args, 0, PyLong_FromLong(i));
391 PyObject *py_it = IntegrationType_Type.tp_new(&IntegrationType_Type, args, nullptr);
392 Py_DECREF(args);
393 return py_it;
394}
395
397{
398 PyObject *py_cp = CurvePoint_Type.tp_new(&CurvePoint_Type, nullptr, nullptr);
399 // CurvePointIterator::operator*() returns a reference of a class data
400 // member whose value is mutable upon iteration over different CurvePoints.
401 // It is likely that such a mutable reference is passed to this function,
402 // so that a new allocated CurvePoint instance is created here to avoid
403 // nasty bugs (cf. #41464).
404 ((BPy_CurvePoint *)py_cp)->cp = new CurvePoint(cp);
405 ((BPy_CurvePoint *)py_cp)->py_if0D.if0D = ((BPy_CurvePoint *)py_cp)->cp;
406 ((BPy_CurvePoint *)py_cp)->py_if0D.borrowed = false;
407 return py_cp;
408}
409
411{
412 PyObject *py_dve = PyTuple_New(2);
414 py_dve, BPy_ViewEdge_from_ViewEdge(*(dve.first)), PyBool_from_bool(dve.second));
415 return py_dve;
416}
417
418//==============================
419// Iterators
420//==============================
421
423{
424 PyObject *py_a_it = AdjacencyIterator_Type.tp_new(&AdjacencyIterator_Type, nullptr, nullptr);
425 ((BPy_AdjacencyIterator *)py_a_it)->a_it = new AdjacencyIterator(a_it);
426 ((BPy_AdjacencyIterator *)py_a_it)->py_it.it = ((BPy_AdjacencyIterator *)py_a_it)->a_it;
427 ((BPy_AdjacencyIterator *)py_a_it)->at_start = true;
428 return py_a_it;
429}
430
432 bool reversed)
433{
434 PyObject *py_if0D_it = Interface0DIterator_Type.tp_new(
435 &Interface0DIterator_Type, nullptr, nullptr);
436 ((BPy_Interface0DIterator *)py_if0D_it)->if0D_it = new Interface0DIterator(if0D_it);
437 ((BPy_Interface0DIterator *)py_if0D_it)->py_it.it =
438 ((BPy_Interface0DIterator *)py_if0D_it)->if0D_it;
439 ((BPy_Interface0DIterator *)py_if0D_it)->at_start = true;
440 ((BPy_Interface0DIterator *)py_if0D_it)->reversed = reversed;
441 return py_if0D_it;
442}
443
445{
446 PyObject *py_cp_it = CurvePointIterator_Type.tp_new(&CurvePointIterator_Type, nullptr, nullptr);
447 ((BPy_CurvePointIterator *)py_cp_it)->cp_it = new CurveInternal::CurvePointIterator(cp_it);
448 ((BPy_CurvePointIterator *)py_cp_it)->py_it.it = ((BPy_CurvePointIterator *)py_cp_it)->cp_it;
449 return py_cp_it;
450}
451
453 StrokeInternal::StrokeVertexIterator &sv_it, bool reversed)
454{
455 PyObject *py_sv_it = StrokeVertexIterator_Type.tp_new(
456 &StrokeVertexIterator_Type, nullptr, nullptr);
457 ((BPy_StrokeVertexIterator *)py_sv_it)->sv_it = new StrokeInternal::StrokeVertexIterator(sv_it);
458 ((BPy_StrokeVertexIterator *)py_sv_it)->py_it.it = ((BPy_StrokeVertexIterator *)py_sv_it)->sv_it;
459 ((BPy_StrokeVertexIterator *)py_sv_it)->at_start = true;
460 ((BPy_StrokeVertexIterator *)py_sv_it)->reversed = reversed;
461 return py_sv_it;
462}
463
465{
466 PyObject *py_sv_it = SVertexIterator_Type.tp_new(&SVertexIterator_Type, nullptr, nullptr);
467 ((BPy_SVertexIterator *)py_sv_it)->sv_it = new ViewEdgeInternal::SVertexIterator(sv_it);
468 ((BPy_SVertexIterator *)py_sv_it)->py_it.it = ((BPy_SVertexIterator *)py_sv_it)->sv_it;
469 return py_sv_it;
470}
471
474{
475 PyObject *py_ove_it = orientedViewEdgeIterator_Type.tp_new(
476 &orientedViewEdgeIterator_Type, nullptr, nullptr);
477 ((BPy_orientedViewEdgeIterator *)py_ove_it)->ove_it =
479 ((BPy_orientedViewEdgeIterator *)py_ove_it)->py_it.it =
480 ((BPy_orientedViewEdgeIterator *)py_ove_it)->ove_it;
481 ((BPy_orientedViewEdgeIterator *)py_ove_it)->at_start = true;
482 ((BPy_orientedViewEdgeIterator *)py_ove_it)->reversed = reversed;
483 return py_ove_it;
484}
485
487{
488 PyObject *py_ve_it = ViewEdgeIterator_Type.tp_new(&ViewEdgeIterator_Type, nullptr, nullptr);
489 ((BPy_ViewEdgeIterator *)py_ve_it)->ve_it = new ViewEdgeInternal::ViewEdgeIterator(ve_it);
490 ((BPy_ViewEdgeIterator *)py_ve_it)->py_it.it = ((BPy_ViewEdgeIterator *)py_ve_it)->ve_it;
491 return py_ve_it;
492}
493
495{
496 PyObject *py_c_it = ChainingIterator_Type.tp_new(&ChainingIterator_Type, nullptr, nullptr);
497 ((BPy_ChainingIterator *)py_c_it)->c_it = new ChainingIterator(c_it);
498 ((BPy_ChainingIterator *)py_c_it)->py_ve_it.py_it.it = ((BPy_ChainingIterator *)py_c_it)->c_it;
499 return py_c_it;
500}
501
503{
504 PyObject *py_cp_it = ChainPredicateIterator_Type.tp_new(
505 &ChainPredicateIterator_Type, nullptr, nullptr);
506 ((BPy_ChainPredicateIterator *)py_cp_it)->cp_it = new ChainPredicateIterator(cp_it);
507 ((BPy_ChainPredicateIterator *)py_cp_it)->py_c_it.py_ve_it.py_it.it =
508 ((BPy_ChainPredicateIterator *)py_cp_it)->cp_it;
509 return py_cp_it;
510}
511
513{
514 PyObject *py_cs_it = ChainSilhouetteIterator_Type.tp_new(
515 &ChainSilhouetteIterator_Type, nullptr, nullptr);
516 ((BPy_ChainSilhouetteIterator *)py_cs_it)->cs_it = new ChainSilhouetteIterator(cs_it);
517 ((BPy_ChainSilhouetteIterator *)py_cs_it)->py_c_it.py_ve_it.py_it.it =
518 ((BPy_ChainSilhouetteIterator *)py_cs_it)->cs_it;
519 return py_cs_it;
520}
521
522//==============================
523// Python => C++
524//==============================
525
526bool bool_from_PyBool(PyObject *b)
527{
528 return PyObject_IsTrue(b) != 0;
529}
530
532{
533 return static_cast<IntegrationType>(PyLong_AsLong(obj));
534}
535
537{
538 return static_cast<Stroke::MediumType>(PyLong_AsLong(obj));
539}
540
542{
543 return static_cast<Nature::EdgeNature>(PyLong_AsLong(obj));
544}
545
546bool Vec2f_ptr_from_PyObject(PyObject *obj, Vec2f &vec)
547{
548 if (Vec2f_ptr_from_Vector(obj, vec)) {
549 return true;
550 }
551 if (Vec2f_ptr_from_PyList(obj, vec)) {
552 return true;
553 }
554 if (Vec2f_ptr_from_PyTuple(obj, vec)) {
555 return true;
556 }
557 return false;
558}
559
560bool Vec3f_ptr_from_PyObject(PyObject *obj, Vec3f &vec)
561{
562 if (Vec3f_ptr_from_Vector(obj, vec)) {
563 return true;
564 }
565 if (Vec3f_ptr_from_Color(obj, vec)) {
566 return true;
567 }
568 if (Vec3f_ptr_from_PyList(obj, vec)) {
569 return true;
570 }
571 if (Vec3f_ptr_from_PyTuple(obj, vec)) {
572 return true;
573 }
574 return false;
575}
576
577bool Vec3r_ptr_from_PyObject(PyObject *obj, Vec3r &vec)
578{
579 if (Vec3r_ptr_from_Vector(obj, vec)) {
580 return true;
581 }
582 if (Vec3r_ptr_from_Color(obj, vec)) {
583 return true;
584 }
585 if (Vec3r_ptr_from_PyList(obj, vec)) {
586 return true;
587 }
588 if (Vec3r_ptr_from_PyTuple(obj, vec)) {
589 return true;
590 }
591 return false;
592}
593
594bool Vec2f_ptr_from_Vector(PyObject *obj, Vec2f &vec)
595{
596 if (!VectorObject_Check(obj) || ((VectorObject *)obj)->vec_num != 2) {
597 return false;
598 }
599 if (BaseMath_ReadCallback((BaseMathObject *)obj) == -1) {
600 return false;
601 }
602 vec[0] = ((VectorObject *)obj)->vec[0];
603 vec[1] = ((VectorObject *)obj)->vec[1];
604 return true;
605}
606
607bool Vec3f_ptr_from_Vector(PyObject *obj, Vec3f &vec)
608{
609 if (!VectorObject_Check(obj) || ((VectorObject *)obj)->vec_num != 3) {
610 return false;
611 }
612 if (BaseMath_ReadCallback((BaseMathObject *)obj) == -1) {
613 return false;
614 }
615 vec[0] = ((VectorObject *)obj)->vec[0];
616 vec[1] = ((VectorObject *)obj)->vec[1];
617 vec[2] = ((VectorObject *)obj)->vec[2];
618 return true;
619}
620
621bool Vec3r_ptr_from_Vector(PyObject *obj, Vec3r &vec)
622{
623 if (!VectorObject_Check(obj) || ((VectorObject *)obj)->vec_num != 3) {
624 return false;
625 }
626 if (BaseMath_ReadCallback((BaseMathObject *)obj) == -1) {
627 return false;
628 }
629 vec[0] = ((VectorObject *)obj)->vec[0];
630 vec[1] = ((VectorObject *)obj)->vec[1];
631 vec[2] = ((VectorObject *)obj)->vec[2];
632 return true;
633}
634
635bool Vec3f_ptr_from_Color(PyObject *obj, Vec3f &vec)
636{
637 if (!ColorObject_Check(obj)) {
638 return false;
639 }
640 if (BaseMath_ReadCallback((BaseMathObject *)obj) == -1) {
641 return false;
642 }
643 vec[0] = ((ColorObject *)obj)->col[0];
644 vec[1] = ((ColorObject *)obj)->col[1];
645 vec[2] = ((ColorObject *)obj)->col[2];
646 return true;
647}
648
649bool Vec3r_ptr_from_Color(PyObject *obj, Vec3r &vec)
650{
651 if (!ColorObject_Check(obj)) {
652 return false;
653 }
654 if (BaseMath_ReadCallback((BaseMathObject *)obj) == -1) {
655 return false;
656 }
657 vec[0] = ((ColorObject *)obj)->col[0];
658 vec[1] = ((ColorObject *)obj)->col[1];
659 vec[2] = ((ColorObject *)obj)->col[2];
660 return true;
661}
662
663static bool float_array_from_PyList(PyObject *obj, float *v, int n)
664{
665 for (int i = 0; i < n; i++) {
666 v[i] = PyFloat_AsDouble(PyList_GET_ITEM(obj, i));
667 if (v[i] == -1.0f && PyErr_Occurred()) {
668 PyErr_SetString(PyExc_TypeError, "list elements must be a number");
669 return false;
670 }
671 }
672 return true;
673}
674
675bool Vec2f_ptr_from_PyList(PyObject *obj, Vec2f &vec)
676{
677 float v[2];
678
679 if (!PyList_Check(obj) || PyList_GET_SIZE(obj) != 2) {
680 return false;
681 }
682 if (!float_array_from_PyList(obj, v, 2)) {
683 return false;
684 }
685 vec[0] = v[0];
686 vec[1] = v[1];
687 return true;
688}
689
690bool Vec3f_ptr_from_PyList(PyObject *obj, Vec3f &vec)
691{
692 float v[3];
693
694 if (!PyList_Check(obj) || PyList_GET_SIZE(obj) != 3) {
695 return false;
696 }
697 if (!float_array_from_PyList(obj, v, 3)) {
698 return false;
699 }
700 vec[0] = v[0];
701 vec[1] = v[1];
702 vec[2] = v[2];
703 return true;
704}
705
706bool Vec3r_ptr_from_PyList(PyObject *obj, Vec3r &vec)
707{
708 float v[3];
709
710 if (!PyList_Check(obj) || PyList_GET_SIZE(obj) != 3) {
711 return false;
712 }
713 if (!float_array_from_PyList(obj, v, 3)) {
714 return false;
715 }
716 vec[0] = v[0];
717 vec[1] = v[1];
718 vec[2] = v[2];
719 return true;
720}
721
722static bool float_array_from_PyTuple(PyObject *obj, float *v, int n)
723{
724 for (int i = 0; i < n; i++) {
725 v[i] = PyFloat_AsDouble(PyTuple_GET_ITEM(obj, i));
726 if (v[i] == -1.0f && PyErr_Occurred()) {
727 PyErr_SetString(PyExc_TypeError, "tuple elements must be a number");
728 return false;
729 }
730 }
731 return true;
732}
733
734bool Vec2f_ptr_from_PyTuple(PyObject *obj, Vec2f &vec)
735{
736 float v[2];
737
738 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2) {
739 return false;
740 }
741 if (!float_array_from_PyTuple(obj, v, 2)) {
742 return false;
743 }
744 vec[0] = v[0];
745 vec[1] = v[1];
746 return true;
747}
748
749bool Vec3f_ptr_from_PyTuple(PyObject *obj, Vec3f &vec)
750{
751 float v[3];
752
753 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 3) {
754 return false;
755 }
756 if (!float_array_from_PyTuple(obj, v, 3)) {
757 return false;
758 }
759 vec[0] = v[0];
760 vec[1] = v[1];
761 vec[2] = v[2];
762 return true;
763}
764
765bool Vec3r_ptr_from_PyTuple(PyObject *obj, Vec3r &vec)
766{
767 float v[3];
768
769 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 3) {
770 return false;
771 }
772 if (!float_array_from_PyTuple(obj, v, 3)) {
773 return false;
774 }
775 vec[0] = v[0];
776 vec[1] = v[1];
777 vec[2] = v[2];
778 return true;
779}
780
781// helpers for argument parsing
782
783bool float_array_from_PyObject(PyObject *obj, float *v, int n)
784{
785 if (VectorObject_Check(obj) && ((VectorObject *)obj)->vec_num == n) {
786 if (BaseMath_ReadCallback((BaseMathObject *)obj) == -1) {
787 return false;
788 }
789 for (int i = 0; i < n; i++) {
790 v[i] = ((VectorObject *)obj)->vec[i];
791 }
792 return true;
793 }
794 if (ColorObject_Check(obj) && n == 3) {
795 if (BaseMath_ReadCallback((BaseMathObject *)obj) == -1) {
796 return false;
797 }
798 for (int i = 0; i < n; i++) {
799 v[i] = ((ColorObject *)obj)->col[i];
800 }
801 return true;
802 }
803 if (PyList_Check(obj) && PyList_GET_SIZE(obj) == n) {
804 return float_array_from_PyList(obj, v, n);
805 }
806 if (PyTuple_Check(obj) && PyTuple_GET_SIZE(obj) == n) {
807 return float_array_from_PyTuple(obj, v, n);
808 }
809 return false;
810}
811
812int convert_v4(PyObject *obj, void *v)
813{
814 return mathutils_array_parse((float *)v, 4, 4, obj, "Error parsing 4D vector");
815}
816
817int convert_v3(PyObject *obj, void *v)
818{
819 return mathutils_array_parse((float *)v, 3, 3, obj, "Error parsing 3D vector");
820}
821
822int convert_v2(PyObject *obj, void *v)
823{
824 return mathutils_array_parse((float *)v, 2, 2, obj, "Error parsing 2D vector");
825}
826
#define BLI_assert(a)
Definition BLI_assert.h:50
unsigned short ushort
PyTypeObject AdjacencyIterator_Type
PyTypeObject BBox_Type
Definition BPy_BBox.cpp:70
PyTypeObject ChainPredicateIterator_Type
PyTypeObject ChainSilhouetteIterator_Type
PyTypeObject Chain_Type
PyTypeObject ChainingIterator_Type
PyObject * BPy_NonTVertex_from_NonTVertex(NonTVertex &ntv)
bool Vec3r_ptr_from_Vector(PyObject *obj, Vec3r &vec)
bool Vec3r_ptr_from_Color(PyObject *obj, Vec3r &vec)
bool Vec2f_ptr_from_Vector(PyObject *obj, Vec2f &vec)
PyObject * BPy_CurvePointIterator_from_CurvePointIterator(CurveInternal::CurvePointIterator &cp_it)
PyObject * BPy_StrokeVertexIterator_from_StrokeVertexIterator(StrokeInternal::StrokeVertexIterator &sv_it, bool reversed)
PyObject * Vector_from_Vec3r(Vec3r &vec)
bool Vec3r_ptr_from_PyList(PyObject *obj, Vec3r &vec)
bool Vec3r_ptr_from_PyTuple(PyObject *obj, Vec3r &vec)
PyObject * BPy_Interface0DIterator_from_Interface0DIterator(Interface0DIterator &if0D_it, bool reversed)
PyObject * BPy_Id_from_Id(Id &id)
PyObject * BPy_StrokeAttribute_from_StrokeAttribute(StrokeAttribute &sa)
bool bool_from_PyBool(PyObject *b)
void PyLong_subtype_add_to_dict(PyObject *dict, PyTypeObject *ty, const char *attr, long value)
PyObject * BPy_ChainPredicateIterator_from_ChainPredicateIterator(ChainPredicateIterator &cp_it)
PyObject * BPy_SVertexIterator_from_SVertexIterator(ViewEdgeInternal::SVertexIterator &sv_it)
bool Vec2f_ptr_from_PyTuple(PyObject *obj, Vec2f &vec)
PyObject * BPy_StrokeVertex_from_StrokeVertex(StrokeVertex &sv)
PyObject * PyLong_subtype_new(PyTypeObject *ty, long value)
bool Vec3f_ptr_from_PyObject(PyObject *obj, Vec3f &vec)
PyObject * BPy_Interface0D_from_Interface0D(Interface0D &if0D)
PyObject * BPy_SShape_from_SShape(SShape &ss)
bool Vec3f_ptr_from_Color(PyObject *obj, Vec3f &vec)
PyObject * BPy_ViewVertex_from_ViewVertex(ViewVertex &vv)
PyObject * BPy_Chain_from_Chain(Chain &c)
PyObject * Any_BPy_Interface1D_from_Interface1D(Interface1D &if1D)
bool Vec2f_ptr_from_PyObject(PyObject *obj, Vec2f &vec)
PyObject * BPy_MediumType_from_MediumType(Stroke::MediumType n)
bool float_array_from_PyObject(PyObject *obj, float *v, int n)
bool Vec3f_ptr_from_PyTuple(PyObject *obj, Vec3f &vec)
PyObject * Any_BPy_FEdge_from_FEdge(FEdge &fe)
Nature::EdgeNature EdgeNature_from_BPy_Nature(PyObject *obj)
PyObject * BPy_orientedViewEdgeIterator_from_orientedViewEdgeIterator(ViewVertexInternal::orientedViewEdgeIterator &ove_it, bool reversed)
PyObject * BPy_FEdgeSmooth_from_FEdgeSmooth(FEdgeSmooth &fes)
PyObject * BPy_FEdgeSharp_from_FEdgeSharp(FEdgeSharp &fes)
bool Vec3f_ptr_from_PyList(PyObject *obj, Vec3f &vec)
PyObject * BPy_Nature_from_Nature(ushort n)
PyObject * BPy_directedViewEdge_from_directedViewEdge(ViewVertex::directedViewEdge &dve)
PyObject * BPy_ViewEdgeIterator_from_ViewEdgeIterator(ViewEdgeInternal::ViewEdgeIterator &ve_it)
PyObject * BPy_Stroke_from_Stroke(Stroke &s)
PyObject * BPy_ChainingIterator_from_ChainingIterator(ChainingIterator &c_it)
PyObject * BPy_CurvePoint_from_CurvePoint(CurvePoint &cp)
PyObject * BPy_ChainSilhouetteIterator_from_ChainSilhouetteIterator(ChainSilhouetteIterator &cs_it)
PyObject * BPy_FEdge_from_FEdge(FEdge &fe)
PyObject * BPy_AdjacencyIterator_from_AdjacencyIterator(AdjacencyIterator &a_it)
int convert_v4(PyObject *obj, void *v)
IntegrationType IntegrationType_from_BPy_IntegrationType(PyObject *obj)
PyObject * BPy_SVertex_from_SVertex(SVertex &sv)
Stroke::MediumType MediumType_from_BPy_MediumType(PyObject *obj)
PyObject * Vector_from_Vec3f(Vec3f &vec)
PyObject * Any_BPy_ViewVertex_from_ViewVertex(ViewVertex &vv)
static bool float_array_from_PyList(PyObject *obj, float *v, int n)
PyObject * BPy_ViewEdge_from_ViewEdge(ViewEdge &ve)
PyObject * Vector_from_Vec2f(Vec2f &vec)
int convert_v3(PyObject *obj, void *v)
PyObject * BPy_IntegrationType_from_IntegrationType(IntegrationType i)
PyObject * BPy_TVertex_from_TVertex(TVertex &tv)
PyObject * PyBool_from_bool(bool b)
bool Vec2f_ptr_from_PyList(PyObject *obj, Vec2f &vec)
PyObject * BPy_ViewShape_from_ViewShape(ViewShape &vs)
PyObject * BPy_Interface1D_from_Interface1D(Interface1D &if1D)
PyObject * BPy_BBox_from_BBox(const BBox< Vec3r > &bb)
bool Vec3f_ptr_from_Vector(PyObject *obj, Vec3f &vec)
bool Vec3r_ptr_from_PyObject(PyObject *obj, Vec3r &vec)
static bool float_array_from_PyTuple(PyObject *obj, float *v, int n)
PyObject * BPy_FrsMaterial_from_FrsMaterial(const FrsMaterial &m)
int convert_v2(PyObject *obj, void *v)
PyObject * Any_BPy_Interface0D_from_Interface0D(Interface0D &if0D)
PyTypeObject CurvePointIterator_Type
PyTypeObject CurvePoint_Type
PyTypeObject FEdgeSharp_Type
PyTypeObject FEdgeSmooth_Type
PyTypeObject FEdge_Type
PyTypeObject FrsMaterial_Type
PyTypeObject Id_Type
Definition BPy_Id.cpp:164
PyTypeObject IntegrationType_Type
PyTypeObject Interface0DIterator_Type
PyTypeObject Interface0D_Type
PyTypeObject Interface1D_Type
PyTypeObject MediumType_Type
PyTypeObject Nature_Type
PyTypeObject NonTVertex_Type
PyTypeObject SShape_Type
PyTypeObject SVertexIterator_Type
PyTypeObject SVertex_Type
PyTypeObject StrokeAttribute_Type
PyTypeObject StrokeVertexIterator_Type
PyTypeObject StrokeVertex_Type
PyTypeObject Stroke_Type
PyTypeObject TVertex_Type
PyTypeObject ViewEdgeIterator_Type
PyTypeObject ViewEdge_Type
PyTypeObject ViewShape_Type
PyTypeObject ViewVertex_Type
PyTypeObject orientedViewEdgeIterator_Type
Class to define the representation of a stroke (for display purpose)
ATTR_WARN_UNUSED_RESULT const BMVert * v
virtual string getExactTypeName() const
Definition Silhouette.h:468
virtual string getExactTypeName() const
Definition Interface0D.h:52
virtual string getExactTypeName() const
value_type x() const
Definition VecMat.h:296
value_type y() const
Definition VecMat.h:306
value_type x() const
Definition VecMat.h:493
value_type z() const
Definition VecMat.h:513
value_type y() const
Definition VecMat.h:503
pair< ViewEdge *, bool > directedViewEdge
Definition ViewMap.h:267
virtual string getExactTypeName() const
Definition ViewMap.h:260
local_group_size(16, 16) .push_constant(Type b
int mathutils_array_parse(float *array, int array_num_min, int array_num_max, PyObject *value, const char *error_prefix)
Definition mathutils.cc:97
#define BaseMath_ReadCallback(_self)
Definition mathutils.hh:125
#define ColorObject_Check(v)
PyObject * Vector_CreatePyObject(const float *vec, const int vec_num, PyTypeObject *base_type)
#define VectorObject_Check(v)
ushort EdgeNature
Definition Nature.h:36
inherits from class Rep
Definition AppCanvas.cpp:20
static uint c
Definition RandGen.cpp:87
#define PyTuple_SET_ITEMS(op_arg,...)