From c2a85a2d86394611ea430c10731a9ee9b694570a Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Thu, 25 May 2023 20:54:50 +0800 Subject: [PATCH] [fix] fix empty material slot export error. - fix the issue that the object with empty material slot will cause a None crash when exporting BM file. --- ballance_blender_plugin/BMFILE_export.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/ballance_blender_plugin/BMFILE_export.py b/ballance_blender_plugin/BMFILE_export.py index 1e61a64..9e0bfe8 100644 --- a/ballance_blender_plugin/BMFILE_export.py +++ b/ballance_blender_plugin/BMFILE_export.py @@ -202,6 +202,10 @@ def export_bm(context, bmx_filepath, prefs_fncg, opts_exportMode, opts_exportTar mesh_usedBlenderMtl = mesh.materials[:] mesh_noMaterial = len(mesh_usedBlenderMtl) == 0 for mat in mesh_usedBlenderMtl: + # skip empty mtl slot + if mat is None: + continue + # add into mtl set if mat not in materialSet: materialSet.add(mat) materialList.append(mat) @@ -212,7 +216,10 @@ def export_bm(context, bmx_filepath, prefs_fncg, opts_exportMode, opts_exportTar mesh_vIndex = [] for f, f_index in mesh_faceIndexPairs: # confirm material use - if mesh_noMaterial: + # a face without mtl have 2 situations. first is the whole object do not have mtl + # another is this face use an empty mtl slot. + mesh_faceNoMtl = mesh_noMaterial or (mesh_usedBlenderMtl[f.material_index] is None) + if mesh_faceNoMtl: mesh_materialIndex = 0 else: mesh_materialIndex = materialList.index(mesh_usedBlenderMtl[f.material_index]) @@ -235,7 +242,7 @@ def export_bm(context, bmx_filepath, prefs_fncg, opts_exportMode, opts_exportTar mesh_vIndex[0], mesh_vtIndex[0], mesh_vnIndex[0]) # set used material - UTILS_file_io.write_bool(fmesh, not mesh_noMaterial) + UTILS_file_io.write_bool(fmesh, not mesh_faceNoMtl) UTILS_file_io.write_uint32(fmesh, mesh_materialIndex) # free splited normals