0
$\begingroup$

I am trying to make an animation creation script using cvs.

I referenced several csv scripts here, but I couldn't understand it at all because I not long ago learned Python.

How can I input the csv data of the attached photo to a specific object by creating a keyframe for each frame?

screenshot of spreadsheet with data

Here is a csv file with similar data

0,0,0,0,0,0,0
1,0,-.02,7,0,0,0
2,0,-.05,14,0,0,0
3,0,-.09,21,0,0,0
4,0,-.14,29,0,0,0
5,0,-.20,36,0,0,0
6,0,-.29,44,0,-.04,0
7,0,-.39,52,0,-.11,0
8,0,-.49,59,0,-.17,0
$\endgroup$
2

1 Answer 1

3
$\begingroup$

I'm assuming that you want to apply the animation to the current object. I'm also assuming that the csv file does not have the column headings but only the data. Finally, I'm assuming that the position and rotation are meant to be absolute values and not deltas.

That said, here's a script that does what you want:

import bpy
import csv

csvfilename = PATH_TO_YOUR_CSV_FILE

object = bpy.context.active_object
with open(csvfilename, newline='') as csvfile:
    csvreader = csv.reader(csvfile, delimiter=',')
    for row in csvreader:
        fn = int(row[0])
        loc = (float(row[1]), float(row[2]), float(row[3]))
        rot = (float(row[4]), float(row[5]), float(row[6]))
        print(fn, loc, rot)
        object.location = loc
        object.rotation_euler = rot
        object.keyframe_insert(data_path="location", frame=fn)
        object.keyframe_insert(data_path="rotation_euler", frame=fn)

This works by actually moving the object in world space and then adding keyframes for its location and rotation. There are several optimizations that could be applied to reduce the number of keyframes, and if you want deltas rather than absolute location and rotation you'll need to change the loc and rot creation; but it works as specified.

You can read about CSV handling in Python here and in more detail in PEP 305.

The blender manual entry for keyframe_insert might be helpful, although it's not terribly clear.

$\endgroup$

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