]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/material.py
Support exporting "empty" materials with only a technique or shader
[libs/gl.git] / blender / io_mspgl / material.py
index b275caa37f54a92a2a459df8ca9d66d7d12d90d0..3b79036181d0d45ab6af0da226ef39cecb59e2d6 100644 (file)
@@ -20,7 +20,7 @@ class MaterialProperty:
                                self.value = input_socket.default_value[:len(self.value)-1]+(alpha_socket.default_value,)
                        else:
                                self.value = input_socket.default_value[:len(self.value)]
-               else:
+               elif self.value is not None:
                        self.value = input_socket.default_value
 
                from_node, _ = get_linked_node_and_socket(node_tree, input_socket)
@@ -48,12 +48,8 @@ class MaterialProperty:
 class Material:
        def __init__(self, material):
                self.name = material.name
-
-               self.base_color = MaterialProperty((0.8, 0.8, 0.8, 1.0))
-               self.metalness = MaterialProperty(0.0)
-               self.roughness = MaterialProperty(0.5)
-               self.normal = MaterialProperty((0.0, 0.0, 0.1))
-               self.emission = MaterialProperty((0.0, 0.0, 0.0))
+               self.type = None
+               self.properties = {}
 
                self.render_mode = material.render_mode
                self.technique = material.technique
@@ -75,17 +71,34 @@ class Material:
 
                surface_node, _ = get_linked_node_and_socket(material.node_tree, out_node.inputs["Surface"])
                if not surface_node:
-                       raise Exception("Material has no surface node")
+                       if self.render_mode=='BUILTIN':
+                               raise Exception("Empty material can't use builtin rendering mode")
+                       return
                elif surface_node.type!='BSDF_PRINCIPLED':
                        raise Exception("Unsupported surface node type "+surface_node.type)
 
-               self.base_color.set_from_input(material.node_tree, surface_node.inputs["Base Color"], surface_node.inputs["Alpha"])
-               self.metalness.set_from_input(material.node_tree, surface_node.inputs["Metallic"])
-               self.roughness.set_from_input(material.node_tree, surface_node.inputs["Roughness"])
-               self.normal.set_from_input(material.node_tree, surface_node.inputs["Normal"])
-               self.emission.set_from_input(material.node_tree, surface_node.inputs["Emission"])
+               self.type = "pbr"
+
+               base_color = self.properties["base_color"] = MaterialProperty((0.8, 0.8, 0.8, 1.0))
+               metalness = self.properties["metalness"] = MaterialProperty(0.0)
+               roughness = self.properties["roughness"] = MaterialProperty(0.5)
+               normal = self.properties["normal"] = MaterialProperty(None)
+               emission = self.properties["emission"] = MaterialProperty((0.0, 0.0, 0.0))
+
+               base_color.set_from_input(material.node_tree, surface_node.inputs["Base Color"], surface_node.inputs["Alpha"])
+               metalness.set_from_input(material.node_tree, surface_node.inputs["Metallic"])
+               roughness.set_from_input(material.node_tree, surface_node.inputs["Roughness"])
+               normal.set_from_input(material.node_tree, surface_node.inputs["Normal"])
+               emission.set_from_input(material.node_tree, surface_node.inputs["Emission"])
 
-               self.properties = (self.base_color, self.metalness, self.roughness, self.normal, self.emission)
+               sampler_settings = None
+               for p in self.properties.values():
+                       if p.texture:
+                               settings = (p.texture.default_filter, p.texture.interpolation, p.texture.use_mipmap, p.texture.max_anisotropy)
+                               if sampler_settings is None:
+                                       sampler_settings = settings
+                               elif settings!=sampler_settings:
+                                       raise Exception("Conflicting sampler settings in material textures")
 
 
 class MaterialMap:
@@ -137,10 +150,10 @@ def create_material_map(context, material):
        if not material.material_map:
                raise Exception("Material is not part of a material map")
 
-       tech = material.technique
+       shader = material.shader
        materials = []
        for m in context.blend_data.materials:
-               if m.material_map and m.technique==tech:
+               if m.material_map and m.shader==shader:
                        materials.append(m)
 
        mat_map = MaterialMap(materials)