1
$\begingroup$

I would like to animate a body using data I have collected in an external text file. How would I go about importing quaternion data and animating an object from it using a python script.

Here is a sample of the data:

0.90,0.13,-0.09,-0.41
0.90,0.11,-0.08,-0.40
0.91,0.10,-0.08,-0.40
0.91,0.09,-0.07,-0.40
0.91,0.08,-0.07,-0.40

It is in the format WXYZ where each line in the file would be a frame.

$\endgroup$
1

1 Answer 1

2
$\begingroup$

I managed to answer my own question but will post answer to help future people who have a similar question.

I used the answer to this question and this python script to animate the object:

import bpy
from mathutils import Quaternion

filename = 'input.txt'

file = open(filename, 'r')
lines = file.readlines()

frame_number = 0

ob = bpy.context.object 
ob.rotation_mode = 'QUATERNION'

for i in lines:
    frame_number += 1
    q0, q1, q2, q3 = i.split(',')
    quat = float(q0), float(q1), float(q2), float(q3)

    ob.rotation_quaternion = quat
    ob.keyframe_insert('rotation_quaternion', frame=frame_number)

file.close()
$\endgroup$

You must log in to answer this question.

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