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