--- /dev/null
+class MaterialMap:
+ def __init__(self, materials):
+ self.technique = materials[0].technique
+ self.name = "material_map_"+self.technique
+ self.materials = materials
+ self.srgb_colors = materials[0].srgb_colors
+ for m in self.materials:
+ if m.technique!=self.technique:
+ raise Exception("Conflicting techniques in MaterialMap constructor")
+ if m.srgb_colors!=self.srgb_colors:
+ raise Exception("Conflicting colorspace settings in MaterialMap constructor")
+
+ count = len(self.materials)
+ size = 1
+ while size*size*2<count:
+ size *= 2
+ if size*size>=count:
+ self.size = (size, size)
+ else:
+ self.size = (size*2, size)
+
+ from .util import get_colormap
+
+ cm = get_colormap(self.srgb_colors)
+ self.diffuse_data = ""
+ for m in self.materials:
+ diff = [int(cm(c)*255) for c in m.diffuse_color*m.diffuse_intensity]
+ self.diffuse_data += "\\x{:02X}\\x{:02X}\\x{:02X}\\x{:02X}".format(*diff, int(m.ambient*255))
+ self.diffuse_data += "\\x00\\x00\\x00\\x00"*(self.size[0]*self.size[1]-count)
+
+ def get_material_uv(self, material):
+ index = self.materials.index(material)
+ x = index%self.size[0]
+ y = index//self.size[0]
+ return ((x+0.5)/self.size[0], (y+0.5)/self.size[1])
+
+def create_material_map(context, material):
+ if not material.material_map:
+ raise Exception("Material is not part of a material map")
+
+ tech = material.technique
+ materials = []
+ for m in context.blend_data.materials:
+ if m.material_map and m.technique==tech:
+ materials.append(m)
+
+ mat_map = MaterialMap(materials)
+
+ return mat_map