Need help with FBX Exporter

Hello,

I am trying to use unity’s fbx exporter to export fbx files from my runtime animation app.

The animation in the app is custom made(ie, it doesn’t use Unity’s AnimationClips or AnimationCurves). It just has transforms saved for each frame in a list. During playback, the transforms are applied on to the model at a set rate and it plays out like animations.

I am using Unity’s Fbx Exporter from the package manager. I am able to export the hierarchy and the meshes properly into the fbx file. The problem comes when I try to create the animation.

From the fbx sdk documentation, and unity’s fbxexporter script(com.unity.formats.fbx/com.unity.formats.fbx/Editor/FbxExporter.cs at master · Unity-Technologies/com.unity.formats.fbx · GitHub), I figured out I have to do this to create animation data in the fbx file

FbxAnimCurve rotationCurveX = thisNode.LclRotation.GetCurve(animLayer, "RotationX", Globals.FBXSDK_CURVENODE_COMPONENT_X, true);
rotationCurveX.KeyModifyBegin();

But this throws a null reference exception at KeyModifyBegin(), which means that the rotationCurveX is null.
According to the documentation, the true value in the GetCurve function is supposed to create the curve if it can’t find it.

It also says that the eAnimatable flag has to be true for it to work. So I added this before calling GetCurve.

thisNode.LclRotation.ModifyFlag(FbxPropertyFlags.EFlags.eAnimatable, true);

Even then, it still throws the null reference exception. I tried to put it inside a coroutine to try and delay the function, and even that didn’t help.

Any idea on what is happening and how I can fix it?

Edit: I have found it. When I was creating the AnimStack and AnimLayer, I was using the fbxManager, and not the fbxScene. Now the error is gone.

I still need the animations to appear in other software that imports fbx files.

Excellent investigation… and look at that, you solved the problem!

(I was going to say, see if there is a method you can call to ask “what are the names of the animation curves on here?” but you figured it out already!)

This happens every time, the moment I type out something, I get a different viewpoint and get a solution. Rubber Ducky Debugging FTW.

I have other things I need to fix on this, but that’s specific to my use case. So, thanks for letting me vent.

1 Like

I could be wrong, but I’m pretty positive that Unity’s FBX exporter is not able to run at runtime.

it’s more of a workflow solution between applications for creating assets.

but you could use the FBX SDK to get this working.

Unless you mean runtime as in when you hit the play button. That’s not runtime.

According to this,
https://docs.unity3d.com/Packages/com.unity.formats.fbx@2.0/manual/devguide.html

The fbx exporter scripts don’t work in runtime, but the fbx sdk bindings are available at runtime which is what i’m using to make my own exporter.
And I have confirmed that it does work at runtime by building it and exporting the file from the exe file.

1 Like

Okay, I have a new issue with the fbx exporting, this time with rotations(Quaternions woo).

Anyway, as I stated first, my frames are saved in a list of Transforms with local positions and local rotations saved, and I am applying the transforms from the list when I move to a new frame/saving it to the list when I edit the frame.

Some joints in the exported animation have the rotations wrong.
It looks like this in Unity
8427963--1115703--upload_2022-9-9_18-44-56.png

and the same frame looks like this in blender,
8427963--1115706--upload_2022-9-9_18-46-20.png.
The rotation seems to have flipped directions.

The fbx exporter seems to require Euler rotations when applying animation, so I used localRotation.eulerAngles to get the values. For most of the cases, this seems fine, but if the joints in my rig go beyond a certain rotation(like, take the knee joint again, if it goes to a full-squat-like position), the euler values seem to be wrong. If it doesn’t reach that, then it is fine

I printed the local rotation’s euler and the quaternion values, put the quaternion values into an online converter, and compared the euler value that it gave to the one that unity prints out. The value from unity and the value from the converter are different. I compared it against a few other frames and the frames that don’t have the joint flipped they seem to be fine(the euler value from the converter and unity are the same).

For example, in a flipped frame, the knee joint has a quaternion value of
(-0.8482306, 2.090353E-06, -0.001516377, 0.5296249) according to my print statement, and according to the unity inspector for that same frame(obtained from switching it to debug mode), it is (-0.8482306, 1.988373E-06, -0.001516377, 0.5296249), which is the same
BUT
the for the same frame, the printed euler value(obtained from localRotation.eulerAngles) is (296.0, 179.7, 180.2), and the values shown in the inspector are (-116.04, -0.3359985, 0.209915) which is equivalent to (243.96, 359.6640015, 0.209915). That’s a 50 degree change in the x axis, and close to 180 degrees in y and z.

Any clue what is happening here and how I can fix it?

Blender uses a right-hand coordinate system, Unity uses a left hand. That’s why Blender objects import with (-90,0,0) and axes swapped around as a way to half work around this.

You’re going the opposite way. I would assume the FBX export stuff would handle it, but perhaps there’s a bug or some special considerations that need to happen. I would contrive a super-simple example to test in all orientations before going too much further.

PS I love your little orange blocky dude… looks like he is doing aerobics in that pose! Always cool to get glimpses of other people’s work in progress!

No, the axis thing isn’t the issue here. I know that because I had troubles with that before. The fbx sdk has a function to convert the axis to other coordinate systems. If I skip that step, the left leg which I raised in unity would be the right leg in blender.

The issue that I said exists even before I convert it to fbx. The values that are returned by localRotation.eulerAngles don’t match the ones from Quaternions. This issue only happens if the bend is beyond a certain threshold. If it doesn’t reach that bend, then the angles are fine, and even the exported fbx in blender is fine.

PS. Thanks, you can check out the app here
https://store.steampowered.com/app/1696890/Anima_Toon_3D_Voxel_Character_Animation/

More details:

This says that even though the euler rotation shows a different value, they will still represent the same rotation.

So I guess they are the same value, just that something is happening in the exported fbx that reads that value wrong.

I’m trying to export a skinned character pose from Unity v2022.3.7f1 using the Export To FBX v4.2.1 tool. But when I click Export nothing happens. I tried it on non-skinned objects and it seems to work fine. Does anyone know what I might be doing wrong?