0

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.

batFINGER
  • 84,216
  • 10
  • 108
  • 233
  • Related https://blender.stackexchange.com/questions/112029/is-there-any-way-to-export-the-last-frame-of-a-physics-simulation-from-command-l – batFINGER Jul 12 '20 at 10:09

2 Answers2

1

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)>
batFINGER
  • 84,216
  • 10
  • 108
  • 233
0

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

Crantisz
  • 35,244
  • 2
  • 37
  • 89
  • Looks more simple than I imagined, thanks! – Nadav Halali Jul 12 '20 at 10:29
  • 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 – iluvatar Oct 07 '21 at 15:33
  • @iluvatar are you running it in windows? If it so, you should be able to toggle system console in "window" menu – Crantisz Oct 07 '21 at 15:50
  • @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 – iluvatar Oct 07 '21 at 15:58
  • 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: https://docs.blender.org/api/current/bpy.app.handlers.html – Crantisz Oct 07 '21 at 23:58