2

how can I export objects position information from blender to a text file for every frame of an animation?

Alice
  • 21
  • 1
  • 2

2 Answers2

1

This will write the location x,y and z values of the active object(bpy.context.object) at every frame starting from the start frame and finishing with the end frame set in the render settings under Dimensions section, Frame Range. You can change that by changing the for loop's condition. You can format the output as you need as well, just keep in mind you need to convert values you wish to output to strings.

import bpy

file = open("C:\\SomeDirectoryBlenderHasPermissionToAccess\\SomeFile.txt", 'w')
scene = bpy.context.scene
loc = bpy.context.object.location
for frames in range(scene.frame_start, scene.frame_end+1):
    scene.frame_set(frames)
    file.write('Loacton at frame '+str(frames)+':('
    +str(loc.x)+', '+str(loc.y)+', '+str(loc.z)+')\n')
file.close()

If you needed to run it for all objects in the scene you would need a loop within a loop:

...

for every_object in bpy.context.scene.objects:
    for every_frame in range(scene.frame_start, scene.frame_end+1):
        scene.frame_set(every_frame)
        loc = every_object.location
        file.write('Loacton at frame '+str(every_frame)+':('
        +str(loc.x)+', '+str(loc.y)+', '+str(loc.z)+')\n')

...

You can find more info on working with strings here. www.w3schools.com is a wonderful resource in general when learning Python.

Blender Python documentation is usefull as well especially the search function there.

One more extremely useful place to learn is Blender itself. There are some very useful examples that come with Blender here: enter image description here

Also auto complete function(ctrl+space) is extremely useful in the Python Console: enter image description here

Martynas Žiemys
  • 24,274
  • 2
  • 34
  • 77
  • Martin Z how would I configure that to output more info for each object in a scene? can the order of the output be controlled, – Alice Sep 10 '18 at 13:16
  • The general idea would be only a few steps: 1. Learn Python. 2. Learn bpy API. 3. Add the functionality needed using your newly obtained skills. :D I think there are many ways to sort the output. You would need to be more specific. See the links I added to the answer. – Martynas Žiemys Sep 10 '18 at 13:33
  • 2
    The drawback to using frame_set is that it updates the scene, that can get expensive for large scenes. – sambler Sep 11 '18 at 00:14
1

We can read blenders animation data, which allows us to then write that data to a file.

First step is to get the animation data that you want. There is an fcurve for every animated value, these can be found in obj.animation_data.action.fcurves. We can search the list of fcurves to get the curve/s we want. Each curve can also give us the value at a specific frame using fcurve.evaluate(frame). We then format that data as we write it to a file.

The index used with fcurve.find is based on 0=X, 1=Y, 2=Z.

import bpy

scn = bpy.context.scene
obj = bpy.context.object

x_curve = obj.animation_data.action.fcurves.find('location', index=0)
y_curve = obj.animation_data.action.fcurves.find('location', index=1)
z_curve = obj.animation_data.action.fcurves.find('location', index=2)

with open('data_out.txt', 'w') as out_file:
    for f in range(scn.frame_start, scn.frame_end):
        x_pos = x_curve.evaluate(f)
        y_pos = y_curve.evaluate(f)
        z_pos = z_curve.evaluate(f)
        out_file.write('{},{:.3f},{:.3f},{:.3f}\n'.format(f,x_pos,y_pos,z_pos))
sambler
  • 55,387
  • 3
  • 59
  • 192