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