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:


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


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)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.