]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/export_object.py
Support defining and exporting object LoDs in Blender
[libs/gl.git] / blender / io_mspgl / export_object.py
index 09703e58e902a1847e2b70d3e145117094296122..b910add12fad3ed86ef397861de92fc8bb6a50b9 100644 (file)
@@ -13,6 +13,13 @@ def image_name(i):
        else:
                return i.name
 
+def external_name(out_file, ext, index):
+       path, base = os.path.split(out_file.filename)
+       base = os.path.splitext(base)[0]
+       if index>0:
+               base += "_lod{}".format(index)
+       return path, base+ext
+
 
 class ObjectExporter:
        def __init__(self):
@@ -23,6 +30,7 @@ class ObjectExporter:
                self.separate_tech = False
                self.external_tech = True
                self.shared_tech = True
+               self.export_lods = True
 
        def export(self, context, out_file, objs=None, progress=None):
                if objs is None:
@@ -30,35 +38,80 @@ class ObjectExporter:
                else:
                        obj = objs[0]
 
+               lods = [obj]
+               for c in obj.children:
+                       if c.lod_for_parent:
+                               if c.lod_index>=len(lods):
+                                       lods += [None]*(c.lod_index+1-len(lods))
+                               lods[c.lod_index] = c
+
                from .outfile import open_output
                out_file = open_output(out_file)
-               path, base = os.path.split(out_file.filename)
 
+               prev_mesh = None
+               prev_tech = (None, None)
+               for i, l in enumerate(lods):
+                       if i>0:
+                               out_file.begin("level_of_detail", i)
+                               objs = [l]
+
+                       same_mesh = (l.data.name==prev_mesh)
+                       if i==0 or not same_mesh:
+                               mesh = self.export_object_mesh(context, out_file, i, objs, progress)
+                               prev_mesh = l.data.name
+
+                       same_tech = True
+                       mat = None
+                       if l.material_slots and l.material_slots[0].material:
+                               mat = l.material_slots[0].material.name
+                               if mat!=prev_tech[1]:
+                                       same_tech = False
+                       if self.external_tech and l.technique!=prev_tech[0]:
+                               same_tech = False
+                       if i==0 or not same_tech:
+                               self.export_object_technique(l, mesh, out_file, i)
+                               prev_tech = (l.technique, mat)
+
+                       if i>0:
+                               out_file.end()
+
+       def export_object_mesh(self, context, out_file, lod_index, objs, progress):
                from .export_mesh import MeshExporter
                mesh_export = MeshExporter()
                for k, v in self.__dict__.items():
                        setattr(mesh_export, k, v)
 
                if self.separate_mesh:
-                       base, ext = os.path.splitext(base)
-                       mesh_out = open_output(os.path.join(path, base+".mesh"))
+                       from .outfile import open_output
+                       path, name = external_name(out_file, ".mesh", lod_index)
+                       mesh_out = open_output(os.path.join(path, name))
                        mesh = mesh_export.export(context, mesh_out, objs, progress)
-                       out_file.write("mesh", '"{}.mesh"'.format(base))
+                       out_file.write("mesh", '"{}"'.format(name))
                else:
                        out_file.begin("mesh")
                        mesh = mesh_export.export(context, out_file, objs, progress)
                        out_file.end()
 
+               return mesh
+
+       def export_object_technique(self, obj, mesh, out_file, lod_index):
                if self.srgb_colors:
                        self.colormap = linear_to_srgb
                else:
                        self.colormap = lambda x: x
 
+               material = None
+               if obj.material_slots:
+                       material = obj.material_slots[0].material
+
+               from .outfile import open_output
+               path, name = external_name(out_file, ".tech", lod_index)
+
                if self.external_tech and obj.technique:
-                       if obj.inherit_tech and (obj.override_material or mesh.materials[0].texture_slots):
+                       if obj.inherit_tech and material and (obj.override_material or material.texture_slots):
                                out_file.begin("technique")
                                out_file.begin("inherit", '"{}"'.format(obj.technique))
-                               for slot in mesh.materials[0].texture_slots:
+                               for slot in material.texture_slots:
                                        if slot and slot.texture.type=="IMAGE":
                                                name = image_name(slot.texture.image)
                                                if slot.use_map_color_diffuse:
@@ -66,31 +119,28 @@ class ObjectExporter:
                                                elif slot.use_map_normal:
                                                        out_file.write("texture", '"normal_map"', '"{}"'.format(name))
                                if obj.override_material:
-                                       mat_name = mesh.materials[0].name+".mat"
+                                       mat_name = material.name+".mat"
                                        mat_out = open_output(os.path.join(path, mat_name))
-                                       self.export_material(mesh.materials[0], mat_out)
+                                       self.export_material(material, mat_out)
                                        out_file.write("material", '""', '"{}"'.format(mat_name))
                                out_file.end()
                                out_file.end()
                        else:
                                out_file.write("technique", '"{}"'.format(obj.technique))
                elif self.separate_tech:
-                       if self.shared_tech and mesh.materials:
-                               tech_name = mesh.materials[0].name+".tech"
-                       else:
-                               base, ext = os.path.splitext(base)
-                               tech_name = base+".tech"
-                       tech_out = open_output(os.path.join(path, tech_name))
-                       self.export_technique(mesh, tech_out)
-                       out_file.write("technique", '"{}"'.format(tech_name))
+                       if self.shared_tech and material:
+                               name = material.name+".tech"
+                       tech_out = open_output(os.path.join(path, name))
+                       self.export_technique_definition(material, mesh, tech_out)
+                       out_file.write("technique", '"{}"'.format(name))
                else:
                        out_file.begin("technique")
-                       self.export_technique(mesh, out_file)
+                       self.export_technique_definition(material, mesh, out_file)
                        out_file.end()
 
-       def export_technique(self, mesh, out_file):
+       def export_technique_definition(self, material, mesh, out_file):
                out_file.begin("pass", '""')
-               if mesh.materials:
+               if material:
                        cm = self.colormap
 
                        if self.material_tex:
@@ -116,11 +166,11 @@ class ObjectExporter:
                                out_file.end()
                        else:
                                out_file.begin("material")
-                               self.export_material(mesh.materials[0], out_file)
+                               self.export_material(material, out_file)
                                out_file.end()
 
                        if self.textures!="NONE":
-                               for slot in mesh.materials[0].texture_slots:
+                               for slot in material.texture_slots:
                                        if not slot:
                                                continue