]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/mesh.py
Implement material maps for exporting objects with multiple materials
[libs/gl.git] / blender / io_mspgl / mesh.py
index 95749f46d4eb67aede93b53a87bdb4be1f789487..125bc5bbbaf01a938af18ec21eefb76239f1534b 100644 (file)
@@ -393,6 +393,30 @@ class Mesh:
                                for g in v.groups:
                                        g.group = group_index_map[g.group]
 
+       def apply_material_map(self, material_map):
+               for m in self.materials:
+                       if m not in material_map.materials:
+                               raise Exception("Material map is not compatible with Mesh")
+
+               if self.use_uv=='NONE':
+                       return
+
+               layer = UvLayer("material_map")
+               if self.use_uv=='UNIT0':
+                       self.uv_layers = [layer]
+                       layer.unit = 0
+               else:
+                       self.uv_layers.append(layer)
+                       used_units = [u.unit for u in self.uv_layers]
+                       layer.unit = next(i for i in itertools.count() if i not in used_units)
+                       self.uv_layers.sort(key=lambda u: u.unit)
+
+               layer.uvs = [(0.0, 0.0)]*len(self.loops)
+               for f in self.faces:
+                       uv = material_map.get_material_uv(self.materials[f.material_index])
+                       for i in f.loop_indices:
+                               layer.uvs[i] = uv
+
        def prepare_uv(self, progress):
                # Form a list of UV layers referenced by materials with the array atlas
                # property set
@@ -740,7 +764,7 @@ class Mesh:
                self._mesh = None
 
 
-def create_mesh_from_object(context, obj, progress):
+def create_mesh_from_object(context, obj, progress, *, material_map=None):
        if obj.type!="MESH":
                raise Exception("Object is not a mesh")
 
@@ -781,6 +805,9 @@ def create_mesh_from_object(context, obj, progress):
 
        mesh.name = obj.data.name
 
+       if material_map:
+               mesh.apply_material_map(material_map)
+
        progress.set_task("Triangulating", 0.2, 0.3)
        mesh.prepare_triangles(progress)
        progress.set_task("Smoothing", 0.3, 0.5)