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.srgb_colors = materials[0].srgb_colors
12 for m in self.materials:
13 if m.technique!=self.technique:
14 raise Exception("Conflicting techniques in MaterialMap constructor")
15 if m.srgb_colors!=self.srgb_colors:
16 raise Exception("Conflicting colorspace settings in MaterialMap constructor")
18 count = len(self.materials)
20 while size*size*2<count:
23 self.size = (size, size)
25 self.size = (size*2, size)
27 from .util import get_colormap
29 cm = get_colormap(self.srgb_colors)
30 self.diffuse_data = ""
31 for m in self.materials:
32 diff = [int(cm(c)*255) for c in m.diffuse_color*m.diffuse_intensity]
33 self.diffuse_data += "\\x{:02X}\\x{:02X}\\x{:02X}\\x{:02X}".format(*diff, int(m.ambient*255))
34 self.diffuse_data += "\\x00\\x00\\x00\\x00"*(self.size[0]*self.size[1]-count)
36 def get_material_uv(self, material):
37 index = self.materials.index(material)
38 x = index%self.size[0]
39 y = index//self.size[0]
40 return ((x+0.5)/self.size[0], (y+0.5)/self.size[1])
42 def create_material_map(context, material):
43 if not material.material_map:
44 raise Exception("Material is not part of a material map")
46 tech = material.technique
48 for m in context.blend_data.materials:
49 if m.material_map and m.technique==tech:
52 mat_map = MaterialMap(materials)