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__
34 #include "MT_Point2.h"
35 #include "MT_Point3.h"
36 #include "MT_Vector2.h"
37 #include "MT_Vector3.h"
38 #include "MT_Vector4.h"
39 #include "MT_Matrix3x3.h"
40 #include "MT_Matrix4x4.h"
42 #include "KX_Python.h"
43 #include "PyObjectPlus.h"
47 #include "../../blender/python/generic/Mathutils.h" /* so we can have mathutils callbacks */
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 BaseMath_ReadCallback(pyvec);
113 if (pyvec->size != Size(vec)) {
114 PyErr_Format(PyExc_AttributeError, "error setting vector, %d args, should be %d", pyvec->size, Size(vec));
117 vec.setValue((float *) pyvec->vec);
120 else if(QuaternionObject_Check(pyval)) {
121 QuaternionObject *pyquat= (QuaternionObject *)pyval;
122 BaseMath_ReadCallback(pyquat);
123 if (4 != Size(vec)) {
124 PyErr_Format(PyExc_AttributeError, "error setting vector, %d args, should be %d", 4, Size(vec));
127 /* xyzw -> wxyz reordering is done by PyQuatTo */
128 vec.setValue((float *) pyquat->quat);
131 else if(EulerObject_Check(pyval)) {
132 EulerObject *pyeul= (EulerObject *)pyval;
133 BaseMath_ReadCallback(pyeul);
134 if (3 != Size(vec)) {
135 PyErr_Format(PyExc_AttributeError, "error setting vector, %d args, should be %d", 3, Size(vec));
138 vec.setValue((float *) pyeul->eul);
142 if(PyTuple_Check(pyval))
144 unsigned int numitems = PyTuple_GET_SIZE(pyval);
145 if (numitems != Size(vec)) {
146 PyErr_Format(PyExc_AttributeError, "error setting vector, %d args, should be %d", numitems, Size(vec));
150 for (unsigned int x = 0; x < numitems; x++)
151 vec[x] = PyFloat_AsDouble(PyTuple_GET_ITEM(pyval, x)); /* borrow ref */
153 if (PyErr_Occurred()) {
154 PyErr_SetString(PyExc_AttributeError, "one or more of the items in the sequence was not a float");
160 else if (PyObject_TypeCheck(pyval, &PyObjectPlus::Type))
161 { /* note, include this check because PySequence_Check does too much introspection
162 * on the PyObject (like getting its __class__, on a BGE type this means searching up
163 * the parent list each time only to discover its not a sequence.
164 * GameObjects are often used as an alternative to vectors so this is a common case
165 * better to do a quick check for it, likely the error below will be ignored.
167 * This is not 'correct' since we have proxy type CListValues's which could
168 * contain floats/ints but there no cases of CValueLists being this way
170 PyErr_Format(PyExc_AttributeError, "expected a sequence type");
173 else if (PySequence_Check(pyval))
175 unsigned int numitems = PySequence_Size(pyval);
176 if (numitems != Size(vec)) {
177 PyErr_Format(PyExc_AttributeError, "error setting vector, %d args, should be %d", numitems, Size(vec));
181 for (unsigned int x = 0; x < numitems; x++)
183 PyObject *item = PySequence_GetItem(pyval, x); /* new ref */
184 vec[x] = PyFloat_AsDouble(item);
188 if (PyErr_Occurred()) {
189 PyErr_SetString(PyExc_AttributeError, "one or more of the items in the sequence was not a float");
196 PyErr_Format(PyExc_AttributeError, "not a sequence type, expected a sequence of numbers size %d", Size(vec));
203 bool PyQuatTo(PyObject* pyval, MT_Quaternion &qrot);
205 bool PyOrientationTo(PyObject* pyval, MT_Matrix3x3 &mat, const char *error_prefix);
208 * Converts an MT_Matrix4x4 to a python object.
210 PyObject* PyObjectFrom(const MT_Matrix4x4 &mat);
213 * Converts an MT_Matrix3x3 to a python object.
215 PyObject* PyObjectFrom(const MT_Matrix3x3 &mat);
218 * Converts an MT_Tuple2 to a python object.
220 PyObject* PyObjectFrom(const MT_Tuple2 &vec);
223 * Converts an MT_Tuple3 to a python object
225 PyObject* PyObjectFrom(const MT_Tuple3 &vec);
229 * Converts an MT_Quaternion to a python object.
231 PyObject* PyObjectFrom(const MT_Quaternion &qrot);
235 * Converts an MT_Tuple4 to a python object.
237 PyObject* PyObjectFrom(const MT_Tuple4 &pos);