Reading CSV file - Problem updating object location and keyframing

I need help. At time = 1 I’m reading in a .csv file, assigning the values to variables, and creating cubes at the designated x,y,z values. Then at time = 2 I select/make active the previously created cubes based on the naming convention and move it to the new x,y,z value on the list, insert keyframe, and deselect. Simple right? For some reason my code is not properly moving the designated cube and setting a keyframe at the new x,y,z value…it just sets a keyframe with the time = 1 values again.

What am I missing?

import bpy
import math

def newCube(diam):
     tempCube = bpy.ops.mesh.primtive_cube_add(location=(0,0,0), rotation=(0,0,0))
     return tempCube

myfile = open(r'F:\Homework\myAwesomeData.txt', "r")
for line in mydata:
', ' ')

     #This generates cubes in the their initial positions just fine at frame 1...

     if time==1:
          if section==1:
      = (str(time) + "." + str(section) + "." + str(part))

     #This just keyframes the cube at the initial position on frame 2... 

     if time==2:
          if section==1:
      =['1.1.' + str(part)]
     ['1.1. + str(part)].select = True
               obj.location = x,y,z

Simple examples without the for loop work fine. Help would be appreciated. Thanks.

I know nothing about python so forgive me if this makes no sense… just glancing thru your code I don’t see where (for frame 2) you reference the next snippet of your csv values? x, y, z never changes? Otherwise… you would be setting keyframes in frame 2 with the same values that were accessed from your csv file in the first frame. I could be completely clueless here but just trying to be helpful.


Thanks for the reply. In a goofy turn of events, after looking at the source data, I discovered that the portion of data I was using to test the code simply didn’t move (the X,Y,Z values for section 1 were the same at Time 1 and Time 2 and Time 24! When I tested it on a different section (section 2, 3) of the data, it works just as it should. Amazing.

I haven’t really tried the code, but I can see a couple of problems.

For one thing, I’d say you need to set the frame and THEN keyframe. Which is what is done for frame 1 but not frame 2. The reason is that changing the frame via the operator reloads the object’s position from the animation data. If there is no animation data, it doesn’t. I also don’t quite follow some of the differences between these two pieces of code.

Anyway, this is not how you should be animating objects in Python! It would be much better to directly change the fcurves’ keyframes. The advantage of that approach is that you can make sure the corresponding attributes don’t contain other animation data.

I think that is documented somewhere in the documentation, but on a cursory search I couldn’t find it. Anyway, you’ll need to look at least at AnimationData, Action and FCurve data types.

Maybe this will inspire you some more: