]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/export_material.py
Support exporting splat materials from Blender
[libs/gl.git] / blender / io_mspgl / export_material.py
index a0c0432e993e2b37f1077036bfbc8dbbdc3613d5..fc144b7ab86d163d71c688207dd242219c4d3fdf 100644 (file)
@@ -1,3 +1,5 @@
+import itertools
+
 class MaterialExporter:
        def export_technique_resources(self, ctx, material, resources):
                from .export_texture import SamplerExporter, TextureExporter
@@ -8,7 +10,7 @@ class MaterialExporter:
                if type(material)!=Material:
                        material = Material(material)
 
-               textured_props = [p for p in material.properties if p.texture]
+               textured_props = [p for p in itertools.chain(material.properties, *(s.properties for s in material.sub_materials)) if p.texture]
 
                ctx.set_slices(len(textured_props)+1)
                for p in textured_props:
@@ -166,7 +168,7 @@ class MaterialExporter:
                from .datafile import Resource, Statement, Token
                mat_res = Resource(material.name+".mat", "material")
 
-               if material.type!="pbr" and material.type!="unlit":
+               if material.type!="pbr" and material.type!="unlit" and material.type!="splat":
                        raise Exception("Can't export material {} of unknown type {}".format(material.name, material.type))
 
                mat_res.statements.append(Statement("type", Token(material.type)));
@@ -174,7 +176,19 @@ class MaterialExporter:
                        st = self.create_property_statement(mat_res, p, resources)
                        if st:
                                mat_res.statements.append(st)
-               textures = [p.texture for p in material.properties if p.texture]
+
+               if material.sub_materials:
+                       for k, s in material.array_storage.items():
+                               mat_res.statements.append(Statement(k.replace("_map", "_storage"), Token(s[0]), s[1], s[2]))
+                       for s in material.sub_materials:
+                               st = Statement("sub")
+                               for p in s.properties:
+                                       ss = self.create_property_statement(mat_res, p, resources, raw_texture=True)
+                                       if ss:
+                                               st.sub.append(ss)
+                               mat_res.statements.append(st)
+
+               textures = [p.texture for p in itertools.chain(material.properties, *(s.properties for s in material.sub_materials)) if p.texture]
                if textures:
                        from .export_texture import SamplerExporter
                        sampler_export = SamplerExporter()
@@ -184,12 +198,18 @@ class MaterialExporter:
 
                return mat_res
 
-       def create_property_statement(self, mat_res, prop, resources):
+       def create_property_statement(self, mat_res, prop, resources, *, raw_texture=False):
                from .datafile import Statement
                if prop.texture:
                        from .export_texture import TextureExporter
                        texture_export = TextureExporter()
                        tex_res = resources[texture_export.get_texture_name(prop.texture, prop.tex_channels)]
+                       if raw_texture:
+                               for s in tex_res.statements:
+                                       if s.keyword.startswith("external_data"):
+                                               return mat_res.create_reference_statement(prop.tex_keyword, s.args[0])
+                                       elif s.keyword.startswith("external_image"):
+                                               return Statement(prop.tex_keyword, s.args[0])
                        return mat_res.create_reference_statement(prop.tex_keyword, tex_res)
                elif not prop.keyword:
                        return