diff options
Diffstat (limited to 'source/gameengine/Ketsji/KX_PyMath.h')
-rw-r--r-- | source/gameengine/Ketsji/KX_PyMath.h | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/source/gameengine/Ketsji/KX_PyMath.h b/source/gameengine/Ketsji/KX_PyMath.h index 39c9c358792..4a64063aaa1 100644 --- a/source/gameengine/Ketsji/KX_PyMath.h +++ b/source/gameengine/Ketsji/KX_PyMath.h @@ -92,18 +92,35 @@ bool PyMatTo(PyObject* pymat, T& mat) } /** - * Converts a python list to a MT class. + * Converts a python sequence to a MT class. */ template<class T> bool PyVecTo(PyObject* pyval, T& vec) { - if (PySequence_Check(pyval)) + + if(PyTuple_Check(pyval)) + { + unsigned int numitems = PyTuple_GET_SIZE(pyval); + if (numitems != Size(vec)) { + PyErr_Format(PyExc_AttributeError, "error setting vector, %d args, should be %d", numitems, Size(vec)); + return false; + } + + for (unsigned int x = 0; x < numitems; x++) + vec[x] = PyFloat_AsDouble(PyTuple_GET_ITEM(pyval, x)); /* borrow ref */ + + if (PyErr_Occurred()) { + PyErr_SetString(PyExc_AttributeError, "one or more of the items in the sequence was not a float"); + return false; + } + + return true; + } + else if (PySequence_Check(pyval)) { unsigned int numitems = PySequence_Size(pyval); if (numitems != Size(vec)) { - char err[128]; - sprintf(err, "error setting vector, %d args, should be %d", numitems, Size(vec)); - PyErr_SetString(PyExc_AttributeError, err); + PyErr_Format(PyExc_AttributeError, "error setting vector, %d args, should be %d", numitems, Size(vec)); return false; } @@ -122,9 +139,7 @@ bool PyVecTo(PyObject* pyval, T& vec) return true; } else { - char err[128]; - sprintf(err, "not a sequence type, expected a sequence of numbers size %d", Size(vec)); - PyErr_SetString(PyExc_AttributeError, err); + PyErr_Format(PyExc_AttributeError, "not a sequence type, expected a sequence of numbers size %d", Size(vec)); } return false; |