Parsing a keyframe from a string in Python

   2363   7   1
User Avatar
Member
517 posts
Joined: Dec. 2013
Offline
Hey does anyone know if there's an easy way to convert this string -
<hou.Keyframe t=0.4 expr='bezier()' lang=exprLanguage.Hscript v=0 s=0 auto_slopes=True in a=0.133333 out a=0.333333 use_accel_ratio=True>
back into a keyframe?

Thanks!
Pete
User Avatar
Member
201 posts
Joined: Jan. 2013
Offline
Here's an option.

import re

string = "hou.Keyframe t=0.4 expr='bezier()' lang=exprLanguage.Hscript v=0 s=0 auto_slopes=True in a=0.133333 out a=0.333333 use_accel_ratio=True"
print re.search('\d{1,2}[\.]\d{1,2}', string).group()
User Avatar
Member
517 posts
Joined: Dec. 2013
Offline
Hey thanks, I tried that but it only returns the ‘t’ value? I'm a little new with python, but I'm not sure that will work since I have to grab the odd True or false statement or string as well as values.
User Avatar
Member
201 posts
Joined: Jan. 2013
Offline
In what format should the result return? Write down how it should look after we've parsed the string.
User Avatar
Member
7762 posts
Joined: Sept. 2011
Online
Why do you need to parse this string in the first place? If you have this string, then at some point you would have had the python object that generated it. The python object makes interfacing each item directly possible. Are you trying to recover data that was somehow archived this way as a last resort?

In the future I recommend using the hscript opscript command. It is far more foolproof than the keyframe object methods, and you don't have to worry about whether you need to set a string keyframe or a standard keyframe. It has the other benefit that it can be executed directly instead of parsing.
User Avatar
Member
517 posts
Joined: Dec. 2013
Offline
Hey jsmack,
Yes I'm generating the string, I'm just messing around with saving off a bunch of animation to load back in at another time. So I'm writing data out to a txt file. I could seperate each element of the key manually I guess but since it wrote it out in that specific way I thought there might be a simple way to read that back in.
User Avatar
Staff
466 posts
Joined: July 2005
Offline
Parsing a string like this isn't as easy as you might think. Especially given that:

* It doesn't have a very regular structure (mostly attributes in the form x=y, but also free-floating keywords like “in” and “out” where it isn't clear how they should be interpreted).

* It has quoted strings that would need to be parsed specially if they allow quoting inside.

It's definitely not a quick answer… you'd need to write a parser or else learn how to use a Python parsing library.

IMHO the best bet would be for you to generate the string as JSON instead of as a custom one-off format. That would make writing the info out and reading it back in trivial.
Edited by mchaput - June 1, 2017 11:54:01
User Avatar
Member
517 posts
Joined: Dec. 2013
Offline
Thanks mchaput! Yes, the “in” was getting me I think exporting it better is the way to go, I just thought I'd check I wasn't missing something.
I really need to learn JSON formatting one of these days, it seems to be something I've avoided for long enough now
Thanks!
Pete
  • Quick Links