[Bf-blender-cvs] [06b6cd8] master: PyAPI: mathutils.Vector.cross now works for 2d vectors (returns a float)

Campbell Barton noreply at git.blender.org
Wed Feb 5 13:36:54 CET 2014


Commit: 06b6cd83459713ef5c00f705f6cdf1481ed24179
Author: Campbell Barton
Date:   Wed Feb 5 23:32:51 2014 +1100
https://developer.blender.org/rB06b6cd83459713ef5c00f705f6cdf1481ed24179

PyAPI: mathutils.Vector.cross now works for 2d vectors (returns a float)

also fixed crash when running on large vectors (raises exception now)

===================================================================

M	source/blender/python/mathutils/mathutils_Vector.c

===================================================================

diff --git a/source/blender/python/mathutils/mathutils_Vector.c b/source/blender/python/mathutils/mathutils_Vector.c
index 68a070e..1924697 100644
--- a/source/blender/python/mathutils/mathutils_Vector.c
+++ b/source/blender/python/mathutils/mathutils_Vector.c
@@ -855,30 +855,36 @@ PyDoc_STRVAR(Vector_cross_doc,
 "   :arg other: The other vector to perform the cross product with.\n"
 "   :type other: :class:`Vector`\n"
 "   :return: The cross product.\n"
-"   :rtype: :class:`Vector`\n"
+"   :rtype: :class:`Vector` or float when 2D vectors are used\n"
 "\n"
-"   .. note:: both vectors must be 3D\n"
+"   .. note:: both vectors must be 2D or 3D\n"
 );
 static PyObject *Vector_cross(VectorObject *self, PyObject *value)
 {
-	VectorObject *ret;
-	float tvec[MAX_DIMENSIONS];
+	PyObject *ret;
+	float tvec[3];
 
 	if (BaseMath_ReadCallback(self) == -1)
 		return NULL;
 
-	if (mathutils_array_parse(tvec, self->size, self->size, value, "Vector.cross(other), invalid 'other' arg") == -1)
-		return NULL;
-
-	if (self->size != 3) {
+	if (self->size > 3) {
 		PyErr_SetString(PyExc_ValueError,
-		                "Vector must be 3D");
+		                "Vector must be 2D or 3D");
 		return NULL;
 	}
 
-	ret = (VectorObject *)Vector_CreatePyObject(NULL, 3, Py_NEW, Py_TYPE(self));
-	cross_v3_v3v3(ret->vec, self->vec, tvec);
-	return (PyObject *)ret;
+	if (mathutils_array_parse(tvec, self->size, self->size, value, "Vector.cross(other), invalid 'other' arg") == -1)
+		return NULL;
+
+	if (self->size == 3) {
+		ret = Vector_CreatePyObject(NULL, 3, Py_NEW, Py_TYPE(self));
+		cross_v3_v3v3(((VectorObject *)ret)->vec, self->vec, tvec);
+	}
+	else {
+		/* size == 2 */
+		ret = PyFloat_FromDouble(cross_v2v2(self->vec, tvec));
+	}
+	return ret;
 }
 
 PyDoc_STRVAR(Vector_dot_doc,




More information about the Bf-blender-cvs mailing list