What the good method for import a fbx file in editor with c++ ?

Hello,
I’m working on a plugin that import a StaticMesh into the Content Browser (not runtime) but the import does not seem to work. :confused:
The file Mesh.uasset is created in Content\MyMeshFolder but it is only 1Ko and is not readable by the engine
This is the first time I made an fbx import in C++, I do not think I’m using the right method and I do not see how to do this

This is my current code

FReply SBlenderImporterAssetSlate::ImportButtonClicked()
{
	UE_LOG(LogTemp, Warning, TEXT("Start"));
	UnFbx::FFbxImporter* FbxImporter = UnFbx::FFbxImporter::GetInstance();
	if (FbxImporter->ImportFromFile(FbxPath, TEXT("fbx")))
	{
		UE_LOG(LogTemp, Warning, TEXT("ImportFromFile success"));
		TArray<FbxNode*> FbxMeshArray;
		FbxImporter->FillFbxMeshArray(FbxImporter->Scene->GetRootNode(), FbxMeshArray, FbxImporter);
		UFbxStaticMeshImportData* ImportData = NewObject<UFbxStaticMeshImportData>(GetTransientPackage(), "MyImportData", RF_NoFlags, nullptr);
		ImportedMesh = FbxImporter->ImportStaticMeshAsSingle(GetTransientPackage(), FbxMeshArray, "MyMesh", RF_Public, ImportData, ImportedMesh);

		if (ImportedMesh->GetFullName() != "None")
		{
			UE_LOG(LogTemp, Warning, TEXT("ImportStaticMeshAsSingle success"));
			UPackage* Package = CreatePackage(NULL, TEXT("/Game/MyMeshFolder/Mesh")); //Create package if not exist
			Package->FullyLoad(); //Load full package for import
			Package->MarkPackageDirty(); // Mark package dirty to be able to save it
			FAssetRegistryModule::AssetCreated(ImportedMesh); //Notifies registry of new asset in-memory
			FString PackageFileName = FPackageName::LongPackageNameToFilename(TEXT("/Game/MyMeshFolder/Mesh"), FPackageName::GetAssetPackageExtension());
			bool bSaved = UPackage::SavePackage(Package, ImportedMesh, EObjectFlags::RF_Public, *PackageFileName, GError, nullptr, true, true);
			if (bSaved == true)
			{
				UE_LOG(LogTemp, Warning, TEXT("Saved package"));
			}
		}
	}
	return FReply::Handled();
}

And this the Output Log

LogTemp: Warning: Start
LogSlate: Took 0.000287 seconds to synchronously load lazily loaded font '../../../Engine/Content/Slate/Fonts/Roboto-Light.ttf' (167K)
LogFbx: Loading FBX Scene from G:\SM_SuzanneLod0.fbx
LogFbx: FBX Scene Loaded Succesfully
LogTemp: Warning: ImportFromFile success
LogFbx: Warning: Triangulating static mesh SuzanneLod0
LogFbx: Warning: Warning: The imported mesh is very small. This is most likely an issue with the units used when exporting to FBX.
LogTemp: Warning: ImportStaticMeshAsSingle success
LogSlate: FSceneViewport::OnFocusLost() reason 2
LogSavePackage: Save=50.57ms
LogSavePackage: Moving '../../../../../../Projet perso/Unreal Projects/Marketplace/Blender For Unreal Import/Engine 4.19/Saved/Mesh6F4104AB4DB366300413F78EB2E07A03.tmp' to '../../../../../../Projet perso/Unreal Projects/Marketplace/Blender For Unreal Import/Engine 4.19/Content/MyMeshFolder/Mesh.uasset'
LogSavePackage: Display: Finished SavePackage ../../../../../../Projet perso/Unreal Projects/Marketplace/Blender For Unreal Import/Engine 4.19/Content/MyMeshFolder/Mesh.uasset
LogTemp: Warning: Saved package

Thank you for your help, Xavier.

1 Like

Hi Loux,

Is there a reason you are attempting to write an FBX importer yourself? The engine has native support for FBX meshes.

Hi Darkwind,
Yes, I’m working on a plugin that will import and configure multiple assets at the same time via an .ini file

Hi Loux,

Looking at the source, importing an FBX file seems to be much more complicated than what your code is doing. I would suggest looking at the implementation of UFbxFactor::FactoryCreateFile (located in %EngineInstallDir%\Engine\Source\UnrealEd\Private\Fbx\FbxFactory.cpp) for reference. Note, though, that your plugin will be subject to any future changes to the FBX import system.

Thank you so much Darkwind.

I did some research about UFbxFactory::FactoryCreateFile and I redid my code and it works!

	UE_LOG(LogTemp, Warning, TEXT("Start02"));
	UFbxFactory* factory = NewObject<UFbxFactory>(UFbxFactory::StaticClass(), FName("Factory"), RF_NoFlags);
	factory->ImportUI->StaticMeshImportData->bCombineMeshes = true;

	bool canceled = false;
	UPackage* Package = CreatePackage(NULL, TEXT("/Game/MyMeshFolder/Mesh")); //Create package if not exist
	factory->ImportObject(factory->ResolveSupportedClass(), Package, FName("MyMesh"), RF_Public | RF_Standalone, FbxPath, nullptr, canceled);
	if (canceled == true)
	{
		UE_LOG(LogTemp, Warning, TEXT("Import canceled."));
	}

I’ve tried a lot of different ways to get this to work and still nothing is perfect. Here’s a post with all of my code and maybe something will help you and you could help me.
fbx-automated-command-line-import