FactoryCanImport returns false when it should be true

I’m writing a custom import routine and some of my importer mimics the AssetTools import routine. At one point, UFactory::FactoryCanImport is called. It takes in a filepath and returns a bool indicating whether or not the factory can handle the given file. Problem is, the default implementation returns false if the filepath has an any extension other than “t3d”. Even the UFbxFactory returns false when passed a typical fbx file. When I use the factory anyway, the fbx file seems to be imported without error.

In the AssetTools module, if you step through the import routine, it saves the first factory in the array. It then iterates over all factories in the array, calling FactoryCanImport on each one, and if it returns true, it saves that factory instead and breaks the loop. However, it never returns true if it’s not a t3d extension. Since the first factory in the array was saved before iterating the array, that factory gets used anyway, and it happens to be the appropriate one so it works. Is this the intended behavior? What is the point of iterating over all factories in the array if it’s not a t3d extension, since the first one is always used anyway? Is there another way we’re supposed to check if a factory can deal with a given file before using that factory to import it?

A lot of this code is largely out of date and due for an update. The factory is almost always chosen based on the extensions that the factory supports. See UFactory::GetSupportedFileExtensions. Usually your FactoryCreateBinary or FactoryCreateText should just return false if it cant handle the file.

Thanks for that bit of clarification. I ended up leaving out the inner loop entirely and always using the first factory found that supports the given extension. It doesn’t feel totally comfortable since I’m not sure how to guarantee the first factory found with that extension will always work, but it works at least on the particular types of assets I’m importing.

Hi there, I am stuck trying to implement a UFactory for importing fbx models. How would you feel about sharing your solution to this? I will buy you a drink and sing your praises far and wide. thanks!

Reanimating this thread after such a long time… But it’s exactly the problem I have.

I’d suggest to modify UFactory::FactoryCanImport() by the following code snippet:

bool UFactory::FactoryCanImport( const FString& Filename )
{
	bool CanImport = false;
	FString FileExtension = FPaths::GetExtension(Filename);
	for (const auto& Format : Formats)
	{
		const int32 DelimiterIdx = Format.Find(TEXT(";"));
		if ((DelimiterIdx != INDEX_NONE) && (Format.Left(DelimiterIdx) == FileExtension))
		{
			CanImport = true;
			break;
		}
	}

	if (CanImport && FileExtension == TEXT("t3d"))

followed by the t3d-handling stuff.
That way, FactoryCanImport just checks for the extension string (case sensitive, as I just realize) being listed in the Formats array. That array is supposed to be filled by the derived Factory classes.
Is there anything, that would render that approach to be wrong? For me, it seems to be “just the right thing” ™.