0
$\begingroup$

I created a simulation of falling apples into a box. For research purposes, I want to be able to export the XYZ Coordinates of all apples (center of mass of objects, if possible) into a dataset, only for the last frame of the simulation.

enter image description here

I'm new to blender, and have a basic knowledge in Python. Any direction will do nicely.

$\endgroup$
1
1
$\begingroup$

Bake, then use the matrix world translation

As proposed here Is there any way to export the last frame of a physics simulation from command line?

bake the sim, then in this case instead of exporting, run thru the objects in the scene and print their global translation. in lieu of Formatting and writing data to a text file

Make the origin of the physics objects the center of mass when setting up.

import bpy
context = bpy.context

scene = context.scene
rbw = scene.rigidbody_world
pc = rbw.point_cache

# match bake to animation
print(pc.frame_start, pc.frame_end)


bpy.ops.ptcache.bake({"point_cache": pc}, bake=True)

scene.frame_set(250)
#bpy.ops.export_scene.obj(filepath="test.obj")

for ob in scene.objects:
    print(ob.name, ob.matrix_world.translation)

Result on default file, cube made a default rigid and dropped

Cube <Vector (0.0000, 0.0000, -460.8643)>
Lamp <Vector (1.7122, 1.0489, 8.5606)>
Camera <Vector (5.1171, -6.4642, 8.0004)>
$\endgroup$
1
  • $\begingroup$ Thank you! definitely enough for me to go on $\endgroup$ Jul 12 '20 at 10:29
0
$\begingroup$

It's easy, if you know python. Use text editor in Blender to write script, and play button (in script editor, not in timeline) to run it

enter image description here

Get selected objects from bpy.context.selected_objects. Then you can get location of object (in current frame) from its location property.

import bpy

for obj in bpy.context.selected_objects:
    print(obj.location)

When you run this script, you will find in terminal something like this:

enter image description here

Each individual component: obj.location.x, obj.location.y, obj.location.z

$\endgroup$
5
  • $\begingroup$ Looks more simple than I imagined, thanks! $\endgroup$ Jul 12 '20 at 10:29
  • $\begingroup$ This is great! But how do you run it on a terminal? I actually pressed play inside blender in the python editor and blender just prints bpy.ops.text.run_script() and nothing else. Blender 2.92.0 . Thanks $\endgroup$
    – iluvatar
    Oct 7 '21 at 15:33
  • $\begingroup$ @iluvatar are you running it in windows? If it so, you should be able to toggle system console in "window" menu $\endgroup$
    – Crantisz
    Oct 7 '21 at 15:50
  • $\begingroup$ @Crantisz No, in mac. I already found that in Mac i must run blender from the terminal. I did it. When I run the script from blender, I get the initial position of the sphere (in my case), but it does not update even if I animate it. When I play the script with the play button, it just prints the initial position but does not animate and does print the actual position, only the initial one. I need it to print the position of the sphere during the animation (I am trying to design an exercise where physics students play with the restitution coefficient in blender). Thanks for any tip $\endgroup$
    – iluvatar
    Oct 7 '21 at 15:58
  • $\begingroup$ Why don't you ask a question then? It doesn't work as you think. It launches only one time. If you want to run a script on every frame, you should make a function and add it to frame change handlers: docs.blender.org/api/current/bpy.app.handlers.html $\endgroup$
    – Crantisz
    Oct 7 '21 at 23: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.