]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/export_material.py
Support exporting "empty" materials with only a technique or shader
[libs/gl.git] / blender / io_mspgl / export_material.py
index 4ce892dbba742bb711d76eab69c09da3d40e218a..266790f22d1dd4af80275e638c5565ebaf235c9b 100644 (file)
@@ -7,7 +7,8 @@ def create_technique_resource(material, resources):
        mat_res = resources[material.name+".mat"]
 
        st = Statement("pass", "")
-       st.sub.append(tech_res.create_embed_statement("material", mat_res))
+       if mat_res:
+               st.sub.append(tech_res.create_embed_statement("material", mat_res))
 
        if material.render_mode=='CUSTOM':
                st.sub.append(Statement("shader", material.shader))
@@ -36,7 +37,7 @@ class MaterialExporter:
                material = Material(material)
 
                if self.use_textures:
-                       for p in material.properties:
+                       for p in material.properties.values():
                                if p.texture:
                                        tex_name = p.texture.image.name+".tex2d"
                                        if tex_name not in resources:
@@ -48,7 +49,10 @@ class MaterialExporter:
 
                mat_name = material.name+".mat"
                if mat_name not in resources:
-                       resources[mat_name] = self.export_material(material, resources=resources)
+                       if material.type:
+                               resources[mat_name] = self.export_material(material, resources=resources)
+                       else:
+                               resources[mat_name] = None
 
        def export_technique(self, material, *, resources):
                return create_technique_resource(material, resources)
@@ -57,14 +61,16 @@ class MaterialExporter:
                from .datafile import Resource, Statement
                mat_res = Resource(material.name+".mat", "material")
 
-               st = Statement("pbr")
-               st.sub.append(self.create_property_statement(mat_res, material.base_color, "base_color", resources))
-               st.sub.append(self.create_property_statement(mat_res, material.metalness, "metalness", resources))
-               st.sub.append(self.create_property_statement(mat_res, material.roughness, "roughness", resources))
-               st.sub.append(self.create_property_statement(mat_res, material.normal, "normal", resources, tex_only=True))
-               st.sub.append(self.create_property_statement(mat_res, material.emission, "emission", resources))
+               if material.type!="pbr":
+                       raise Exception("Can't export unknown material type "+material.type)
+
+               st = Statement(material.type)
+               for kw, p in material.properties.items():
+                       ss = self.create_property_statement(mat_res, p, kw, resources)
+                       if ss:
+                               st.sub.append(ss)
                if self.use_textures:
-                       first_tex = (p.texture for p in material.properties if p.texture).__next__
+                       first_tex = (p.texture for p in material.properties.values() if p.texture).__next__()
                        if first_tex and not first_tex.default_filter:
                                from .export_texture import SamplerExporter
                                sampler_export = SamplerExporter()
@@ -73,7 +79,7 @@ class MaterialExporter:
 
                return mat_res
 
-       def create_property_statement(self, mat_res, prop, keyword, resources, *, tex_only=False):
+       def create_property_statement(self, mat_res, prop, keyword, resources):
                from .datafile import Statement
                if self.use_textures and prop.texture:
                        tex_res = resources[prop.texture.image.name+".tex2d"]
@@ -82,7 +88,7 @@ class MaterialExporter:
                                return Statement(keyword+"_map", fn)
                        else:
                                return mat_res.create_reference_statement(keyword+"_map", tex_res)
-               elif tex_only:
+               elif prop.value is None:
                        return
                elif type(prop.value)==tuple:
                        return Statement(keyword, *prop.value)