# Exporting the animation of an object to a text file with custom code

I've seen a lot of discussion on how to animate an object from a text file, and I'm attempting to do the opposite of that.

To clarify, I want a program that goes frame by frame and writes the location of an object to the file, line by line. (line one has the X,Y,Z of the object on frame 1, line two has the X,Y,Z of the object on frame 2, etc...)

The problem that I am having is that when I run the code it writes down the location of the object 5 times, all from the same frame. (It writes down a bunch of [5.0 0.0 0.0]'s). So I believe that this is a problem with the line that changes the frame not being updated correctly when I run the code.

I should state that I'm coming from basic java knowledge and have almost no idea how python works. I'm using a for loop and I want the frame to be bumped up one every time it goes through a cycle. I know how to do this in java, but not in python.

Hope I explained this correctly, thanks for the help!

My code:

obj = "Cube"
filename = "file"
filepath = 'FILEPATH'
combinedpath = os.path.join(filepath + filename + ".txt")
SAVEFILE = open(combinedpath,'a')

for i in range (0, 5):

#frame set line (is this the problem?)
bpy.data.scenes["Scene"].frame_current = i

#location grabbers
locnab0 = bpy.data.objects[obj].location[0]
locnab1 = bpy.data.objects[obj].location[1]
locnab2 = bpy.data.objects[obj].location[2]

#location converters
ink0 = str(locnab0)
ink1 = str(locnab1)
ink2 = str(locnab2)

#write to file
SAVEFILE.write("\n" + ink0 + " " + ink1 + " " + ink2)

SAVEFILE.close()


bpy.context.scene.frame_set(i)

bpy.data.scenes["Scene"].frame_current = i