]> git.tdb.fi Git - libs/gl.git/commitdiff
Allow materials to be overridden when inheriting a technique
authorMikko Rasa <tdb@tdb.fi>
Sat, 6 Dec 2014 12:39:12 +0000 (14:39 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 6 Dec 2014 12:41:14 +0000 (14:41 +0200)
blender/io_mspgl/export_object.py
blender/io_mspgl/properties.py
source/technique.cpp
source/technique.h

index a12474af94bbfe5505a0d7e8d2fd016b1774aebf..09703e58e902a1847e2b70d3e145117094296122 100644 (file)
@@ -32,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()
@@ -39,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)
@@ -49,8 +49,13 @@ 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:
-                       if obj.inherit_tech and mesh.materials[0].texture_slots:
+                       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:
@@ -60,12 +65,16 @@ class ObjectExporter:
                                                        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:
@@ -82,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")
@@ -109,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":
@@ -157,3 +151,18 @@ class ObjectExporter:
                                        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);
index f6de7a36acdb543fa6b2446eb28d08b8e14f32db..9d0d666a4f35661b3ce279fdf77a468040745d66 100644 (file)
@@ -12,9 +12,12 @@ class MspGLProperties(bpy.types.Panel):
 
                self.layout.prop(obj, "technique");
                self.layout.prop(obj, "inherit_tech");
+               if obj.inherit_tech:
+                       self.layout.prop(obj, "override_material");
                self.layout.prop(obj, "compound");
 
 def register_properties():
        bpy.types.Object.technique = bpy.props.StringProperty(name="Technique", description="Name of the technique to use for rendering")
        bpy.types.Object.inherit_tech = bpy.props.BoolProperty(name="Inherit technique", description="Inherit from the technique to customize textures")
+       bpy.types.Object.override_material = bpy.props.BoolProperty(name="Override material", description="Override material in the inherited texture as well", default=True)
        bpy.types.Object.compound = bpy.props.BoolProperty(name="Compound with parent", description="Join this object to its parent when exporting")
index aa27d99cc7857118888fced1d59e1647347c2abf..15406a931e10e8a6249434d91a9d7002180efeea 100644 (file)
@@ -77,10 +77,25 @@ void Technique::Loader::pass(const string &n)
 Technique::InheritLoader::InheritLoader(Technique &t, Collection &c):
        DataFile::CollectionObjectLoader<Technique>(t, &c)
 {
+       add("material", &InheritLoader::material);
        add("texture", &InheritLoader::texture);
 }
 
-void Technique::InheritLoader::texture(const std::string &slot, const string &name)
+void Technique::InheritLoader::material(const string &pass_tag, const string &name)
+{
+       RenderPass &pass = get_item(obj.passes, pass_tag);
+       const Material &mat = get_collection().get<Material>(name);
+       if(const Material *base_mat = pass.get_material())
+       {
+               for(PassMap::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
+                       if(i->second.get_material()==base_mat)
+                               i->second.set_material(&mat);
+       }
+       else
+               pass.set_material(&mat);
+}
+
+void Technique::InheritLoader::texture(const string &slot, const string &name)
 {
        Texture &tex = get_collection().get<Texture>(name);
        for(PassMap::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
index 141fa9c80a2b3f42cbfd2a87c1b218253993bce6..48e393fc40f592d35870c8f0a7b5ed3eb84fa688 100644 (file)
@@ -35,6 +35,7 @@ private:
                InheritLoader(Technique &, Collection &);
 
        private:
+               void material(const std::string &, const std::string &);
                void texture(const std::string &, const std::string &);
        };