How to import FBX Animation Sequence from code

I found the function UEditorEngine::ImportFbxAnimation

but I don’t know how to use this function.

I’d like to create Animation Sequence without Import Option Dialog.

Here is my code:

TArray<FAssetData> SK_Skeleton;
AssetRegistry.GetAssetsByPackageName(FName("/Game/SK_Skeleton"), SK_Skeleton);
USkeleton* Skeleton = Cast<USkeleton>(SK_Skeleton[0].GetAsset());
FName AssetName = TEXT("Anim");
FString PackagePath = TEXT("/Game/Anim");
UPackage* Package = CreatePackage(NULL, *PackagePath);
FString FileName = TEXT("F:/SK_Anim.fbx");
UFbxAnimSequenceImportData* Data = NewObject<UFbxAnimSequenceImportData>();
UAnimSequence* NewAnim = UEditorEngine::ImportFbxAnimation(Skeleton, Package,  Data, *Filename, *AssetName.ToString(), true);

What about the ImportData?

The imported asset is not correct.

127021-notvalid.jpg

(Import button)

127022-valid.jpg

please help, thank you.

I think it needs to mark package dirty after your code.

// Update notice
NewAnim->MarkPackageDirty();
ULevel::LevelDirtiedEvent.Broadcast();
NewAnim->PostEditChange();

And then you had better create UnFbx::FBXImportOptions object and initialize options before calling UEditorEngine::ImportFbxAnimation.

Please refer to UFbxFactory::FactoryCreateBinary in FbxFactory.cpp.

thank you very much.

I added the above code after my code and create FBXImportOptions.

(refered to ApplyImportUIToImportOptions in FbxMainImport.cpp)

I got the correct info.

always thankful for the help.

Hi tsqp

Code snippet in your question is not quite clear that how you import FBX animation from disk in c++? I couldn’t find a way to do that. I too don’t know how to use UEditorEngine::ImportFbxAnimation(…) function. Can you please help me with that?

hi Vinayaka_P

OK,
You should create UnFbx::FBXImportOptions object and initialize before calling UEditorEngine::ImportFbxAnimation(…) function.

I attatched sample code.
Please check it out.
I wish for your help. :slight_smile:

link text

Hi,

Thanks for detailed code.

May i know how you managed to get UnFbx::FFbxImporter working?

I tried,

  1. including FFbxImporter.h file → got unresolved external symbol error as in this post
  2. modified project.build.cs file to include FBX → didn’t help.

From this post i came to know that i should modify engine code. Should I?

Engine version i’m using is 4.19

Here’s another post i looked into

hi Vinayaka_P

Try including FbxImporter.h.

not FFbxImporter.h.
and then

UAnimSequence* NewAnim = FTosFBX::ImportAnimationSequence <= function in attached file
SkeletalMeshActor->GetSkeletalMeshComponent()->SetAnimationMode(EAnimationMode::AnimationSingleNode);
SkeletalMeshActor->GetSkeletalMeshComponent()->AnimationData.AnimToPlay = NewAnim;
SkeletalMeshActor->GetSkeletalMeshComponent()->SetAnimation(NewAnim);

Please do your best.

hi,tsqp
I also have some questions about how to create skeleton mesh runtime from fbx file.
I hope your help.thanks

hi, longshensanjie

try this code

UFbxFactory* factory = NewObject<UFbxFactory>(UFbxFactory::StaticClass(), FName("Factory"), RF_NoFlags);
UPackage* package = CreatePackage(NULL, TEXT("/Game"));
FString filename = "fbx file path";
bool canceled = false;
factory->ImportObject(factory->ResolveSupportedClass(), package, FName("SK_Mesh"), RF_Public | RF_Standalone, filename, nullptr, canceled);

do not forget including “Factories/FbxFactory.h”

and you can access fbx options like this

factory->ImportUI->SkeletalMeshImportData->b…

or

factory->ImportUI->StaticMeshImportData->b…