Fix T103976: FBX Export: Duplicate materials cause material indices to be offset

FBX exporter would add duplicate materials to a mesh only once, but would increment the material index once for each duplicate, causing material indices to become offset and/or go out of bounds.

This patch adds a check that skips incrementing the material index if the material has already been added to the mesh.

---

I've added an 'XXX' comment that points out that this area of code causes material slots on the same mesh that contain the same material to be merged into one material slot because the `material_indices` dict's keys are materials only, so a material can only be given one index (this behaviour is not changed by this patch).

I find this to be undesirable behaviour because material slots are useful for working with different parts of the same mesh, such as animating the material of a specific material slot in external software.

Modifying the code to support maintaining multiple slots with the same material is beyond the scope of this patch and I haven't looked into what modifications would be required beyond changing the keying or changing the indices to be multivalued. There could even be a restriction that the FBX format doesn't allow duplicate materials on a mesh in the first place, which would likely require creating copies of duplicate materials on export instead.

Reviewed By: mont29

Maniphest Tasks: T103976

Differential Revision: https://developer.blender.org/D17052
This commit is contained in:
Mysteryem 2023-01-23 15:37:29 +01:00 committed by Bastien Montagne
parent c0a678d368
commit 5c9ecad1d2
Notes: blender-bot 2023-07-06 10:16:02 +02:00
Referenced by issue #103976, FBX Export: Duplicate materials cause material indices to be offset
Referenced by pull request #104667, Fix #104665: FBX Export: Duplicate materials cause material indices to be offset
Referenced by commit 0a4e4f2704, Fix #104665: FBX Export: Duplicate materials cause material indices to be offset
Referenced by commit 65ef64dd01, Fix #104737: FBX: Materials missing from linked duplicates
1 changed files with 6 additions and 1 deletions

View File

@ -2657,8 +2657,13 @@ def fbx_data_from_scene(scene, depsgraph, settings):
if ob_obj.type not in BLENDER_OBJECT_TYPES_MESHLIKE:
continue
_mesh_key, me, _free = data_meshes[ob_obj]
material_indices = mesh_material_indices.setdefault(me, {})
if ma in material_indices:
# Material has already been found for this mesh.
# XXX If a mesh has multiple material slots with the same material, they are combined into one slot.
continue
idx = _objs_indices[ob_obj] = _objs_indices.get(ob_obj, -1) + 1
mesh_material_indices.setdefault(me, {})[ma] = idx
material_indices[ma] = idx
del _objs_indices
# Textures