4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
28 * Initialize Python thingies.
31 #ifndef __KX_PYMATH_H__
32 #define __KX_PYMATH_H__
36 #include "../../blender/python/generic/Mathutils.h" /* so we can have mathutils callbacks */
40 #include "MT_Point2.h"
41 #include "MT_Point3.h"
42 #include "MT_Vector2.h"
43 #include "MT_Vector3.h"
44 #include "MT_Vector4.h"
45 #include "MT_Matrix3x3.h"
46 #include "MT_Matrix4x4.h"
48 #include "KX_Python.h"
49 #include "PyObjectPlus.h"
51 inline unsigned int Size(const MT_Matrix4x4&) { return 4; }
52 inline unsigned int Size(const MT_Matrix3x3&) { return 3; }
53 inline unsigned int Size(const MT_Tuple2&) { return 2; }
54 inline unsigned int Size(const MT_Tuple3&) { return 3; }
55 inline unsigned int Size(const MT_Tuple4&) { return 4; }
58 * Converts the given python matrix to an MT class.
61 bool PyMatTo(PyObject* pymat, T& mat)
65 if (PySequence_Check(pymat))
67 unsigned int rows = PySequence_Size(pymat);
68 if (rows != Size(mat))
71 for (unsigned int y = 0; noerror && y < Size(mat); y++)
73 PyObject *pyrow = PySequence_GetItem(pymat, y); /* new ref */
74 if (!PyErr_Occurred() && PySequence_Check(pyrow))
76 unsigned int cols = PySequence_Size(pyrow);
77 if (cols != Size(mat))
81 for( unsigned int x = 0; x < Size(mat); x++)
83 PyObject *item = PySequence_GetItem(pyrow, x); /* new ref */
84 mat[y][x] = PyFloat_AsDouble(item);
96 PyErr_SetString(PyExc_TypeError, "could not be converted to a matrix (sequence of sequences)");
102 * Converts a python sequence to a MT class.
105 bool PyVecTo(PyObject* pyval, T& vec)
108 /* no need for BaseMath_ReadCallback() here, reading the sequences will do this */
110 if(VectorObject_Check(pyval)) {
111 VectorObject *pyvec= (VectorObject *)pyval;
112 if (pyvec->size != Size(vec)) {
113 PyErr_Format(PyExc_AttributeError, "error setting vector, %d args, should be %d", pyvec->size, Size(vec));
116 vec.getValue((float *) pyvec->vec);
119 else if(EulerObject_Check(pyval)) {
120 EulerObject *pyeul= (EulerObject *)pyval;
121 if (3 != Size(vec)) {
122 PyErr_Format(PyExc_AttributeError, "error setting vector, %d args, should be %d", 3, Size(vec));
125 vec.getValue((float *) pyeul->eul);
129 if(PyTuple_Check(pyval))
131 unsigned int numitems = PyTuple_GET_SIZE(pyval);
132 if (numitems != Size(vec)) {
133 PyErr_Format(PyExc_AttributeError, "error setting vector, %d args, should be %d", numitems, Size(vec));
137 for (unsigned int x = 0; x < numitems; x++)
138 vec[x] = PyFloat_AsDouble(PyTuple_GET_ITEM(pyval, x)); /* borrow ref */
140 if (PyErr_Occurred()) {
141 PyErr_SetString(PyExc_AttributeError, "one or more of the items in the sequence was not a float");
147 else if (BGE_PROXY_CHECK_TYPE(pyval))
148 { /* note, include this check because PySequence_Check does too much introspection
149 * on the PyObject (like getting its __class__, on a BGE type this means searching up
150 * the parent list each time only to discover its not a sequence.
151 * GameObjects are often used as an alternative to vectors so this is a common case
152 * better to do a quick check for it, likely the error below will be ignored.
154 * This is not 'correct' since we have proxy type CListValues's which could
155 * contain floats/ints but there no cases of CValueLists being this way
157 PyErr_Format(PyExc_AttributeError, "expected a sequence type");
160 else if (PySequence_Check(pyval))
162 unsigned int numitems = PySequence_Size(pyval);
163 if (numitems != Size(vec)) {
164 PyErr_Format(PyExc_AttributeError, "error setting vector, %d args, should be %d", numitems, Size(vec));
168 for (unsigned int x = 0; x < numitems; x++)
170 PyObject *item = PySequence_GetItem(pyval, x); /* new ref */
171 vec[x] = PyFloat_AsDouble(item);
175 if (PyErr_Occurred()) {
176 PyErr_SetString(PyExc_AttributeError, "one or more of the items in the sequence was not a float");
183 PyErr_Format(PyExc_AttributeError, "not a sequence type, expected a sequence of numbers size %d", Size(vec));
189 bool PyOrientationTo(PyObject* pyval, MT_Matrix3x3 &mat, const char *error_prefix);
192 * Converts an MT_Matrix4x4 to a python object.
194 PyObject* PyObjectFrom(const MT_Matrix4x4 &mat);
197 * Converts an MT_Matrix3x3 to a python object.
199 PyObject* PyObjectFrom(const MT_Matrix3x3 &mat);
202 * Converts an MT_Tuple2 to a python object.
204 PyObject* PyObjectFrom(const MT_Tuple2 &vec);
207 * Converts an MT_Tuple3 to a python object
209 PyObject* PyObjectFrom(const MT_Tuple3 &vec);
212 * Converts an MT_Tuple4 to a python object.
214 PyObject* PyObjectFrom(const MT_Tuple4 &pos);