0
$\begingroup$

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, "sample.txt")
# confirm path exists
os.makedirs(os.path.dirname(filename), exist_ok=True)
# open a file to write to
text_file = open(filename, "w")

#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()
$\endgroup$
1
  • $\begingroup$ Hey, can you please elaborate? Where do I add this line? $\endgroup$ – Utkarsh Singh Oct 19 '20 at 17:14
2
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Thanks, man. :) $\endgroup$ – Utkarsh Singh Oct 19 '20 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.