[Bf-python] Python can do operator overloading!

Ted Schundler tschundler at gmail.com
Wed Oct 6 22:51:32 CEST 2004


Operator overloading is already used for stuff like Vector + Vector.
And from the C API, it can also support function overloading, which is
something I'd really like to see used to add more flexibility to the
math functions, mostly to avois stuff like this:

#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]))

Or even MatMulVec type operations would be nice if they could take a
list of lists and a list/tuple as their inputs. Just to reduce the
extra pointless coding on the part of script writers.

Somewhat related, it would be nice if Vertex was a subclass of Vector.
So then I can just use a vertex directly in math operations.

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.

Anyways, just my thoughts on the whole overloading & subclassing thing.


Ted

On Wed, 6 Oct 2004 01:54:19 +0300 (EEST), Toni Alatalo
<antont at kyperjokki.fi> wrote:
> On Tue, 5 Oct 2004, joeedh wrote:
> 
> > Go to http://docs.python.org/lib/operator-map.html, and implement these
> > functions (except as __[func]__) in your class, and you'll have instant
> > operator overloading.  I love python!
> 
> :)
> 
> soya3d is using these pretty nicely for 3d:
> (from the tutorial/basic-4.py, available from
> http://home.gna.org/oomadness/en/soya/index.html)
> 
> # Lots of Soya methods have also an operator :
> #
> # Position +  Vector    => Point
> # Position += Vector     Position.add_vector(Vector)
> # Position >> Position   Position.vector_to (Position) => Vector
> # Position %= CoordSyst  Position.convert_to(CoordSyst)
> # Vector   *  float
> 
> # Position +  Vector => Point
> 
> # Position += Vector
> # Position.add_vector(Vector)
> # Translation or vectorial addition (if the Position is a Vector).
> 
> # Position >> Position => Vector
> # Position.vector_to (Position) => Vector
> # Creates a vector from a strating and an ending position.
> 
> # Position %= CoordSyst  Position.convert_to(CoordSyst)
> # Vector   *  float
> 
> > joeedh
> 
> ~Toni
> 
> 
> 
> _______________________________________________
> Bf-python mailing list
> Bf-python at projects.blender.org
> http://projects.blender.org/mailman/listinfo/bf-python
>



More information about the Bf-python mailing list