2
* $Id: KX_PyMath.h,v 1.6 2005/03/09 19:45:59 lukep Exp $
4
* ***** BEGIN GPL/BL DUAL 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. The Blender
10
* Foundation also sells licenses for use in proprietary software under
11
* the Blender License. See http://www.blender.org/BL/ for information
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with this program; if not, write to the Free Software Foundation,
21
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24
* All rights reserved.
26
* The Original Code is: all of this file.
28
* Contributor(s): none yet.
30
* ***** END GPL/BL DUAL LICENSE BLOCK *****
31
* Initialize Python thingies.
34
#ifndef __KX_PYMATH_H__
35
#define __KX_PYMATH_H__
37
#include "MT_Point2.h"
38
#include "MT_Point3.h"
39
#include "MT_Vector2.h"
40
#include "MT_Vector3.h"
41
#include "MT_Vector4.h"
42
#include "MT_Matrix3x3.h"
43
#include "MT_Matrix4x4.h"
45
#include "KX_Python.h"
47
inline unsigned int Size(const MT_Matrix4x4&) { return 4; }
48
inline unsigned int Size(const MT_Matrix3x3&) { return 3; }
49
inline unsigned int Size(const MT_Tuple2&) { return 2; }
50
inline unsigned int Size(const MT_Tuple3&) { return 3; }
51
inline unsigned int Size(const MT_Tuple4&) { return 4; }
54
* Converts the given python matrix to an MT class.
57
bool PyMatTo(PyObject* pymat, T& mat)
61
if (PySequence_Check(pymat))
63
unsigned int rows = PySequence_Size(pymat);
64
if (rows != Size(mat))
67
for (unsigned int y = 0; noerror && y < Size(mat); y++)
69
PyObject *pyrow = PySequence_GetItem(pymat, y); /* new ref */
70
if (!PyErr_Occurred() && PySequence_Check(pyrow))
72
unsigned int cols = PySequence_Size(pyrow);
73
if (cols != Size(mat))
77
for( unsigned int x = 0; x < Size(mat); x++)
79
PyObject *item = PySequence_GetItem(pyrow, x); /* new ref */
80
mat[y][x] = PyFloat_AsDouble(item);
95
* Converts a python list to a MT class.
98
bool PyVecTo(PyObject* pyval, T& vec)
100
if (PySequence_Check(pyval))
102
unsigned int numitems = PySequence_Size(pyval);
103
if (numitems != Size(vec))
106
for (unsigned int x = 0; x < numitems; x++)
108
PyObject *item = PySequence_GetItem(pyval, x); /* new ref */
109
vec[x] = PyFloat_AsDouble(item);
120
* Converts a python argument to an MT class.
121
* This paramater expects arguments as passed to a python method.
124
bool PyVecArgTo(PyObject* args, T& vec)
127
if (PyArg_ParseTuple(args,"O",&pylist))
128
return PyVecTo(pylist, vec);
134
* Converts an MT_Matrix4x4 to a python object.
136
PyObject* PyObjectFrom(const MT_Matrix4x4 &mat);
139
* Converts an MT_Matrix3x3 to a python object.
141
PyObject* PyObjectFrom(const MT_Matrix3x3 &mat);
144
* Converts an MT_Tuple2 to a python object.
146
PyObject* PyObjectFrom(const MT_Tuple2 &vec);
149
* Converts an MT_Tuple3 to a python object
151
PyObject* PyObjectFrom(const MT_Tuple3 &vec);
154
* Converts an MT_Tuple4 to a python object.
156
PyObject* PyObjectFrom(const MT_Tuple4 &pos);
159
* True if the given PyObject can be converted to an MT_Matrix
160
* @param rank = 3 (for MT_Matrix3x3) or 4 (for MT_Matrix4x4)
162
bool PyObject_IsMT_Matrix(PyObject *pymat, unsigned int rank);