FBX automated command line import -> UE4Editor-Cmd.exe test.uproject -run=ImportAssets

There are multiple parts to this question:

  1. Using UE4Editor-Cmd.exe I need to automate the import for thousands of assets given file paths.

  2. If I don’t do -replaceexisting it asks me every asset if I want to replace it.

  3. JSON is no help either, “bReplaceExisting”:“false”, prompts every time also.

  4. I’d like to use the new Scene Importing as Blueprints (FbxSceneImportFactory): HierarchyType = 2.

  5. I’ve tried using FbxSceneImportFactory as the factory in the command line and it crashes.

  6. I’ve tried using a C++ plugin that uses FbxSceneImportFactory, but I can’t get it to ignore the prompt and just make it. I’ve also tried FbxImportFactory, but it’s complicated and I feel like I’m leaving a lot out and making a big mess, and there is no HierarchyType access.

  7. ImportSettings - “bImportScene”:1 doesn’t do it.


I have a C# tool that automates the creation of the JSON importsettings file. This is edited down to 4 files. It took me a while to figure out how to make different destinations because there is no documentation, so i hope this helps someone. You’ll notice “bReplaceExisting”:“false”, and it prompts for every import.

{"ImportGroups":[
	{"FileNames":[
		"Z:/Building/Exterior/GC_Rack/GC_Rack_1.fbx",
		"Z:/Building/Exterior/HVAC_Ceiling_Unit/HVAC_Ceiling_Unit_1.fbx",
		"Z:/Building/Exterior/Ice_Merchandiser_Leer_L40_Slant/Ice_Merchandiser_Leer_L40_Slant_1.fbx",
	],
	"bReplaceExisting":"false",
	"DestinationPath":"/Game/Assets/Building/Exterior/",
	"FactoryName":"FbxFactory",
	"ImportSettings":{
		"AnimSequenceImportData":{},"SkeletalMeshImportData":{},"TextureImportData":{},
		"StaticMeshImportData":{"bCombineMeshes":1,"bAutoGenerateCollision":1,"bRemoveDegenerates":1}
	}}
,
	{"FileNames":[
		"Z:/Building/Exterior/Signage_Fuel_Center/Food_Panels_1.fbx",
	],
	"bReplaceExisting":"false",
	"DestinationPath":"/Game/Assets/Building/Exterior/Signage_Fuel_Center",
	"FactoryName":"FbxFactory",
	"ImportSettings":{
		"AnimSequenceImportData":{},"SkeletalMeshImportData":{},"TextureImportData":{},
		"StaticMeshImportData":{"bCombineMeshes":1,"bAutoGenerateCollision":1,"bRemoveDegenerates":1}
	}}
]}

then it runs this:

"C:\Program Files\Epic Games\UE_4.21\Engine\Binaries\Win64\UE4Editor-Cmd.exe" "F:\UE4\v4_21_Projects\v4_21_Projects.uproject" -run=ImportAssets -importSettings="C:\UnrealImport\ImportSettings3.json" -AllowCommandletRendering -nosourcecontrol > "C:\UnrealImport\LOG.txt"

I don’t want it to replace so I did not use -replaceexisting

Here is as close as I have gotten with C++ — Warning, this is messy and bad

TArray Files;
Files.Add(TEXT("Z:/Signage_Fuel_Center/Sign.fbx"));
Files.Add(TEXT("Z:/AutoCareCenter/Water_Heater_Water_Heater_1.fbx"));

UFbxSceneImportFactory *FbxSceneFactory = nullptr;
for (UClass* Class : TObjectRange())
{
	if (Class->IsChildOf<UFbxSceneImportFactory>())
	{
		UFbxSceneImportFactory* TestFactory = Class->GetDefaultObject<UFbxSceneImportFactory>();
		if (TestFactory->FactoryCanImport(Files[0]))
		{
			/// Pick the first one for now 
			FbxSceneFactory = TestFactory;
			break;
		}
	}
}

/*UFbxFactory *FbxFactory = nullptr;
for (UClass* Class : TObjectRange())
{
	if (Class->IsChildOf<UFbxFactory>())
	{
		UFbxFactory* TestFactory = Class->GetDefaultObject<UFbxFactory>();
		if (TestFactory->FactoryCanImport(Files[0]))
		{
			/// Pick the first one for now 
			FbxFactory = TestFactory;
			break;
		}
	}
}*/


FString RootDestinationPath = "/Game/Assets/";
FString OutDir;
UClass* ImportAssetType = FbxSceneFactory->ResolveSupportedClass();

UnFbx::FFbxImporter* FbxImporter = UnFbx::FFbxImporter::GetInstance();
UnFbx::FBXImportOptions* GlobalImportSettings = FbxImporter->GetImportOptions();
UnFbx::FBXImportOptions::ResetOptions(GlobalImportSettings);
GlobalImportSettings->bImportScene = true;

for (int i = 0; i < Files.Num(); i++)
{
	FString PathPart;
	FString FilenamePart;
	FString ExtensionPart;
	FPaths::Split(Files[i], PathPart, FilenamePart, ExtensionPart);
	OutDir = PathPart.Replace(TEXT("Z:/Projects/Asset_Library/Maya/"), *RootDestinationPath);
	//

	UAutomatedAssetImportData* newdata = NewObject<UAutomatedAssetImportData>();
	newdata->bReplaceExisting = true;
	newdata->DestinationPath = OutDir;

	FbxSceneFactory->SetAutomatedAssetImportData(newdata);

	bool bImportWasCancelled;
	FString Name = ObjectTools::SanitizeObjectName(FPaths::GetBaseFilename(Files[i]));
	FString PackageName = FPaths::Combine(*OutDir, *Name);
	const FString QualifiedName = PackageName + TEXT(".") + Name;
	UPackage* Pkg = CreatePackage(nullptr, *PackageName);
	//
	const TCHAR* Parms = TEXT("");
	UObject* Result = FbxSceneFactory->ImportObject(ImportAssetType, Pkg, FName(*Name), RF_Public | RF_Standalone | RF_Transactional, Files[i], Parms, bImportWasCancelled);
	

	UBlueprint *BP = Cast<UBlueprint>(Result);

	FbxSceneFactory->SetAutomatedAssetImportData(nullptr);
}
1 Like

Awesome! But how to import all .fbx files from a path but not a certain .fbx file? I tried *.fbx and it didn’t work.

should be : “bReplaceExisting”:true

should be : “bReplaceExisting”:true

I think the only way would be to record their paths to a text file using a batch file. I use a C# app I wrote. It writes all this for me after searching the directory for fbxs. Or Shift right click the selected files and select copy path and paste it to text. Sorry I don’t have a better solution but I have got this to work with minimal headache.

Any update on how to import fbx scene?