How can I get the location of all the objects in the scene at a desired keyframe using script? I have baked the attached simulation for 500 frames with some keyframes inserted for each object where it follows an animated path after which it free falls according to Blender's rigid body physics. How do I get the location of each of the objects in the scene at frame 500?
This is something I've tried but it outputs the locations at frame 1.
I've attached the .blend file.

import bpy, os

bpy.context.scene.frame_set(500)
#select all objects in scene
bpy.ops.object.select_all(action='SELECT')

SEL_OBJS = bpy.context.selected_objects

# get path to render output (usually /tmp\)
tempFolder = os.path.abspath (bpy.context.scene.render.filepath)
# make a filename
filename = os.path.join (tempFolder, &quot;sample.txt&quot;)
# confirm path exists
os.makedirs(os.path.dirname(filename), exist_ok=True)
# open a file to write to
text_file = open(filename, &quot;w&quot;)

#loop through all selected objects
for OBJ in SEL_OBJS:
    text_file.write('OBJECT NAME: '+OBJ.name+'\n')
    text_file.write('COORDINATES: '+str(OBJ.location.x)+', '+str(OBJ.location.y)+', '+str(OBJ.location.z)+'\n')
    text_file.write('ROTATION: '+str(OBJ.rotation_euler.x)+', '+str(OBJ.rotation_euler.y)+', '+str(OBJ.rotation_euler.z)+'\n\n')

# close the file    
text_file.close()
the obj.location results in the local location, not including parenting relationships. Same goes for the rotation_euler. 
loc = OBJ.matrix_world.translation
rot = OBJ.matrix_world.to_euler()
text_file.write('COORDINATES:', str(loc.x), ',', str(loc.y),',', str(loc.z) + '\n')

the matrix_world returns the actual world coordinates of the object after calculating all parenting and constraints.