4 def __init__(self, materials):
5 self.technique = materials[0].technique
7 self.name = "material_map_"+os.path.splitext(self.technique)[0]
9 self.name = "material_map"
10 self.materials = materials
11 self.material_names = [m.name for m in self.materials]
12 self.srgb_colors = materials[0].srgb_colors
13 for m in self.materials:
14 if m.technique!=self.technique:
15 raise Exception("Conflicting techniques in MaterialMap constructor")
16 if m.srgb_colors!=self.srgb_colors:
17 raise Exception("Conflicting colorspace settings in MaterialMap constructor")
19 count = len(self.materials)
21 while size*size*2<count:
24 self.size = (size, size)
26 self.size = (size*2, size)
28 from .util import get_colormap
30 cm = get_colormap(self.srgb_colors)
31 self.diffuse_data = ""
32 for m in self.materials:
33 diff = [int(cm(c)*255) for c in m.diffuse_color]
34 self.diffuse_data += "\\x{:02X}\\x{:02X}\\x{:02X}\\xFF".format(*diff)
35 self.diffuse_data += "\\x00\\x00\\x00\\x00"*(self.size[0]*self.size[1]-count)
37 def get_material_uv(self, material):
38 index = self.material_names.index(material.name)
39 x = index%self.size[0]
40 y = index//self.size[0]
41 return ((x+0.5)/self.size[0], (y+0.5)/self.size[1])
43 def create_material_map(context, material):
44 if not material.material_map:
45 raise Exception("Material is not part of a material map")
47 tech = material.technique
49 for m in context.blend_data.materials:
50 if m.material_map and m.technique==tech:
53 mat_map = MaterialMap(materials)