~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to source/gameengine/Ketsji/KX_PyMath.h

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
61
61
 *  Converts the given python matrix (column-major) to an MT class (row-major).
62
62
 */
63
63
template<class T>
64
 
bool PyMatTo(PyObject* pymat, T& mat)
 
64
bool PyMatTo(PyObject *pymat, T& mat)
65
65
{
66
66
        bool noerror = true;
67
67
        mat.setIdentity();
104
104
                        if (!PyErr_Occurred() && PySequence_Check(pyrow))
105
105
                        {
106
106
                                unsigned int cols = PySequence_Size(pyrow);
107
 
                                if (cols != Size(mat))
 
107
                                if (cols != Size(mat)) {
108
108
                                        noerror = false;
109
 
                                else
110
 
                                {
111
 
                                        for( unsigned int col = 0; col < cols; col++)
112
 
                                        {
 
109
                                }
 
110
                                else {
 
111
                                        for (unsigned int col = 0; col < cols; col++) {
113
112
                                                PyObject *item = PySequence_GetItem(pyrow, col); /* new ref */
114
113
                                                mat[row][col] = PyFloat_AsDouble(item);
115
114
                                                Py_DECREF(item);
116
115
                                        }
117
116
                                }
118
 
                        } else 
 
117
                        }
 
118
                        else {
119
119
                                noerror = false;
 
120
                        }
120
121
                        Py_DECREF(pyrow);
121
122
                }
122
123
        } else 
132
133
 *  Converts a python sequence to a MT class.
133
134
 */
134
135
template<class T>
135
 
bool PyVecTo(PyObject* pyval, T& vec)
 
136
bool PyVecTo(PyObject *pyval, T& vec)
136
137
{
137
138
#ifdef USE_MATHUTILS
138
139
        /* no need for BaseMath_ReadCallback() here, reading the sequences will do this */
139
140
        
140
 
        if(VectorObject_Check(pyval)) {
 
141
        if (VectorObject_Check(pyval)) {
141
142
                VectorObject *pyvec= (VectorObject *)pyval;
142
 
                if(BaseMath_ReadCallback(pyvec) == -1) {
 
143
                if (BaseMath_ReadCallback(pyvec) == -1) {
143
144
                        return false; /* exception raised */
144
145
                }
145
146
                if (pyvec->size != Size(vec)) {
149
150
                vec.setValue((float *) pyvec->vec);
150
151
                return true;
151
152
        }
152
 
        else if(QuaternionObject_Check(pyval)) {
 
153
        else if (QuaternionObject_Check(pyval)) {
153
154
                QuaternionObject *pyquat= (QuaternionObject *)pyval;
154
 
                if(BaseMath_ReadCallback(pyquat) == -1) {
 
155
                if (BaseMath_ReadCallback(pyquat) == -1) {
155
156
                        return false; /* exception raised */
156
157
                }
157
158
                if (4 != Size(vec)) {
162
163
                vec.setValue((float *) pyquat->quat);
163
164
                return true;
164
165
        }
165
 
        else if(EulerObject_Check(pyval)) {
 
166
        else if (EulerObject_Check(pyval)) {
166
167
                EulerObject *pyeul= (EulerObject *)pyval;
167
 
                if(BaseMath_ReadCallback(pyeul) == -1) {
 
168
                if (BaseMath_ReadCallback(pyeul) == -1) {
168
169
                        return false; /* exception raised */
169
170
                }
170
171
                if (3 != Size(vec)) {
173
174
                }
174
175
                vec.setValue((float *) pyeul->eul);
175
176
                return true;
176
 
        } else
 
177
        }
 
178
        else
177
179
#endif
178
 
        if(PyTuple_Check(pyval))
179
 
        {
 
180
        if (PyTuple_Check(pyval)) {
180
181
                unsigned int numitems = PyTuple_GET_SIZE(pyval);
181
182
                if (numitems != Size(vec)) {
182
183
                        PyErr_Format(PyExc_AttributeError, "error setting vector, %d args, should be %d", numitems, Size(vec));
193
194
                
194
195
                return true;
195
196
        }
196
 
        else if (PyObject_TypeCheck(pyval, (PyTypeObject *)&PyObjectPlus::Type))
197
 
        {       /* note, include this check because PySequence_Check does too much introspection
 
197
        else if (PyObject_TypeCheck(pyval, (PyTypeObject *)&PyObjectPlus::Type)) {
 
198
                /* note, include this check because PySequence_Check does too much introspection
198
199
                 * on the PyObject (like getting its __class__, on a BGE type this means searching up
199
200
                 * the parent list each time only to discover its not a sequence.
200
201
                 * GameObjects are often used as an alternative to vectors so this is a common case
206
207
                PyErr_Format(PyExc_AttributeError, "expected a sequence type");
207
208
                return false;
208
209
        }
209
 
        else if (PySequence_Check(pyval))
210
 
        {
 
210
        else if (PySequence_Check(pyval)) {
211
211
                unsigned int numitems = PySequence_Size(pyval);
212
212
                if (numitems != Size(vec)) {
213
213
                        PyErr_Format(PyExc_AttributeError, "error setting vector, %d args, should be %d", numitems, Size(vec));
214
214
                        return false;
215
215
                }
216
216
                
217
 
                for (unsigned int x = 0; x < numitems; x++)
218
 
                {
 
217
                for (unsigned int x = 0; x < numitems; x++) {
219
218
                        PyObject *item = PySequence_GetItem(pyval, x); /* new ref */
220
219
                        vec[x] = PyFloat_AsDouble(item);
221
220
                        Py_DECREF(item);
227
226
                }
228
227
                
229
228
                return true;
230
 
        } else
231
 
        {
 
229
        }
 
230
        else {
232
231
                PyErr_Format(PyExc_AttributeError, "not a sequence type, expected a sequence of numbers size %d", Size(vec));
233
232
        }
234
233
        
236
235
}
237
236
 
238
237
 
239
 
bool PyQuatTo(PyObject* pyval, MT_Quaternion &qrot);
 
238
bool PyQuatTo(PyObject *pyval, MT_Quaternion &qrot);
240
239
 
241
 
bool PyOrientationTo(PyObject* pyval, MT_Matrix3x3 &mat, const char *error_prefix);
 
240
bool PyOrientationTo(PyObject *pyval, MT_Matrix3x3 &mat, const char *error_prefix);
242
241
 
243
242
/**
244
243
 * Converts an MT_Matrix4x4 to a python object.
245
244
 */
246
 
PyObject* PyObjectFrom(const MT_Matrix4x4 &mat);
 
245
PyObject *PyObjectFrom(const MT_Matrix4x4 &mat);
247
246
 
248
247
/**
249
248
 * Converts an MT_Matrix3x3 to a python object.
250
249
 */
251
 
PyObject* PyObjectFrom(const MT_Matrix3x3 &mat);
 
250
PyObject *PyObjectFrom(const MT_Matrix3x3 &mat);
252
251
 
253
252
/**
254
253
 * Converts an MT_Tuple2 to a python object.
255
254
 */
256
 
PyObject* PyObjectFrom(const MT_Tuple2 &vec);
 
255
PyObject *PyObjectFrom(const MT_Tuple2 &vec);
257
256
 
258
257
/**
259
258
 * Converts an MT_Tuple3 to a python object
260
259
 */
261
 
PyObject* PyObjectFrom(const MT_Tuple3 &vec);
 
260
PyObject *PyObjectFrom(const MT_Tuple3 &vec);
262
261
 
263
262
#ifdef USE_MATHUTILS
264
263
/**
265
264
 * Converts an MT_Quaternion to a python object.
266
265
 */
267
 
PyObject* PyObjectFrom(const MT_Quaternion &qrot);
 
266
PyObject *PyObjectFrom(const MT_Quaternion &qrot);
268
267
#endif
269
268
 
270
269
/**
271
270
 * Converts an MT_Tuple4 to a python object.
272
271
 */
273
 
PyObject* PyObjectFrom(const MT_Tuple4 &pos);
 
272
PyObject *PyObjectFrom(const MT_Tuple4 &pos);
274
273
 
275
274
#endif
276
275
 
277
 
#endif // WITH_PYTHON
 
276
#endif  /* WITH_PYTHON */