[Bf-extensions-cvs] [5c9ecad1] master: Fix T103976: FBX Export: Duplicate materials cause material indices to be offset

Mysteryem noreply at git.blender.org
Mon Jan 23 15:47:30 CET 2023


Commit: 5c9ecad1d29edd8ba9ebaaad8f71839bd5d88324
Author: Mysteryem
Date:   Mon Jan 23 15:37:29 2023 +0100
Branches: master
https://developer.blender.org/rBA5c9ecad1d29edd8ba9ebaaad8f71839bd5d88324

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

===================================================================

M	io_scene_fbx/export_fbx_bin.py

===================================================================

diff --git a/io_scene_fbx/export_fbx_bin.py b/io_scene_fbx/export_fbx_bin.py
index 3ee9c75e..75d8529f 100644
--- a/io_scene_fbx/export_fbx_bin.py
+++ b/io_scene_fbx/export_fbx_bin.py
@@ -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



More information about the Bf-extensions-cvs mailing list