Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Register
    • Login

    Compare Matrices Matrix with matrix.__eq__

    Cinema 4D SDK
    r25 python
    2
    3
    451
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M
      mogh
      last edited by

      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
      
      1 Reply Last reply Reply Quote 0
      • M
        m_adam
        last edited by m_adam

        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[URL-REMOVED] 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.


        [URL-REMOVED] @maxon: This section contained a non-resolving link which has been removed.

        MAXON SDK Specialist

        Development Blog, MAXON Registered Developer

        1 Reply Last reply Reply Quote 1
        • M
          mogh
          last edited by

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

          1 Reply Last reply Reply Quote 0
          • First post
            Last post