3
$\begingroup$

I have a csv file and an object, which i would like to animate using the data from the file. The file consists of x- y- and z data and time data.

how can i with python code access the objects location and set it to the given coordinates at the given time.

Do i need to iterate throuh all the frames? can it even be done with a csv file?

$\endgroup$

2 Answers 2

2
$\begingroup$

Here is another example to take frames per second into account. For every sample in your file you will only insert one keyframe (assuming you want to interpolate linear between them).

import bpy

... # read x,y,z and t from a file, assuming seconds for t

# and do something like this in a loop for all keys:
f = bpy.data.scenes["Scene"].render.fps * t + 1 # stub
obj = bpy.data.objects["Cube"] # stub
obj.location = [x,y,z]
obj.keyframe_insert(data_path="location", frame=f)

# make interpolation between keyframes linear
for fc in obj.animation_data.action.fcurves: # stub
    for kp in fc.keyframe_points:
        kp.handle_left_type = 'VECTOR'
        kp.handle_right_type = 'VECTOR'
    fc.update()

If you need a higher resolution you could increase the Frame Rate in render settings or come up with a more sophisticated mapping of frames/seconds.

Edit: Assuming you have a file like this:

...
3.0,1.0,3.0,2.666666
1.0,4.0,1.0,5.335345
...

You could use pythons csv module to parse it in Blender like this:

import bpy
import csv

csvf = open('/path/to/values.csv')
rd = csv.reader(csvf, delimiter=',')
for r in rd:
    l = [float(x) for x in r]
    xyz = l[:3] # location
    t = l[3] # time
$\endgroup$
2
$\begingroup$

I am not aware of a direct csv importer. But of course you could write a python script for your special case. If your csv contains just object locations on a timeline, then it would be rather easy:

then you would iterate over the timeline something like this:

import bpy
context = bpy.context
obj     = context.object
scene   = context.scene

start   = scene.frame_start
end     = scene.frame_end

for frame in range(start, end+1):

    # Set object location to whatever you get from the cvs
    # here i just move the object along the x axis:
    obj.location.x=frame
    obj.keyframe_insert("location",frame=frame)

This is not a very efficient way to do an animation. The above example would be done much more efficient by just adding 2 keyframes (one at the start and one at the end) and let blender calculate the movement in between.

Also you might want to create the animation curves (fcurves) directly instead of keyframing every timeframe. But it really depends on what your csv data contains.

However, maybe the above script can help to start your journey :)

$\endgroup$
2
  • 2
    $\begingroup$ I highly discourage the use of bpy.ops.anim.keyframe_insert_menu(), which is inefficient and not supposed to be used like this (operators are user actions). See blender.stackexchange.com/questions/7358/… $\endgroup$
    – CodeManX
    Sep 22, 2014 at 13:20
  • $\begingroup$ Thanks! I changed the example script above according to your recommendation. $\endgroup$
    – Gaia Clary
    Sep 22, 2014 at 13:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .