From cce6f72de36d3cff922c8fa9a894e4a1233b9f86 Mon Sep 17 00:00:00 2001
From: Bastien Montagne <montagne29@wanadoo.fr>
Date: Sat, 15 Mar 2014 13:21:44 +0100
Subject: [PATCH] Fix material indices in mesh's faces.

Patch by Jens Restemeier, many thanks! :)

Differential Revision: https://developer.blender.org/D402
---
 io_scene_fbx/export_fbx_bin.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/io_scene_fbx/export_fbx_bin.py b/io_scene_fbx/export_fbx_bin.py
index a8ba3ff5c..ec6beb5b7 100644
--- a/io_scene_fbx/export_fbx_bin.py
+++ b/io_scene_fbx/export_fbx_bin.py
@@ -1150,11 +1150,15 @@ def fbx_data_mesh_elements(root, me, scene_data):
                 blmats_to_fbxmats_idxs = [me_fbxmats_idx[m] for m in me_blmats]
                 mat_idx_limit = len(blmats_to_fbxmats_idxs)
                 def_mat = blmats_to_fbxmats_idxs[0]
-                _gen = (blmats_to_fbxmats_idxs[m] if m < mat_idx_limit else def_mat for m in range(len(me_blmats)))
+                _gen = (blmats_to_fbxmats_idxs[m] if m < mat_idx_limit else def_mat for m in t_pm)
                 t_pm = array.array(data_types.ARRAY_INT32, _gen)
 
                 elem_data_single_string(lay_mat, b"MappingInformationType", b"ByPolygon")
-                elem_data_single_string(lay_mat, b"ReferenceInformationType", b"Direct")
+                # XXX Logically, should be "Direct" reference type, since we do not have any index array, and have one
+                #     value per polygon...
+                #     But looks like FBX expects it to be IndexToDirect here (maybe because materials are already
+                #     indices??? *sigh*).
+                elem_data_single_string(lay_mat, b"ReferenceInformationType", b"IndexToDirect")
                 elem_data_single_int32_array(lay_mat, b"Materials", t_pm)
                 del t_pm
             else:
-- 
GitLab