]> git.tdb.fi Git - libs/gl.git/commitdiff
Refactor texture handling in the material exporter
authorMikko Rasa <tdb@tdb.fi>
Thu, 16 May 2019 19:56:37 +0000 (22:56 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 17 May 2019 22:10:27 +0000 (01:10 +0300)
blender/io_mspgl/export_material.py

index 3e5210c1ba79df2043e967096d5bc3426042db4a..86fce780c00b22e151aaa04ee79f0941d2ddf3a8 100644 (file)
@@ -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)