bool UEditorEngine::ReimportFbxAnimation( USkeleton* Skeleton, UAnimSequence* AnimSequence, UFbxAnimSequenceImportData* ImportData, const TCHAR* InFilename) { check(Skeleton); GWarn->BeginSlowTask( LOCTEXT("ImportingFbxAnimations", "Importing FBX animations"), true ); UnFbx::FFbxImporter* FbxImporter = UnFbx::FFbxImporter::GetInstance(); // logger for all error/warnings // this one prints all messages that are stored in FFbxImporter UnFbx::FFbxLoggerSetter Logger(FbxImporter); const bool bPrevImportMorph = (AnimSequence->RawCurveData.FloatCurves.Num() > 0) ; if ( ImportData ) { // Prepare the import options UFbxImportUI* ReimportUI = NewObject<UFbxImportUI>(); ReimportUI->MeshTypeToImport = FBXIT_Animation; ReimportUI->bOverrideFullName = false; ReimportUI->AnimSequenceImportData = ImportData; ApplyImportUIToImportOptions(ReimportUI, *FbxImporter->ImportOptions); } else { FbxImporter->ImportOptions->ResetForReimportAnimation(); } if ( !FbxImporter->ImportFromFile( InFilename, FPaths::GetExtension( InFilename ) ) ) { // Log the error message and fail the import. FbxImporter->FlushToTokenizedErrorMessage(EMessageSeverity::Error); } else { // Log the import message and import the mesh. FbxImporter->FlushToTokenizedErrorMessage(EMessageSeverity::Warning); const FString Filename( InFilename ); // Get Mesh nodes array that bind to the skeleton system, then morph animation is imported. TArray<FbxNode*> FBXMeshNodeArray; FbxNode* SkeletonRoot = FbxImporter->FindFBXMeshesByBone(Skeleton->GetReferenceSkeleton().GetBoneName(0), true, FBXMeshNodeArray); if (!SkeletonRoot) { FbxImporter->AddTokenizedErrorMessage(FTokenizedMessage::Create(EMessageSeverity::Error, FText::Format(LOCTEXT("Error_CouldNotFindFbxTrack", "Mesh contains {0} bone as root but animation doesn't contain the root track.\nImport failed."), FText::FromName(Skeleton->GetReferenceSkeleton().GetBoneName(0)))), FFbxErrors::Animation_CouldNotFindTrack); FbxImporter->ReleaseScene(); GWarn->EndSlowTask(); return false; } // for now import all the time? bool bImportMorphTracks = true; // Check for blend shape curves that are not skinned. Unskinned geometry can still contain morph curves if( bImportMorphTracks ) { TArray<FbxNode*> MeshNodes; FbxImporter->FillFbxMeshArray( FbxImporter->Scene->GetRootNode(), MeshNodes, FbxImporter ); for( int32 NodeIndex = 0; NodeIndex < MeshNodes.Num(); ++NodeIndex ) { // Its possible the nodes already exist so make sure they are only added once FBXMeshNodeArray.AddUnique( MeshNodes[NodeIndex] ); } } TArray<FbxNode*> SortedLinks; FbxImporter->RecursiveBuildSkeleton(SkeletonRoot, SortedLinks); if(SortedLinks.Num() == 0) { FbxImporter->AddTokenizedErrorMessage(FTokenizedMessage::Create(EMessageSeverity::Warning, LOCTEXT("Error_CouldNotBuildValidSkeleton", "Could not create a valid skeleton from the import data that matches the given Skeletal Mesh. Check the bone names of both the Skeletal Mesh for this AnimSet and the animation data you are trying to import.")), FFbxErrors::Animation_CouldNotBuildSkeleton); } else { // find the correct animation based on import data FbxAnimStack* CurAnimStack = nullptr; //ignore the source animation name if there's only one animation in the file. //this is to make it easier for people who use content creation programs that only export one animation and/or ones that don't allow naming animations if (FbxImporter->Scene->GetSrcObjectCount(FbxCriteria::ObjectType(FbxAnimStack::ClassId)) > 1 && !ImportData->SourceAnimationName.IsEmpty()) { CurAnimStack = FbxCast<FbxAnimStack>(FbxImporter->Scene->FindSrcObject(FbxCriteria::ObjectType(FbxAnimStack::ClassId), TCHAR_TO_UTF8(*ImportData->SourceAnimationName), 0)); } else { CurAnimStack = FbxCast<FbxAnimStack>(FbxImporter->Scene->GetSrcObject(FbxCriteria::ObjectType(FbxAnimStack::ClassId), 0)); } if (CurAnimStack) { // set current anim stack int32 ResampleRate = DEFAULT_SAMPLERATE; if (FbxImporter->ImportOptions->bResample) { ResampleRate = FbxImporter->GetMaxSampleRate(SortedLinks, FBXMeshNodeArray); } FbxTimeSpan AnimTimeSpan = FbxImporter->GetAnimationTimeSpan(SortedLinks[0], CurAnimStack); // for now it's not importing morph - in the future, this should be optional or saved with asset if (FbxImporter->ValidateAnimStack(SortedLinks, FBXMeshNodeArray, CurAnimStack, ResampleRate, bImportMorphTracks, AnimTimeSpan)) { FbxImporter->ImportAnimation( Skeleton, AnimSequence, Filename, SortedLinks, FBXMeshNodeArray, CurAnimStack, ResampleRate, AnimTimeSpan); } } else { // no track is found FbxImporter->AddTokenizedErrorMessage(FTokenizedMessage::Create(EMessageSeverity::Error, LOCTEXT("Error_CouldNotFindTrack", "Could not find needed track.")), FFbxErrors::Animation_CouldNotFindTrack); FbxImporter->ReleaseScene(); GWarn->EndSlowTask(); return false; } } } FbxImporter->ImportOptions->bImportMorph = bPrevImportMorph; FbxImporter->ReleaseScene(); GWarn->EndSlowTask(); return true; }
void ImportStaticMeshLOD( UStaticMesh* BaseStaticMesh, const FString& Filename, int32 LODLevel ) { UE_LOG(LogExportMeshUtils, Log, TEXT("Fbx LOD loading")); // logger for all error/warnings // this one prints all messages that are stored in FFbxImporter // this function seems to get called outside of FBX factory UnFbx::FFbxImporter* FFbxImporter = UnFbx::FFbxImporter::GetInstance(); UnFbx::FFbxLoggerSetter Logger(FFbxImporter); // don't import materials UnFbx::FBXImportOptions* ImportOptions = FFbxImporter->GetImportOptions(); ImportOptions->bImportMaterials = false; ImportOptions->bImportTextures = false; if ( !FFbxImporter->ImportFromFile( *Filename, FPaths::GetExtension( Filename ) ) ) { // Log the error message and fail the import. // @todo verify if the message works FFbxImporter->FlushToTokenizedErrorMessage(EMessageSeverity::Error); } else { FFbxImporter->FlushToTokenizedErrorMessage(EMessageSeverity::Warning); bool bUseLODs = true; int32 MaxLODLevel = 0; TArray< TArray<FbxNode*>* > LODNodeList; TArray<FString> LODStrings; // Create a list of LOD nodes PopulateFBXStaticMeshLODList(FFbxImporter, FFbxImporter->Scene->GetRootNode(), LODNodeList, MaxLODLevel, bUseLODs); // No LODs, so just grab all of the meshes in the file if (MaxLODLevel == 0) { bUseLODs = false; MaxLODLevel = BaseStaticMesh->GetNumLODs(); // Create a list of meshes PopulateFBXStaticMeshLODList(FFbxImporter, FFbxImporter->Scene->GetRootNode(), LODNodeList, MaxLODLevel, bUseLODs); // Nothing found, error out if (LODNodeList.Num() == 0) { FFbxImporter->AddTokenizedErrorMessage(FTokenizedMessage::Create(EMessageSeverity::Error, FText(LOCTEXT("Prompt_NoMeshFound", "No meshes were found in file."))), FFbxErrors::Generic_Mesh_MeshNotFound); FFbxImporter->ReleaseScene(); return; } } // Display the LOD selection dialog if (LODLevel > BaseStaticMesh->GetNumLODs()) { // Make sure they don't manage to select a bad LOD index FFbxImporter->AddTokenizedErrorMessage(FTokenizedMessage::Create(EMessageSeverity::Warning, FText::Format(LOCTEXT("Prompt_InvalidLODIndex", "Invalid mesh LOD index {0}, as no prior LOD index exists!"), FText::AsNumber(LODLevel))), FFbxErrors::Generic_Mesh_LOD_InvalidIndex); } else { // Import mesh UStaticMesh* TempStaticMesh = NULL; TempStaticMesh = (UStaticMesh*)FFbxImporter->ImportStaticMeshAsSingle(GetTransientPackage(), *(LODNodeList[bUseLODs? LODLevel: 0]), NAME_None, RF_NoFlags, NULL, BaseStaticMesh, LODLevel); // Add imported mesh to existing model if( TempStaticMesh ) { // Update mesh component BaseStaticMesh->MarkPackageDirty(); // Import worked FNotificationInfo NotificationInfo(FText::GetEmpty()); NotificationInfo.Text = FText::Format(LOCTEXT("LODImportSuccessful", "Mesh for LOD {0} imported successfully!"), FText::AsNumber(LODLevel)); NotificationInfo.ExpireDuration = 5.0f; FSlateNotificationManager::Get().AddNotification(NotificationInfo); } else { // Import failed FNotificationInfo NotificationInfo(FText::GetEmpty()); NotificationInfo.Text = FText::Format(LOCTEXT("LODImportFail", "Failed to import mesh for LOD {0}!"), FText::AsNumber( LODLevel )); NotificationInfo.ExpireDuration = 5.0f; FSlateNotificationManager::Get().AddNotification(NotificationInfo); } } // Cleanup for (int32 i = 0; i < LODNodeList.Num(); ++i) { delete LODNodeList[i]; } } FFbxImporter->ReleaseScene(); }
UAnimSequence * UEditorEngine::ImportFbxAnimation( USkeleton* Skeleton, UObject* Outer, UFbxAnimSequenceImportData* TemplateImportData, const TCHAR* InFilename, const TCHAR* AnimName, bool bImportMorphTracks ) { check(Skeleton); UAnimSequence * NewAnimation=NULL; UnFbx::FFbxImporter* FFbxImporter = UnFbx::FFbxImporter::GetInstance(); const bool bPrevImportMorph = FFbxImporter->ImportOptions->bImportMorph; FFbxImporter->ImportOptions->bImportMorph = bImportMorphTracks; if ( !FFbxImporter->ImportFromFile( InFilename, FPaths::GetExtension( InFilename ) ) ) { // Log the error message and fail the import. FFbxImporter->FlushToTokenizedErrorMessage(EMessageSeverity::Error); } else { // Log the import message and import the mesh. FFbxImporter->FlushToTokenizedErrorMessage(EMessageSeverity::Warning); const FString Filename( InFilename ); // Get Mesh nodes array that bind to the skeleton system, then morph animation is imported. TArray<FbxNode*> FBXMeshNodeArray; FbxNode* SkeletonRoot = FFbxImporter->FindFBXMeshesByBone(Skeleton->GetReferenceSkeleton().GetBoneName(0), true, FBXMeshNodeArray); if (!SkeletonRoot) { FFbxImporter->AddTokenizedErrorMessage(FTokenizedMessage::Create(EMessageSeverity::Error, FText::Format(LOCTEXT("Error_CouldNotFindFbxTrack", "Mesh contains {0} bone as root but animation doesn't contain the root track.\nImport failed."), FText::FromName(Skeleton->GetReferenceSkeleton().GetBoneName(0)))), FFbxErrors::Animation_CouldNotFindRootTrack); FFbxImporter->ReleaseScene(); return NULL; } // Check for blend shape curves that are not skinned. Unskinned geometry can still contain morph curves if( bImportMorphTracks ) { TArray<FbxNode*> MeshNodes; FFbxImporter->FillFbxMeshArray( FFbxImporter->Scene->GetRootNode(), MeshNodes, FFbxImporter ); for( int32 NodeIndex = 0; NodeIndex < MeshNodes.Num(); ++NodeIndex ) { // Its possible the nodes already exist so make sure they are only added once FBXMeshNodeArray.AddUnique( MeshNodes[NodeIndex] ); } } TArray<FbxNode*> SortedLinks; FFbxImporter->RecursiveBuildSkeleton(SkeletonRoot, SortedLinks); if(SortedLinks.Num() == 0) { FFbxImporter->AddTokenizedErrorMessage(FTokenizedMessage::Create(EMessageSeverity::Warning, LOCTEXT("Error_CouldNotBuildValidSkeleton", "Could not create a valid skeleton from the import data that matches the given Skeletal Mesh. Check the bone names of both the Skeletal Mesh for this AnimSet and the animation data you are trying to import.")), FFbxErrors::Animation_CouldNotBuildSkeleton); } else { NewAnimation = FFbxImporter->ImportAnimations( Skeleton, Outer, SortedLinks, AnimName, TemplateImportData, FBXMeshNodeArray); if( NewAnimation ) { // since to know full path, reimport will need to do same UFbxAnimSequenceImportData* ImportData = UFbxAnimSequenceImportData::GetImportDataForAnimSequence(NewAnimation, TemplateImportData); ImportData->Update(UFactory::CurrentFilename); } } } FFbxImporter->ImportOptions->bImportMorph = bPrevImportMorph; FFbxImporter->ReleaseScene(); return NewAnimation; }