parse a text file to retrieve keyframes

http://forums.cgsociety.org/showthread.php?p=6473887#post6473887

I stumbled on this at CgTalk and though I would share it here, as it is something that we encounter now and then. Almost all programs as the option to output a text file with raw data of keyframes. But it can be tedious if we don’t know how to parse a text file and get it into maya or softimage.

I will only cover how to parse the file here as it’s mostly a python exercise.

The content of the text file looks something like this

13.232 12.134 11.134
14.234 67.423 22.634
13.232 10.001 7.346
13.232 12.134 4.126

So each column is X Y Z and each row is a frame. We now need to open this file and read in the text

raw_keyframes_file = open('text.txt')
text = raw_keyframes_file.read()
raw_keyframes_file.close()
print text

Once you have it as a list we can start to split the text so that we get each frame. Python sees a linebreak as \n, so we can use that to split the text into frames (since each line is a frame).

raw_keyframes_file = open('text.txt')
frames = raw_keyframes_file.read().split('\n')
raw_keyframes_file.close()
print frames

And we can now also split each frame in into channels. And we should remove empty rows as they might mess up the animation (if each frame was on every other row instead if each row).

raw_keyframes_file = open('text.txt')
frames = raw_keyframes_file.read().split('\n')
raw_keyframes_file.close()
keyframes = [] #empty to be filled with keyframes
for frame in frames:
    ''' removed empty rows '''
    if not frame == '':
        keyframes.append(frame)
x = []
y = []
z = []
for channel in keyframes:
    ''' use the space to split the keyfame into channels '''
    chan = channel.split(' ')
    x.append(chan[0])
    y.append(chan[2])
    z.append(chan[4])
print x, y, x

We have now successfully gathered all information that we need. But the code isn’t very pretty, and we would be better off if we could reuse this. So……… **loud music*** let’s make it into a class.

class MyTools(object):
    def parse_keys(self, textfile):
        raw_keyframes_file = open(textfile)
        frames = raw_keyframes_file.read().split('\n')
        raw_keyframes_file.close()
        keys = MyTools()
        keys.x = []
        keys.y = []
        keys.z = []
        keyframe = []
        for frame in frames:
            if not frame == '':
                keyframe.append(frame)
        for channel in keyframe:
            chan = channel.split(' ')
            keys.x.append(chan[0])
            keys.y.append(chan[2])
            keys.z.append(chan[4])
        return keys

It’s almost the same code, with the difference that we now have it wrapped up in a class. And we now have a attribute called ‘keys’ which also has x, y, z in it. So if you save out the file in a folder called ‘tools’ and call it ‘__init__.py’. If you read the previous ‘lesson’ we can now import this into other scripts.

import sys
sys.path.append('/server/tools/python')
from sanders3d import tools

tools = tools.MyTools() #first we need to instance the class
keys = tools.parse_keys('raw_keyframe.txt') #now point the text file into it

#print out the channels
print keys.x
print keys.y
print keys.z

Did that work? I hope so :)

About sanders3d

Digital Animation Monkey
This entry was posted in python. Bookmark the permalink.

2 Responses to parse a text file to retrieve keyframes

  1. Kym Watts says:

    Hey ,

    A simpler way to do the below code:
    #old code
    raw_keyframes_file = open(‘text.txt’)
    frames = raw_keyframes_file.read().split(‘\n’)
    raw_keyframes_file.close()
    for line in frames:
    print line
    #
    #simpler
    frames = open(‘text.txt’).readlines()
    for line in frames:
    print line

    readlines does 2 things, it closes the file once it gets to the eof.
    Secondly it splits on both ‘\n’ & ‘\r’, so you have unix based files covered too.

    Hope this helps.
    Cheers
    Kym

  2. Handy, there should be more blogs like this! Starting with python in SI(and scripting in general) is bit hard…
    I actually need to write F-Curve to a file, but I can`t get the values of the keys per frame out of the damn F-curve object!
    I must solve thissss….
    Thanks.. : )

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s