Save and load bone animation to any file!!!

General Moho topics.

Moderators: Víctor Paredes, Belgarath, slowtiger

User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

I've got it working now reading in action keys! Woohooo!

I am so close now. I don't want to jump the gun like I did in the past. I want to thoroughly test this as much as possible so there aren't any glaring bugs. My main concern is using the names of bones and actions in quotes. Lots of other things are quoted, styles, layers etc. I need to be absolutely certain that when reading the file format it isn't going to pick up the name of a style with the same name as a bone.

So far I've not had to change the original file format except for adding in interpolation on the end of each key line. I hope to just workaround that as a "version" filter so older saved animations will still load and then you can "fix" the interpolation and resave.

Or just open the originals and resave with key frame interpolation. I can't imagine how beneficial those saved animation files are without the key frame interpolation.

----------

In the middle of reorganizing my machines. I have a new PC, and I bought a new UPS so I am moving stuff around... put the Mac there? Or there? Where should the modem go? What about the external hard drive? Dang cord won't reach the UPS... etc etc.

If you ever buy a UPS READ THE FINE PRINT. It may state 8 outlets... but only 4 are battery back ups. The other 4 are just plain surge strip type plugs. I have two computers and two monitors... but I have an external hard drive which MUST be on the UPS... so one computer won't have a monitor if the power goes out. I will hook that one up to USB for auto/save/shutdown since I won't be able to use it if the power is out (I have a lot of problems with power outages during minor storms around here)

The packaging is VERY misleading... big bold letters UPS WITH 8 PLUGS! Yeah right... forget the 4 surge plugs... I could just by a good power strip for that... useless.

-vern
chucky
Posts: 4650
Joined: Sun Jan 28, 2007 4:24 am

Post by chucky »

Those power outs are bad news, I had three here last night, at least they were short, but sometimes you have to have a finger constantly hovering over "save". :?
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

That's why I got the new UPS. Before when the weather looked bad I shut everything down. Of course when I'm not home and the weather gets bad... it crashes. Now at least I have some leeway time (30-60 minutes).

Dang thing only has one USB port for auto shut down. Very annoying. they don't say that on the box. I may put VNC on the mac and then I can shut down remotely when I'm not home and the weather gets bad.

-vern
User avatar
DK
Posts: 2896
Joined: Mon Aug 09, 2004 6:06 am
Location: Australia

Post by DK »

The main reason I use Laptops nowdays. Power outs don't worry me at all ;)

D.K
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

<heavy sigh>

Lost my mind there for a moment. Don't know how I missed this one. I can't just easily save the action data because there is no direct script access to action files. I can read the file format but to write that data I have two choices:

Parse ALL the info from the file format (icky yuck blech!)

Activate the actions and read the bone data (less icky)

I will probably go with choice 2. I'm so annoyed with myself for thinking this would be easy. I was sitting there in front of my computer all ready to change the code to write the bone channel information for the actions... and I'm scratching my head wondering why there seems to be something missing from the equation. Some small thing I couldn't quite put my finger on... like... uh... no way to do it.

See I have the interpolation info reading the file format for the actions. That WAS easy because it is exactly the same as for "regular" key frames on the main time line. But you can't read position, rotation etc from an action unless it is ACTIVATED.

So, I will activate each action at the END of the script (after writing the main time line keys) and write all the action data separately to the end of the data file. I can't activate the action during the other writing because I would be in the wrong time line. <sigh>. This will actually make it easier for versioning. I can just skip that last bit completely for older versions. This will also make it easier to add in a check box to import animation data DIRECTLY to a NEW action. I can put in a text box to name the action and the imported animation will go in the action.

It's still doable. I just thought I was closer to being finished. ;)

-vern
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Update!

Post by heyvern »

My brain is... spinning.

I had to back up and start over a bit. Trying to parse the file format is VERY HARD. Just to give you an example of how this got out of hand...

You read one line of the file. That reads to the end of the line. You can also read just the next number excluding spaces. However if you read the next number of a line AND THEN READ THE LINE... it doesn't read the next line it only reads to the END of the line you read the number from.

For example:

Code: Select all

This is a line of info
Here is another line of info
0 1 2 3 4 5 6 this line has numbers but also has text
You read the lines using:

Code: Select all

Myfilevariable:read()
So if you use that code above you get the whole first line.
If you use this again you get the next whole line.

Now imagine I want the numbers from the third line.

Code: Select all

Myfilevariable:read("*n")
That would read the first number from line 3 above which is 0.
Doing this again and again for each number gives me each number.

Here is where I got lost completely. You would THINK that using:

Code: Select all

Myfilevariable:read()
Would read in the NEXT WHOLE LINE. WRONG! It reads the REST of the line you read the numbers from. According to the definition of the :read() function it SHOULD read the next WHOLE line but it doesn't. It reads from the last number you read in using :read("*n") to the end of that line.

So, all my code was so messed up... I got completely lost. I had to create a simplified script and carefully track exactly which line is being read and print it out to verify it. After doing that and finally understanding how to progress through the file format I am at a point now where I can move forward to actually extracting all of the action key frames. PHEW! What a freaking frustrating NIGHTMARE. I finally understand it though.

This took me ages to figure out. Basically in some places I have to put in "extra" line reads, but in others I don't. then I have to make sure I figure out in loops where to read in an extra line. Sometimes there is an extra line that has to be skipped... sometimes not... so I have to account for that... <sigh>. It's like doing a Sudoku puzzle wearing gloves and blindfolded.

;)

-vern
User avatar
synthsin75
Posts: 10276
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

I may be mistaken, but I believe you could use the function:

Code: Select all

function file:seek(whence, offset)
The arguments are:
"set": base is position 0 (beginning of the file);
"cur": base is current position;
"end": base is end of file;

If I read this right, you can set the 'whence' to 'cur' to get the current position in the file, and then adjust your offset number to 'reset' the current position.

I'm not sure if that helps, but it may be a shortcut for what you're trying to do.

Perhaps once your 'number' read returns false (or something), you could use this to backup the current position the same number as the numbers read in and then skip that whole line from it's start.

But I don't really know. :wink:
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

Great idea!

I've got it working but this will save performance issues. I can back up certain positions that I go back to.

I finally got it working. Another bug I ran into was that the key frames for the IKParentTarget and IKGlobalAngle don't have interpolation values that can be set using the script interface. I guess they pick up interpolation from the bone lock interpolation. It was causing errors I couldn't track down till I figured it out.

-vern
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

What a pain in the arse! ;)

First off, thanks for the tip on using file:seek() Synth. Worked great. I use it store the previous position when I have to back track.

The other problem is how the file format is written when using actions. I was wrong, the actions are not written to every bone's channel unless there is a key for that action. This means that even though... technically... there appears to be a "key" on frame zero for ALL channels in an action there really isn't.

This is making it tricky to store and right the channel keys for an action. I have to be able to skip over the channels and interpolations for action keys if they don't exist.

I wasted hours of effort trying to determine how the file format was being written and where the action data is stored before finding this out. It was causing major errors that I had trouble tracking down.

It wasn't until I created a series of actions leaving each channel (translation, angle etc) empty before I saw that the actions weren't being written in the keys. It appeared at first as if keys were always written even if the action only had one key on frame zero. This does happen but only if ONE of the actions has a key on a frame OTHER than zero.

For instance with 5 actions, if one of those actions has a key on frame 12 for the angle, ALL of the channels have that action referenced in the bone keys. If NONE OF THE ACTIONS have any keys for angle than the action is not written to any channels. This is probably a rare situation but it popped up in my tests and caused no end of confusion for me.

-vern
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

IT WORKS! It works it works it works. Chew my dirty socks and call me Swanson it WORKS!!!

I successfully saved and loaded ALL key frames ALL actions from a bone layer. It is not ready for prime time yet. I want to do some MAJOR testing and implement some of the other features discussed BUT IT WORKS IT WORKS IT WORKS!

Another bonus, by mistake I found out how to import "regular" non action bone animation directly to an action. that is what I want to add. the ability to just create the name of an action when importing so the animation goes straight to an action without any extra effort.

I have learned SO MUCH about reading the AS file format and how it is structured. Ultimately... at some point down the road I could see this working directly on an AS file with out the need to save the data first. Just import the file and read the data directly. Choose the bone layer you want to import. It's all the same info anyway. It's just reading the file which is hard and I've got a handle on it now.

That's... later... much later... down the road as I said. ;)

I'm so happy I could cry.

-vern
User avatar
synthsin75
Posts: 10276
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

Ptooey! Alright Swanson, great news! Glad I could provide what little helpful ideas I did. Can't wait to see the finished script. But I'm just as interested in what insight you can provide on the file format. I'm sure the work you've done will make any future work with the format much less guess work.

There seems to be no end to what can be done with AS scripting. Now if Mike can ever see his way to openning it up some more, but I guess he'd soon have to just go open source if he openned up too much.

:wink:
User avatar
rylleman
Posts: 750
Joined: Tue Feb 15, 2005 5:22 pm
Location: sweden
Contact:

Post by rylleman »

heyvern wrote:IT WORKS! It works it works it works. Chew my dirty socks and call me Swanson it WORKS!!!
Fantastic!
I think this script will save tons of work in the long run.
Thank you in advance, hope to see the released script soon!
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

synthsin75 wrote:But I'm just as interested in what insight you can provide on the file format.
I've learned a lot. I think with some effort I can parse the whole file. The trick is getting rid of unneccessary elements, simplifying the file so the content can be read more easily. I've only scratched the surface by extracting the interpolation. This could easily lead to extracting data not in the script interface. Unfortunately making changes to an open file in this way doesn't update in AS. I suppose you could just close and reopen it... but... that's kind of... spooky and weird. It could potentially lead to corrupt files that won't open.

The potential is worth looking into though.

-vern
User avatar
DK
Posts: 2896
Joined: Mon Aug 09, 2004 6:06 am
Location: Australia

Post by DK »

GO VERN!!!!

D.K
Post Reply