# for http://blender.stackexchange.com/questions/35945/python-script-to-read-in-csv-file-containing-quaternion-coordinates-and-turn-it
import bpy
import csv
def mission1(obj, loc_csv, quat_csv):
"""
this version assumes that loc_csv and quat_csv have the same number of data lines.
:param obj: blender object that will be animated
:param loc_csv: CSV file containing location triples
:param quat_csv: CSV file containing quaternion rotation quartets
"""
with open(loc_csv, 'r') as loc_fh:
with open(quat_csv, 'r') as quat_fh:
r_loc = csv.reader(loc_fh, delimiter=',')
r_quat = csv.reader(quat_fh, delimiter=',')
i = 0
obj.rotation_mode = 'QUATERNION'
for row_loc in r_loc:
row_quat = r_quat.__next__()
#print([row_loc,row_quat])
row_loc = [ float(x.strip()) for x in row_loc[:3]]
row_quat = [ float(x.strip()) for x in row_quat[:4]]
#print([row_loc,row_quat])
fr = 10+2*i
obj.location = row_loc
obj.rotation_quaternion = row_quat
obj.keyframe_insert(data_path="location", frame=fr)
obj.keyframe_insert(data_path="rotation_quaternion", frame=fr)
i = i+1
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")
|
Blender python API quick-start
Syntax highlighting by Pygments.