[Bf-python] Python can do operator overloading!

Yann Vernier yann at algonet.se
Mon Oct 11 14:42:32 CEST 2004


On Wed, Oct 06, 2004 at 01:51:32PM -0700, Ted Schundler wrote:
> #I have to alter the Matrix I get from blenderObject before using it
> at as mathutils matrix?
> omtx=blenderObject.getMatrix()
> matrix=Mathutils.Matrix(list(omtx[0]),list(omtx[1]),list(omtx[2]),list(omtx[3]))

Clumsy indeed, and I agree that automatic list->matrix conversions would
be wonderful. Just thought I'd give you a tip on how to write this
version though:

matrix=Mathutils.Matrix(*map(list, omtx))

> And while I'm ranting, having MatMulVec type operations support lists
> of vectors an operate on all of them would be a nice speed
> enchancement, since the inner loop is in C then. And in that case it
> is even more helpful for Vertex to be a subclass of Vector.

Nice, yes, but Python does have relatively efficient loop constructs. An
example from my LDraw import script:
      self.verts.extend([
                tuple(MatMultVec(matrix, Vector(list(v[0:3])+[1]))[0:3])
		for v in model.verts])
That's constructing a list on the fly. Not the most efficient way to
convert between 3 and 4 value vectors, I'll admit. That's another thing
that might be done as a convenience, perhaps. I store as tuple so I
can hash during duplicate vertex removal, which uses a dictionary.

You could have the functions attempt to construct matrices or vectors on
any other type objects supporting the sequence protocol. The
list(v[0:3])+[1] construct is possibly the most wasteful part of the
loop I showed.

-- 
PGP fingerprint = 9242 DC15 2502 FEAB E15F  84C6 D538 EC09 5380 5746
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.blender.org/pipermail/bf-python/attachments/20041011/dce8a897/attachment.sig>


More information about the Bf-python mailing list