]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/export_object.py
Add an object property for inheriting the specified technique
[libs/gl.git] / blender / io_mspgl / export_object.py
index 12b15b63c26586abe203629992782d594b266a64..a12474af94bbfe5505a0d7e8d2fd016b1774aebf 100644 (file)
@@ -1,17 +1,35 @@
+import os
+
 def linear_to_srgb(l):
        if l<0.0031308:
                return 12.92*l
        else:
                return 1.055*(l**(1/2.4))-0.055
 
+def image_name(i):
+       fp = i.filepath
+       if fp:
+               return os.path.split(fp)[1]
+       else:
+               return i.name
+
 
 class ObjectExporter:
        def __init__(self):
                self.material_tex = False
                self.srgb_colors = True
                self.textures = "REF"
+               self.separate_mesh = False
+               self.separate_tech = False
+               self.external_tech = True
+               self.shared_tech = True
+
+       def export(self, context, out_file, objs=None, progress=None):
+               if objs is None:
+                       obj = context.active_object
+               else:
+                       obj = objs[0]
 
-       def export(self, context, out_file):
                from .outfile import open_output
                out_file = open_output(out_file)
 
@@ -20,11 +38,48 @@ class ObjectExporter:
                for k, v in self.__dict__.items():
                        setattr(mesh_export, k, v)
 
-               out_file.begin("mesh")
-               mesh = mesh_export.export(context, out_file)
-               out_file.end()
+               if self.separate_mesh:
+                       path, base = os.path.split(out_file.filename)
+                       base, ext = os.path.splitext(base)
+                       mesh_out = open_output(os.path.join(path, base+".mesh"))
+                       mesh = mesh_export.export(context, mesh_out, objs, progress)
+                       out_file.write("mesh", '"{}.mesh"'.format(base))
+               else:
+                       out_file.begin("mesh")
+                       mesh = mesh_export.export(context, out_file, objs, progress)
+                       out_file.end()
+
+               if self.external_tech and obj.technique:
+                       if obj.inherit_tech and mesh.materials[0].texture_slots:
+                               out_file.begin("technique")
+                               out_file.begin("inherit", '"{}"'.format(obj.technique))
+                               for slot in mesh.materials[0].texture_slots:
+                                       if slot and slot.texture.type=="IMAGE":
+                                               name = image_name(slot.texture.image)
+                                               if slot.use_map_color_diffuse:
+                                                       out_file.write("texture", '"diffuse_map"', '"{}"'.format(name))
+                                               elif slot.use_map_normal:
+                                                       out_file.write("texture", '"normal_map"', '"{}"'.format(name))
+                               out_file.end()
+                               out_file.end()
+                       else:
+                               out_file.write("technique", '"{}"'.format(obj.technique))
+               elif self.separate_tech:
+                       path, base = os.path.split(out_file.filename)
+                       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))
+               else:
+                       out_file.begin("technique")
+                       self.export_technique(mesh, out_file)
+                       out_file.end()
 
-               out_file.begin("technique")
+       def export_technique(self, mesh, out_file):
                out_file.begin("pass", '""')
                if mesh.materials:
                        if self.srgb_colors:
@@ -47,7 +102,7 @@ class ObjectExporter:
                                out_file.write("storage", "RGB", len(mesh.materials), 1)
                                texdata = '"'
                                for m in mesh.materials:
-                                       color = [int(cm(c)*255) for c in m.diffuse_color]
+                                       color = [int(cm(c)*255) for c in m.diffuse_color*mat.diffuse_intensity]
                                        texdata += "\\x%02X\\x%02X\\x%02X"%tuple(color)
                                texdata += '"'
                                out_file.write("raw_data", texdata)
@@ -61,7 +116,7 @@ class ObjectExporter:
                                        amb = cm(mat.ambient)
                                        out_file.write("ambient", amb, amb, amb, 1.0)
                                else:
-                                       diff = mat.diffuse_color
+                                       diff = mat.diffuse_color*mat.diffuse_intensity
                                        out_file.write("diffuse", cm(diff.r), cm(diff.g), cm(diff.b), 1.0)
                                        amb = diff*mat.ambient
                                        out_file.write("ambient", cm(amb.r), cm(amb.g), cm(amb.b), 1.0)
@@ -98,8 +153,7 @@ class ObjectExporter:
                                                out_file.write("raw_data", texdata)
                                                out_file.end()
                                        else:
-                                               out_file.write("texture", '"%s"'%tex.image.name)
+                                               out_file.write("texture", '"%s"'%image_name(tex.image))
                                        out_file.end()
 
                out_file.end()
-               out_file.end()