The initial fix made for #103976 incorrectly identified the issue as
being part of the exporter and changed the exporter to work with the
bugged behaviour of the importer. This patch fixes the issue on the
importer side.
Unlike Blender, Materials always belong to the FBX Model (Object
equivalent), whereas Blender defaults to the Materials belonging to the
Mesh (FBX Geometry equivalent). When a Mesh was used by multiple
Objects, to prevent appending the Materials of each FBX Model to the
Mesh, a set of already used Materials was kept and non-unique Materials
would be skipped. However, skipping non-unique Materials would mean the
material indices of a Mesh no longer match its Materials, causing the
material indices to appear offset or invalid.
This patch changes the import of Meshes containing duplicate Materials.
The duplicates are now added to the Mesh in their original order
instead of being skipped.
This patch changes the import of Meshes used by more than one Object
whereby the imported Objects have different Materials. After the
Materials from the first imported Object are appended to the Mesh, any
different Materials from other imported Objects at the same indices have
their Material Slot linked to the Object with the differing Material set
in that Material Slot. If other imported Objects have more Materials
than the number of existing materials on the Mesh, the additional
Materials are appended to the Mesh. Previously, Materials that were
already present in the Mesh would be skipped and new Materials would be
appended to the end of the Mesh's Materials.
Meshes containing duplicate materials that were exported by Blender 3.5
and earlier would export duplicate materials despite only one of the
duplicates being used by the exported mesh (fixed in #104667). All the
duplicates in these FBX files are now imported with this change.
Due to the previous incorrect attempt fix for #103976, FBX with
duplicate materials exported by Blender 3.5 have incorrect material
indices (fixed in #104667). The importer issue fixed by this patch
combined with the exporter issue would cause such incorrectly exported
FBX files to import as if they had been exported correctly in the first
place. Now that the importer issue is fixed, this is no longer the case
and the importer will now show that those Blender 3.5 exported FBX with
duplicate materials have incorrect material indices.