[Bf-python] Update

Joseph Gilbert models at paposo.com
Sun Jan 11 19:28:24 CET 2004


Hi,

I have been fiddling with the vector class.  I've added a set of numeric
protocols to the vector class so that it can be treated as a number and as a
sequence. You can also create vectors with ints or floats or a mix of ints
and floats.  Ints are cast to floats internally. Vector object also has
internal methods for self operations.  The neat thing is that no changes
need to be made anywhere. Any module that calls newVectorObject() will
return a vector with all the functionality of the vector class with internal
methods and numeric protocols. Order of operations is preserved. So you can
do this stuff:

vecA = vecB + vecC / 5
print vecA.length

or
vecA = vecB * vecC[3]

or
vecSmall.resize3D()
vecSmall = vecSmall * vecLarge.length
vecSmall.normalize()

I need to do the same for matrix (and maybe a quat and euler)
You can only create the vector object current through the Mathutils module
or any other module that calls return newVectorObject() if it chooses to do
so.
Anyway it looks pretty freaking sweet. :)

The trick of the matter with the numeric protocols is to 'coerce' non vector
types to vector types for numeric operations. 2D, 3D and 4D vectors are
supported.

Code for vector creation:

static PyObject *M_Mathutils_Vector(PyObject *self, PyObject *args)
{
	PyObject *listObject;
	int tempFloat;
	float *vec;
	int x;

	if (!PyArg_ParseTuple(args, "O!", &PyList_Type, &listObject))
		return (EXPP_ReturnPyObjError (PyExc_TypeError,
						"expected list"));

	//2D 3D 4D supported
	if(PyList_Size(listObject) != 2 && PyList_Size(listObject) != 3 &&
PyList_Size(listObject) != 4)
		return (EXPP_ReturnPyObjError (PyExc_TypeError,	"2D, 3D and 4D vectors
supported"));

	//check for int/floats in series
	for (x = 0; x < PyList_Size(listObject); x++) {
		PyObject* checkOb = PyList_GetItem(listObject, x);
		if(!PyFloat_Check(checkOb)){
			if(!PyInt_Check(checkOb)){
				return EXPP_ReturnPyObjError (PyExc_TypeError, "list contains members
other than float or int");
			}
		}
	}

	//allocate memory
	vec = PyMem_Malloc (PyList_Size(listObject)*sizeof (float));

	if(!EXPP_check_sequence_consistency(listObject, &PyFloat_Type)){
		if(!EXPP_check_sequence_consistency(listObject, &PyInt_Type)){
			//mixed int and float series
			for (x = 0; x < PyList_Size(listObject); x++) {
				PyObject* tempOb = PyList_GetItem(listObject, x);
				if(!PyFloat_Check(tempOb)){
					if(!PyInt_Check(tempOb)){
					}else{
						if (!PyArg_Parse(PyList_GetItem(listObject, x), "i", &tempFloat)){
							return EXPP_ReturnPyObjError (PyExc_TypeError, "python list not
parseable");
						}
						vec[x] = ((float)tempFloat);
					}
				}else{
					if (!PyArg_Parse(PyList_GetItem(listObject, x), "f", &vec[x])){
						return EXPP_ReturnPyObjError (PyExc_TypeError, "python list not
parseable");
					}
				}
			}
		}else{ //int series
			for (x = 0; x < PyList_Size(listObject); x++) {
				if (!PyArg_Parse(PyList_GetItem(listObject, x), "i", &tempFloat)){
					return EXPP_ReturnPyObjError (PyExc_TypeError, "python list not
parseable");
				}
				vec[x] = ((float)tempFloat);
			}
		}
	}else{ //float series
		for (x = 0; x < PyList_Size(listObject); x++) {
			if (!PyArg_Parse(PyList_GetItem(listObject, x), "f", &vec[x])){
				return EXPP_ReturnPyObjError (PyExc_TypeError, "python list not
parseable");
			}
		}
	}
	return (PyObject *)newVectorObject(vec, PyList_Size(listObject));
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Vector.zip
Type: application/x-zip-compressed
Size: 5686 bytes
Desc: not available
URL: <http://lists.blender.org/pipermail/bf-python/attachments/20040111/02074176/attachment.bin>


More information about the Bf-python mailing list