[Bf-committers] Proposal: Mathutils types default in the BGE

Campbell Barton ideasman42 at gmail.com
Wed Jun 24 00:51:43 CEST 2009

for blender 2.5 Im proposing to change the default return types in the
BGE to use Mathutils types, even though there are not many people
working in this area I probably should give others a chance to give

Currently a vector, quat, matrix in the BGE are simple python types,
even though builtin types have a lot of advantages in that scripters
know how they work and are less trouble to maintain,
this makes common tasks like finding the direction from one object to
another or a point between then fairly tedious.

Something I have seen reported as a bug is that you cant directly
assign an axis -
 ob.position[2] = 10.0 # wont work

You need to do this..
 loc= ob.position
 loc[2] = 10.0
 ob.position= loc

Another annoyance is that you need to manually write your own vector
matrix multiplication function.
I could give many more examples as to why this is a problem but Im
sure BGE users are familiar with this.

Making this change WILL BREAK SCRIPTS, (we will most likely be using
python3.x and updating deprecated functions, users will be in some
pain already) ok, got that out of the way...

Options are....
* Do nothing, script writers can convert to mathutils types if they
want - Mathutils.Vector(ob.position), ok for vec*matrix, doesn't help
with axis assignment.
* Write new C++ python math classes that wrap Moto
* use Mathutils, ugly well tested C utility module lots of useful
utilities like vector.reflect(other), vec.zyx = other.xyz, already
written and has docs.
* use numpy, has the disadvantage that we cant integrate callbacks
afaik... see below.

The problem with adding Mathutils types is that they are not
compatible with Moto C++ math types which use doubles instead of
floats, quat axis order is different. Matrix col/row need to be

An easy way around this is to always return a new
Matrix/Vector/Quat/Euler and convert from Moto on creation but again,
this makes "ob.position[0] = 0" fail.

Recently I tested a method that uses callbacks, which synchronizes
between the BGE type and the Mathutils type.

the callbacks are done so Mathutils doesn't need to know about BGE
types (or RNA), it just calls a registered callback to sync from/to.
While this is a bit annoying to have the overhead of syncing between
different types, I think the python interpreter running this would
incur a negligible performance hit.

using callbacks has an advantage over wrapped types that it can detect
when for eg the KX_GameObject has been removed, thin wrapping could
easily crash in this case by accessing an invalid pointer.

This method has also being used with RNA math types so Im hoping even
though mathutils is a bit of a kludge, it can be re-used in different
areas wile not needing to have BGE or RNA specific functions.

Theres nothing stopping anyone from porting mathutils to C++, cython
or using Moto rather then arithb.c functions but Im not planning on
- Campbell

More information about the Bf-committers mailing list