From 725fccf49be5669a5a74f267eb409bf2fc071e05 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 16 May 2019 22:56:37 +0300 Subject: [PATCH] Refactor texture handling in the material exporter --- blender/io_mspgl/export_material.py | 42 +++++++++++++---------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/blender/io_mspgl/export_material.py b/blender/io_mspgl/export_material.py index 3e5210c1..86fce780 100644 --- a/blender/io_mspgl/export_material.py +++ b/blender/io_mspgl/export_material.py @@ -34,7 +34,7 @@ class MaterialExporter: if self.textures=='INLINE': for s in material.texture_slots: - if s and s.texture.type=='IMAGE': + if s and s.texture.type=='IMAGE' and s.texture.image: tex_name = s.texture.name+".tex2d" if tex_name not in resources: resources[tex_name] = texture_export.export_texture(s.texture) @@ -44,19 +44,22 @@ class MaterialExporter: tech_res = Resource(material.name+".tech") mat_res = resources[material.name+".mat"] - image_texture_slots = [s for s in material.texture_slots if s and s.texture.type=='IMAGE'] + textures = {} + if self.textures!='NONE': + image_texture_slots = [s for s in material.texture_slots if s and s.texture.type=='IMAGE' and s.texture.image] + for s in image_texture_slots: + if s.use_map_color_diffuse: + textures["diffuse_map"] = s.texture + elif s.use_map_normal: + textures["normal_map"] = s.texture if material.technique: if not obj.inherit_tech: return [] st = Statement("inherit", material.technique) - for slot in image_texture_slots: - name = image_name(slot.texture.image) - if slot.use_map_color_diffuse: - st.sub.append(Statement("texture", "diffuse_map", name)) - elif slot.use_map_normal: - st.sub.append(Statement("texture", "normal_map", name)) + for s, t in textures.items(): + st.sub.append(Statement("texture", s, image_name(t.image))) if material.override_material: st.sub.append(tech_res.create_reference_statement("material", "surface", mat_res)) tech_res.statements.append(st) @@ -64,21 +67,14 @@ class MaterialExporter: st = Statement("pass", "") st.sub.append(tech_res.create_embed_statement("material", mat_res)) - if self.textures!='NONE': - diffuse_tex = None - for slot in image_texture_slots: - if slot.use_map_color_diffuse: - diffuse_tex = slot.texture - break - - if diffuse_tex: - ss = Statement("texunit", 0) - if self.textures=='INLINE': - tex_res = resources[slot.texture.name+".tex2d"] - ss.sub.append(tech_res.create_embed_statement("texture2d", tex_res)) - elif tex.image: - ss.sub.append(Statement("texture", image_name(tex.image))) - st.sub.append(ss) + if "diffuse_map" in textures: + diffuse_tex = textures["diffuse_map"] + ss = Statement("texunit", 0) + if self.textures=='INLINE': + ss.sub.append(tech_res.create_embed_statement("texture2d", resources[diffuse_tex.name+".tex2d"])) + else: + ss.sub.append(Statement("texture", image_name(diffuse_tex.image))) + st.sub.append(ss) tech_res.statements.append(st) -- 2.45.2