python script to read in .csv file containing quaternion coordinates and turn it into a path/curve for an object to follow I am a highschool student working at a college's engineering department for the summer. I just began working in blender and have intermediate knowledge of python. Could anyone share a script that:


Read a .csv file of xyz coordinates and a .csv file of quaternion coordinates into python.
Turn these points into some type of curve or path that an object can then follow to model the path the coordinates map out




My .csv files (around 250 lines) have three and four points on each line. All the coordinates would need to be floats on account that they are decimals. 

Here you can find both example .csv files: https://gist.github.com/anonymous/a1a4cbe6e959ac14e5af to play with. My code looks like:

import os
import bpy
import csv
from mathutils import Vector

def set_keyframes(obj, loc_txt, quat_txt):
    obj.rotation_mode = 'QUATERNION'

    with open(loc_txt, 'r') as loc_fh:
        with open(quat_txt, 'r') as quat_fh:
            r_loc = csv.reader(loc_fh, delimiter=',')
            r_quat = csv.reader(quat_fh, delimiter=',')

            i = 0

            for row_loc in r_loc:
                row_quat = r_quat.__next__()
                row_loc = [ float(x.strip()) for x in row_loc[:3]]
                row_quat = [ float(x.strip()) for x in row_quat[:4]]

                fr = 10+2*i

                obj.location = Vector(row_loc) * 10
                obj.rotation_quaternion = row_quat
                obj.keyframe_insert(data_path="location", frame=fr)
                obj.keyframe_insert(data_path="rotation_quaternion", frame=fr)

                i += 1


base_folder =  '/Users/sharonshorak/Desktop/ARYA_PYTHON'

#  os.path.join  takes care of joining folder with filename.        
positions = os.path.join(base_folder, 'xyzposition.csv')
quaternions = os.path.join(base_folder, 'quaternions.csv')

set_keyframes(bpy.context.active_object, positions, quaternions)
Your data was rather ugly, but I was able to write code that copes with the extra commas and spaces:

From http://www.purplefrog.com/~thoth/blender/python-cookbook/csv-to-fcurve-loc-rot.html :

Here's an updated version that handles the situation where the location and quaternion .CSV files do not have the same number of lines.  This opens up more questions about why they differ.  Are they different sample rates or different time spans or both?

import bpy
import csv

def keyframe_location(obj, fname):
    with open(fname, 'r') as loc_fh:
        r_loc = csv.reader(loc_fh, delimiter=',')
        i = 0
        for row_loc in r_loc:
            row_loc = [ float(x.strip()) for x in row_loc[:3]]
            fr = 10+2*i
            obj.location = row_loc
            obj.keyframe_insert(data_path="location", frame=fr)

            i = i+1


def keyframe_rotation(obj, fname):
    with open(fname, 'r') as quat_fh:
        r_quat = csv.reader(quat_fh, delimiter=',')
        i = 0
        obj.rotation_mode = 'QUATERNION'
        for row_quat in r_quat:
            row_quat = [ float(x.strip()) for x in row_quat[:4]]
            fr = 10+2*i
            obj.rotation_quaternion = row_quat
            obj.keyframe_insert(data_path="rotation_quaternion", frame=fr)

            i = i+1


def mission2(obj, loc_csv, quat_csv):
    """
     Use this version if the loc and quat .CSV files do not have the same number of lines.
     This opens up the question: why do they have differing numbers of lines?
     Are they covering the same time span at different frame rates, or do they cover different time spans?
    """
    keyframe_location(obj, loc_csv)
    keyframe_rotation(obj, quat_csv)


#
#

mission2(bpy.context.active_object, '/var/tmp/loc.txt', "/var/tmp/quat.txt")