Replacing Mathutils for old Blender files (2.47)

Hello, it’s interesting how with all the excitement towards Blender 2.49 and 2.50 my main task today was to port a file from Blender 2.48 back to Blender 2.47.

The only problem I had it’s that I was relying on Mathutils to calculate the angle between two vectors.

My original code was:


# #############################################################
# uses a raycast sensor which looks for an object with property "ground"
# #############################################################

from Mathutils import Vector, AngleBetweenVecs

import GameLogic as G

cont = G.getCurrentController()
own = cont.getOwner()
act_move = cont.getActuator('Movement')

pulse = 120 # the time delay ...

fromPos = own.getPosition()
fromPos[2] += 5.0

toPos = fromPos[:]
toPos[2] -= 100.0

cast = own.rayCast(toPos, fromPos, 0, "ground")

if cast[1]:

    axisY = own.getAxisVect([0,1,0])
  <b>  angle = AngleBetweenVecs(Vector(cast[2]), Vector(axisY))</b>
    angle -= 90
    
    rotX = angle * 0.02 / pulse
    act_move.setDRot(rotX,0.0,0.0,1)

But I can’t import Mathutils anymore (it’s not supported in Blender 2.47) :confused:
So what could I do? What did I do?

Well, I opened the Blender source code, found the file Mathutils.c and … converted the AngleBetweenVecs function to python =D


# m_Mathutils.py
import math

def saacos(fac):
#safe acos
    if(fac &lt;= -1.0):
        return math.pi
    
    elif (fac &gt;= 1.0):
        return 0.0
    else:
        return math.acos(fac)


def Normalize (vec):
    length = math.sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2])
    if (length != 0.0):
        vec[0] /= length
        vec[1] /= length
        vec[2] /= length
        
def AngleBetweenVecs (vec1, vec2):
    dot = 0.0
    angleRads = 0.0
    test_v1 = 0.0
    test_v2 = 0.0

    Normalize(vec1)
    Normalize(vec2)
    
    for x in range(3):
        test_v1 += vec1[x] * vec1[x]
        test_v2 += vec2[x] * vec2[x]

#    if (!test_v1 || !test_v2):
#        print "error the vector are the same (impossible !!!)"
#        return False

    #dot product
    for x in range(3):
        dot += vec1[x] * vec2[x]

    dot /= (math.sqrt(test_v1) * math.sqrt(test_v2))

    angleRads = saacos(dot)

    return (angleRads * (180/ math.pi))

Now I can run from m_Mathutils import AngleBetweenVecs and then use angle = AngleBetweenVecs(cast[2], axisY) without problems :slight_smile:

Tcharan :slight_smile: It’s really handy to have access to the source code !
Cheers