LazyDuchess / OpenTS2

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

Export FBX animation #59

Open alexcroft93 opened 7 months ago

alexcroft93 commented 7 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 7 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 7 months ago

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

ammaraskar commented 6 months 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 6 months 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 6 months 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 6 months 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 6 months 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 6 months ago

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

alexcroft93 commented 1 month ago

Hi @ammaraskar , I picked up the project again after quite some time. I was trying to load the "a2o-tent-getIn_anim" animation from bonvoyage in the simanimationtestscript. I used the version of OpenTS2 (https://github.com/LazyDuchess/OpenTS2/tree/d0ca1fe69f17387cc7900ab49fe5cda10b3ce816) and modified the file "OpenTS2/Assets/Scripts/OpenTS2/Engine/Tests/SimAnimationTest.cs" by adding this code:

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

However, the script is unable to correctly retrieve the animation, it simply doesn't find it. I can't figure out where I'm going wrong. Could you help me? Thanks

ammaraskar commented 1 month ago

Heyo, try doing:

contentProvider.AddPackages(Filesystem.GetPackagesInDirectory("C:/Path/To/Bon/Voyage/Res/Sims3D"));

I think the product flags have to be configured correctly in config.json which is why it might not be working.

alexcroft93 commented 1 month ago

Thanks it works now

alexcroft93 commented 1 month ago

Hi Anmar, can I ask you what is the process to load objects with animations instead of sims? thanks

ammaraskar commented 1 month ago

You can try using the ScenegraphGMDCTest scene for that.

You'll need to change this line: https://github.com/LazyDuchess/OpenTS2/blob/d0ca1fe69f17387cc7900ab49fe5cda10b3ce816/Assets/Scripts/OpenTS2/Engine/Tests/ScenegraphGMDCTest.cs#L20 to be the object you want to load

and then this line: https://github.com/LazyDuchess/OpenTS2/blob/d0ca1fe69f17387cc7900ab49fe5cda10b3ce816/Assets/Scripts/OpenTS2/Engine/Tests/ScenegraphGMDCTest.cs#L59-L60 to be the animation you want.

I think (most) objects don't use inverse kinematics, except things like the cow plant so that should work. I think I exported the recling chair asset to view on the web here a while ago.

alexcroft93 commented 1 month ago

For the o2a-vehicleOWN-superSportsCar-getIn_anim of the vehiclesupersportscarnew_cres it doesn't load the l_door_rot bone animations. Maybe something missing in the script?

EDIT: I also tried loading the "o-vehicleOWN-SuperSportsCar-driveway-DriveThruFromPark_anim" animation for the "vehiclesupersportscarnew_cres" object, but the rotation animation of the car doesn't load. I don’t understand in which part of the scripts the bone animations are being referenced.