Blender Python ｜I cannot detect collisions of rigid bodies


I cannot detect collisions of rigid bodies.

I am using Blender 3.6.  
I have added a Plane and Cube to the viewport.  
I set the Plane as a passive rigid body and the Cube as an active rigid body.

I am trying to create a program to detect when the Cube collides (falls) onto
the Plane.  
However, it is not working.  
I want to print a message when the Cube collides with the Plane, but it
doesn't detect the collision.  
Please help. Here is my program:

[code]

    import bpy
    
    def add_plane_and_cube():
        # Add a Plane
        bpy.ops.mesh.primitive_plane_add(size=5)
        plane = bpy.context.active_object
        plane.location = (0, 0, 0)
    
        # Add a Cube
        bpy.ops.mesh.primitive_cube_add(size=2)
        cube = bpy.context.active_object
        cube.location = (0, 0, 10)
    
        # Set up rigid bodies
        bpy.context.view_layer.objects.active = plane
        bpy.ops.rigidbody.object_add(type='PASSIVE')
    
        bpy.context.view_layer.objects.active = cube
        bpy.ops.rigidbody.object_add(type='ACTIVE')
    
    def check_collision(scene):
        for obj in bpy.context.scene.objects:
            if obj.name == "Cube" and obj.rigid_body_constraint:
                # Get collision information
                contacts = obj.rigid_body_constraint.collisions
                
                # Check if there are collisions
                if contacts:
                    print('Cube has collided with the Plane')
    
    # Register the handler
    bpy.app.handlers.frame_change_post.append(check_collision)
    
    # Clear the scene and add new objects
    bpy.ops.object.select_all(action='DESELECT')
    bpy.ops.object.select_by_type(type='MESH')
    bpy.ops.object.delete()
    
    add_plane_and_cube()
    
    
[/code]

あ

