Solved Compare Matrices Matrix with matrix.__eq__

Dear Developers,

I found this handy builtin

https://developers.maxon.net/docs/Cinema4DPythonSDK/html/modules/c4d/Matrix/index.html#Matrix.eq

but It always reports false. What am I missing?
thanks in advance.

parent = obj.GetUp()
print (parent.GetMg())
print (obj.GetMg())
print ( obj.GetMg().__eq__(parent.GetMg()) )

Matrix(v1: (1, 0, 0); v2: (0, 1, 0); v3: (0, 0, 1); off: (-33.9, -49.3, 152.75))
Matrix(v1: (1, 0, 0); v2: (0, 1, 0); v3: (0, 0, 1); off: (-33.9, -49.3, 152.75))
False

Hi @mogh I'm not able to reproduce our issue, here it's reliable, would it be possible to share with us a scene?

Just in case the __eq__ is the magic method for the == operator, so you could use obj.GetMg() == obj.GetUp().GetMg()

EDIT: After checking internal code, the == operator of Matrix, use the == operator of the Vector which then use the == operator of the C++ double. But due to Floating Point Issue this operation could fail.

So the best way to compare a Matrix is to manually check for each Vector component like so:

import c4d
import math

def compare_vec(v1, v2, eps=1e-5):
    values_to_compare = [(v1.x, v2.x), (v1.y, v2.y), (v1.z, v2.z)]
    for value_v1, value_v2 in values_to_compare:
        if not math.isclose(value_v1, value_v2, rel_tol=eps):
            return False
    
    return True

def compare_matrix(m1, m2, eps=1e-5):
    values_to_compare = [(m1.v1, m2.v1), (m1.v2, m2.v2), (m1.v3, m2.v3), (m1.off, m2.off)]
    for value_m1, value_m2 in values_to_compare:
        if not compare_vec(value_m1,value_m2, eps):
            return False
    
    return True

def main():
    print(compare_matrix(op.GetMg(), op.GetUp().GetMg()))

# Execute main()
if __name__=='__main__':
    main()

Documentation will be improved in this regards.

Cheers,
Maxime.

I was suspecting something like this ...
Thanks for taking the time to type a solution which I could copy ...