+ def apply_material_map(self, material_map):
+ for m in self.materials:
+ if m.name not in material_map.material_names:
+ 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
+