LazyDuchess / OpenTS2

Open source re-implementation of The Sims 2 in Unity
Mozilla Public License 2.0
221 stars 17 forks source link

Export FBX animation #59

Open alexcroft93 opened 2 months ago

alexcroft93 commented 2 months ago

Hello, your re-implementation of the sims 2 in unity is really impressive. I'm currently looking for a way to transfer animations from The Sims 2 to Blender. I've downloaded the OpenTS2 Unity project and loaded everything correctly with @ammaraskar unity simanimationtestscript. I'm able to play the animations and view their keyframes. Now, I tried exporting the mesh, rig, and animation to Blender using a Unity FBX export, but it seems not to work correctly. Maybe for the IK CHAIN. Is there a way to bake the ik chain directly in unity of the simanimationtestscript to the rig bones, so when i import the fbx in blender it reads only the bones keyframes and not also the ikchain (ik bones).

thanks

ammaraskar commented 2 months ago

Aah it's unfortunate that Unity's FBX export doesn't bake in the inverse kinematics. If there isn't a way to do that with the built-in exporter then you may have to write your own exporter that goes frame-by-frame and dumps the IK-updated position of bones.

@LazyDuchess has an example of an .obj exporter from a past version of OpenTS2 here https://github.com/LazyDuchess/SimUnity2/blob/73a0fd8d08d21cc79397253ab0909f31ac972a07/Assets/Editor/ObjExporter.cs You may have to write something similar.

alexcroft93 commented 2 months ago

Unfortunately, I don’t know the programming language in C#. Could you help me write the script?

ammaraskar commented 1 month ago

Hey @alexcroft93, sorry for the late response but I pushed some code to the branch bake_ik_animations https://github.com/LazyDuchess/OpenTS2/compare/bake_ik_animations

This adds a "baked" version of each animation as it plays which should hopefully survive the export process. Could you give this a try and see if it works?

image

alexcroft93 commented 1 month ago

It works perfectly. Thanks!!! Now the only problem is that Blender doesn't correctly import the rig structure. Root_trans and root_rot are interpreted as empties and not as parented bones. The armature should be 'auskel' and not 'root_rot'. But I believe this is an issue with importing FBX into Blender. I'll understand how to solve it, maybe by searching for information on Blender forums. As for the simanimationtestscript, I noticed that it only reads animations from the base game. In fact, to try to read animations from funkybusiness, I had to copy the object.package from the path 'The Sims 2 Ultimate Collection\Best of Business\EP3\TSData\Res\3D' containing the animations into the base game folder. I would like to find a way to make the script also read the paths of the various expansions. Screenshot 2

ammaraskar commented 1 month ago

That's awesome! Would you mind saving one of those animations out as a .blend file for me? I'm having some trouble getting it to import properly in blender myself. I'm not super experienced with blender but I'll see if I can try to figure out why the armature shows up on the wrong object.

As for loading animations from the DLCs, after this line:

https://github.com/LazyDuchess/OpenTS2/blob/be73b021b2eb9cb82dd5b00850dfcc1d06d71baf/Assets/Scripts/OpenTS2/Engine/Tests/SimAnimationTest.cs#L31-L34

go ahead and add the line

            contentProvider.AddPackages(
                Filesystem.GetPackagesInDirectory(Filesystem.GetDataPathForProduct(ProductFlags.Business) +
                                                  "/Res/Sims3D"));

You can find the other ProductFlags. identifiers here: https://github.com/LazyDuchess/OpenTS2/blob/bake_ik_animations/Assets/Scripts/OpenTS2/Content/ProductFlags.cs

alexcroft93 commented 1 month ago

Sure. To export the animation I select only the one baked animation deleting all others animation array elements in the animation section of the inspector under the amFace_cres object. Then i select all keyframes of the amFace_cres object and export the fbx using the unity fbx exporter (export format binary) To import the fbx in blender i use the Better FBX importer (https://blendermarket.com/products/better-fbx-importer--exporter) which is not free unfortunately. This is a blend file of the exercisemachine animation. Thanks! exercisemachineanim.zip

ammaraskar commented 1 month ago

I pushed some code that optimizes the keyframes used in the animations to make it take up less space. I also made a separate baked object to just contain the animations at the auskel level without all the extra stuff we use to make the IK work.

Using the https://github.com/KhronosGroup/UnityGLTF extension, I was able to manage to export the sim and animations into a .glb file. This file imports pretty cleanly into blender with the animations separated. As far as the armature not being rooted at auskel, this is how the skeleton in the sims2 geometry is set up, the root bone is actually root_rot.

You can switch between the animations by filtering to root_rot in the blender Action Editor window:

image

alexcroft93 commented 1 month ago

It works!! Thanks to your work, there is finally a way to export animations from The Sims 2 (using unity).