I want to return individual vertex location of a rigid body during physics simulation frame by frame, for either a baked or unbaked animation, in global coordinates with pivot point set at (0, 0, 0).
Is there a simple line of code to do this?
I want to return individual vertex location of a rigid body during physics simulation frame by frame, for either a baked or unbaked animation, in global coordinates with pivot point set at (0, 0, 0).
Is there a simple line of code to do this?
Generally you can access the object's pivot point (location) via its world transformation matrix (matrix_world.translation). This value also reflects any constraints, simulations and transformations (location, rotation, scale) on the object.
You can use the world matrix to also find each vertex's global location in each frame, by multiplying it by the individual vertex's coordinate (vertex.co).
Here's a script that prints out the location of a rigid body simulated cube and the location of its first vertex (index=0) in each of the simulation's 40 frames:
import bpy
S = bpy.context.scene
cube = bpy.data.objects["Cube"]
mw = cube.matrix_world # World transformation matrix of Cube object
for i in range( 1, S.frame_end + 1 ):
S.frame_set( i )
vert0loc = ( mw * cube.data.vertices[0].co )[:] # Global location of vertex 0
cubeloc = mw.translation[:] # Global location of cube's pivot point
print( "Frame (%s) Vert %s Pivot %s" % (i, str(vert0loc), str(cubeloc) ) )
This is how the simulation looks like:

And this is how the output looks like:
Frame (1) Vert (-1.0, -1.0, 11.264372825622559) Pivot (0.0, 0.0, 12.264372825622559)
Frame (2) Vert (-1.0, -1.0, 11.256199836730957) Pivot (0.0, 0.0, 12.256199836730957)
Frame (3) Vert (-1.0, -1.0, 11.223535537719727) Pivot (0.0, 0.0, 12.223535537719727)
Frame (4) Vert (-1.0, -1.0, 11.188177108764648) Pivot (0.0, 0.0, 12.188177108764648)
Frame (5) Vert (-1.0, -1.0, 11.14197063446045) Pivot (0.0, 0.0, 12.14197063446045)
Frame (6) Vert (-1.0, -1.0, 11.052353858947754) Pivot (0.0, 0.0, 12.052353858947754)
Frame (7) Vert (-1.0, -1.0, 10.979090690612793) Pivot (0.0, 0.0, 11.979090690612793)
Frame (8) Vert (-1.0, -1.0, 10.848958015441895) Pivot (0.0, 0.0, 11.848958015441895)
Frame (9) Vert (-1.0, -1.0, 10.74873161315918) Pivot (0.0, 0.0, 11.74873161315918)
Frame (10) Vert (-1.0, -1.0, 10.578221321105957) Pivot (0.0, 0.0, 11.578221321105957)
Frame (11) Vert (-1.0, -1.0, 10.451122283935547) Pivot (0.0, 0.0, 11.451122283935547)
Frame (12) Vert (-1.0, -1.0, 10.240371704101562) Pivot (0.0, 0.0, 11.240371704101562)
Frame (13) Vert (-1.0, -1.0, 10.086490631103516) Pivot (0.0, 0.0, 11.086490631103516)
Frame (14) Vert (-1.0, -1.0, 9.835636138916016) Pivot (0.0, 0.0, 10.835636138916016)
Frame (15) Vert (-1.0, -1.0, 9.655065536499023) Pivot (0.0, 0.0, 10.655065536499023)
Frame (16) Vert (-1.0, -1.0, 9.364243507385254) Pivot (0.0, 0.0, 10.364243507385254)
Frame (17) Vert (-1.0, -1.0, 9.157073020935059) Pivot (0.0, 0.0, 10.157073020935059)
Frame (18) Vert (-1.0, -1.0, 8.82641887664795) Pivot (0.0, 0.0, 9.82641887664795)
Frame (19) Vert (-1.0, -1.0, 8.59273910522461) Pivot (0.0, 0.0, 9.59273910522461)
Frame (20) Vert (-1.0, -1.0, 8.222389221191406) Pivot (0.0, 0.0, 9.222389221191406)
Frame (21) Vert (-1.0, -1.0, 7.962289810180664) Pivot (0.0, 0.0, 8.962289810180664)
Frame (22) Vert (-1.0, -1.0, 7.5523786544799805) Pivot (0.0, 0.0, 8.55237865447998)
Frame (23) Vert (-1.0, -1.0, 7.2659502029418945) Pivot (0.0, 0.0, 8.265950202941895)
Frame (24) Vert (-1.0, -1.0, 6.816610813140869) Pivot (0.0, 0.0, 7.816610813140869)
Frame (25) Vert (-1.0, -1.0, 6.503941535949707) Pivot (0.0, 0.0, 7.503941535949707)
Frame (26) Vert (-1.0, -1.0, 6.015308856964111) Pivot (0.0, 0.0, 7.015308856964111)
Frame (27) Vert (-1.0, -1.0, 5.676488876342773) Pivot (0.0, 0.0, 6.676488876342773)
Frame (28) Vert (-1.0, -1.0, 5.148696422576904) Pivot (0.0, 0.0, 6.148696422576904)
Frame (29) Vert (-1.0, -1.0, 4.783813953399658) Pivot (0.0, 0.0, 5.783813953399658)
Frame (30) Vert (-1.0, -1.0, 4.216994285583496) Pivot (0.0, 0.0, 5.216994285583496)
Frame (31) Vert (-1.0, -1.0, 3.826138496398926) Pivot (0.0, 0.0, 4.826138496398926)
Frame (32) Vert (-1.0, -1.0, 3.2204246520996094) Pivot (0.0, 0.0, 4.220424652099609)
Frame (33) Vert (-1.0, -1.0, 2.803682804107666) Pivot (0.0, 0.0, 3.803682804107666)
Frame (34) Vert (-1.0, -1.0, 2.1592068672180176) Pivot (0.0, 0.0, 3.1592068672180176)
Frame (35) Vert (-1.0, -1.0, 1.716667652130127) Pivot (0.0, 0.0, 2.716667652130127)
Frame (36) Vert (-1.0, -1.0, 1.0335607528686523) Pivot (0.0, 0.0, 2.0335607528686523)
Frame (37) Vert (-1.0, -1.0, 0.5653115510940552) Pivot (0.0, 0.0, 1.5653115510940552)
Frame (38) Vert (-1.0, -1.0, -0.15629464387893677) Pivot (0.0, 0.0, 0.8437053561210632)
Frame (39) Vert (-1.0000004768371582, -1.0000001192092896, -0.08050315827131271) Pivot (-1.651987702189217e-07, -6.964894083694162e-08, 0.919496476650238)
Frame (40) Vert (-1.0000011920928955, -1.0000011920928955, 0.012565473094582558) Pivot (-4.125758152895287e-07, -5.586560973824817e-07, 1.0125640630722046)