]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/export_object.py
Allow materials to be overridden when inheriting a technique
[libs/gl.git] / blender / io_mspgl / export_object.py
index 0de98f8446a609692ccd527f8aeddc1bcd599da4..09703e58e902a1847e2b70d3e145117094296122 100644 (file)
@@ -6,6 +6,13 @@ def linear_to_srgb(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):
@@ -25,6 +32,7 @@ class ObjectExporter:
 
                from .outfile import open_output
                out_file = open_output(out_file)
+               path, base = os.path.split(out_file.filename)
 
                from .export_mesh import MeshExporter
                mesh_export = MeshExporter()
@@ -32,7 +40,6 @@ class ObjectExporter:
                        setattr(mesh_export, k, v)
 
                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)
@@ -42,10 +49,32 @@ class ObjectExporter:
                        mesh = mesh_export.export(context, out_file, objs, progress)
                        out_file.end()
 
+               if self.srgb_colors:
+                       self.colormap = linear_to_srgb
+               else:
+                       self.colormap = lambda x: x
+
                if self.external_tech and obj.technique:
-                       out_file.write("technique", '"{}"'.format(obj.technique))
+                       if obj.inherit_tech and (obj.override_material or 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))
+                               if obj.override_material:
+                                       mat_name = mesh.materials[0].name+".mat"
+                                       mat_out = open_output(os.path.join(path, mat_name))
+                                       self.export_material(mesh.materials[0], 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:
-                       path, base = os.path.split(out_file.filename)
                        if self.shared_tech and mesh.materials:
                                tech_name = mesh.materials[0].name+".tech"
                        else:
@@ -62,10 +91,7 @@ class ObjectExporter:
        def export_technique(self, mesh, out_file):
                out_file.begin("pass", '""')
                if mesh.materials:
-                       if self.srgb_colors:
-                               cm = linear_to_srgb
-                       else:
-                               cm = lambda x: x
+                       cm = self.colormap
 
                        if self.material_tex:
                                out_file.begin("material")
@@ -89,20 +115,8 @@ class ObjectExporter:
                                out_file.end()
                                out_file.end()
                        else:
-                               mat = mesh.materials[0]
                                out_file.begin("material")
-                               if any((s and s.use_map_color_diffuse) for s in mat.texture_slots):
-                                       out_file.write("diffuse", 1.0, 1.0, 1.0, 1.0)
-                                       amb = cm(mat.ambient)
-                                       out_file.write("ambient", amb, amb, amb, 1.0)
-                               else:
-                                       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)
-                               spec = mat.specular_color*mat.specular_intensity
-                               out_file.write("specular", spec.r, spec.g, spec.b, 1.0)
-                               out_file.write("shininess", mat.specular_hardness);
+                               self.export_material(mesh.materials[0], out_file)
                                out_file.end()
 
                        if self.textures!="NONE":
@@ -133,7 +147,22 @@ 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()
+
+       def export_material(self, mat, out_file):
+               cm = self.colormap
+               if any((s and s.use_map_color_diffuse) for s in mat.texture_slots):
+                       out_file.write("diffuse", 1.0, 1.0, 1.0, 1.0)
+                       amb = cm(mat.ambient)
+                       out_file.write("ambient", amb, amb, amb, 1.0)
+               else:
+                       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)
+               spec = mat.specular_color*mat.specular_intensity
+               out_file.write("specular", spec.r, spec.g, spec.b, 1.0)
+               out_file.write("shininess", mat.specular_hardness);