Blender V2.61 - r43446
|
00001 /* 00002 * ***** BEGIN GPL LICENSE BLOCK ***** 00003 * 00004 * This program is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU General Public License 00006 * as published by the Free Software Foundation; either version 2 00007 * of the License, or (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software Foundation, 00016 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00017 * 00018 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00019 * All rights reserved. 00020 * 00021 * The Original Code is: all of this file. 00022 * 00023 * Contributor(s): none yet. 00024 * 00025 * ***** END GPL LICENSE BLOCK ***** 00026 * Initialize Python thingies. 00027 */ 00028 00034 #if defined(WIN32) && !defined(FREE_WINDOWS) 00035 #pragma warning (disable : 4786) 00036 #endif //WIN32 00037 00038 #ifdef WITH_PYTHON 00039 00040 #include "MT_Vector3.h" 00041 #include "MT_Vector4.h" 00042 #include "MT_Matrix4x4.h" 00043 #include "MT_Point2.h" 00044 00045 #include "ListValue.h" 00046 00047 #include "KX_Python.h" 00048 #include "KX_PyMath.h" 00049 00050 bool PyOrientationTo(PyObject* pyval, MT_Matrix3x3 &rot, const char *error_prefix) 00051 { 00052 int size= PySequence_Size(pyval); 00053 00054 if (size == 4) 00055 { 00056 MT_Quaternion qrot; 00057 if (PyQuatTo(pyval, qrot)) 00058 { 00059 rot.setRotation(qrot); 00060 return true; 00061 } 00062 } 00063 else if (size == 3) { 00064 /* 3x3 matrix or euler */ 00065 MT_Vector3 erot; 00066 if (PyVecTo(pyval, erot)) 00067 { 00068 rot.setEuler(erot); 00069 return true; 00070 } 00071 PyErr_Clear(); 00072 00073 if (PyMatTo(pyval, rot)) 00074 { 00075 return true; 00076 } 00077 } 00078 00079 PyErr_Format(PyExc_TypeError, "%s, could not set the orientation from a 3x3 matrix, quaternion or euler sequence", error_prefix); 00080 return false; 00081 } 00082 00083 bool PyQuatTo(PyObject* pyval, MT_Quaternion &qrot) 00084 { 00085 if(!PyVecTo(pyval, qrot)) 00086 return false; 00087 00088 /* annoying!, Blender/Mathutils have the W axis first! */ 00089 MT_Scalar w= qrot[0]; /* from python, this is actually the W */ 00090 qrot[0]= qrot[1]; 00091 qrot[1]= qrot[2]; 00092 qrot[2]= qrot[3]; 00093 qrot[3]= w; 00094 00095 return true; 00096 } 00097 00098 PyObject* PyObjectFrom(const MT_Matrix4x4 &mat) 00099 { 00100 #ifdef USE_MATHUTILS 00101 float fmat[16]; 00102 mat.getValue(fmat); 00103 return Matrix_CreatePyObject(fmat, 4, 4, Py_NEW, NULL); 00104 #else 00105 PyObject *collist = PyList_New(4); 00106 PyObject *col; 00107 int i; 00108 00109 for(i=0; i < 4; i++) { 00110 col = PyList_New(4); 00111 PyList_SET_ITEM(col, 0, PyFloat_FromDouble(mat[0][i])); 00112 PyList_SET_ITEM(col, 1, PyFloat_FromDouble(mat[1][i])); 00113 PyList_SET_ITEM(col, 2, PyFloat_FromDouble(mat[2][i])); 00114 PyList_SET_ITEM(col, 3, PyFloat_FromDouble(mat[3][i])); 00115 PyList_SET_ITEM(collist, i, col); 00116 } 00117 00118 return collist; 00119 #endif 00120 } 00121 00122 PyObject* PyObjectFrom(const MT_Matrix3x3 &mat) 00123 { 00124 #ifdef USE_MATHUTILS 00125 float fmat[9]; 00126 mat.getValue3x3(fmat); 00127 return Matrix_CreatePyObject(fmat, 3, 3, Py_NEW, NULL); 00128 #else 00129 PyObject *collist = PyList_New(3); 00130 PyObject *col; 00131 int i; 00132 00133 for(i=0; i < 3; i++) { 00134 col = PyList_New(3); 00135 PyList_SET_ITEM(col, 0, PyFloat_FromDouble(mat[0][i])); 00136 PyList_SET_ITEM(col, 1, PyFloat_FromDouble(mat[1][i])); 00137 PyList_SET_ITEM(col, 2, PyFloat_FromDouble(mat[2][i])); 00138 PyList_SET_ITEM(collist, i, col); 00139 } 00140 00141 return collist; 00142 #endif 00143 } 00144 00145 #ifdef USE_MATHUTILS 00146 PyObject* PyObjectFrom(const MT_Quaternion &qrot) 00147 { 00148 /* NOTE, were re-ordering here for Mathutils compat */ 00149 float fvec[4]= {qrot[3], qrot[0], qrot[1], qrot[2]}; 00150 return Quaternion_CreatePyObject(fvec, Py_NEW, NULL); 00151 } 00152 #endif 00153 00154 PyObject* PyObjectFrom(const MT_Tuple4 &vec) 00155 { 00156 #ifdef USE_MATHUTILS 00157 float fvec[4]= {vec[0], vec[1], vec[2], vec[3]}; 00158 return Vector_CreatePyObject(fvec, 4, Py_NEW, NULL); 00159 #else 00160 PyObject *list = PyList_New(4); 00161 PyList_SET_ITEM(list, 0, PyFloat_FromDouble(vec[0])); 00162 PyList_SET_ITEM(list, 1, PyFloat_FromDouble(vec[1])); 00163 PyList_SET_ITEM(list, 2, PyFloat_FromDouble(vec[2])); 00164 PyList_SET_ITEM(list, 3, PyFloat_FromDouble(vec[3])); 00165 return list; 00166 #endif 00167 } 00168 00169 PyObject* PyObjectFrom(const MT_Tuple3 &vec) 00170 { 00171 #ifdef USE_MATHUTILS 00172 float fvec[3]= {vec[0], vec[1], vec[2]}; 00173 return Vector_CreatePyObject(fvec, 3, Py_NEW, NULL); 00174 #else 00175 PyObject *list = PyList_New(3); 00176 PyList_SET_ITEM(list, 0, PyFloat_FromDouble(vec[0])); 00177 PyList_SET_ITEM(list, 1, PyFloat_FromDouble(vec[1])); 00178 PyList_SET_ITEM(list, 2, PyFloat_FromDouble(vec[2])); 00179 return list; 00180 #endif 00181 } 00182 00183 PyObject* PyObjectFrom(const MT_Tuple2 &vec) 00184 { 00185 #ifdef USE_MATHUTILS 00186 float fvec[2]= {vec[0], vec[1]}; 00187 return Vector_CreatePyObject(fvec, 2, Py_NEW, NULL); 00188 #else 00189 PyObject *list = PyList_New(2); 00190 PyList_SET_ITEM(list, 0, PyFloat_FromDouble(vec[0])); 00191 PyList_SET_ITEM(list, 1, PyFloat_FromDouble(vec[1])); 00192 return list; 00193 #endif 00194 } 00195 00196 #endif // WITH_PYTHON