]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/material.py
Allow tagging objects in a scene file for retrieval after loading
[libs/gl.git] / blender / io_mspgl / material.py
1 import os
2
3 class MaterialMap:
4         def __init__(self, materials):
5                 self.technique = materials[0].technique
6                 if self.technique:
7                         self.name = "material_map_"+os.path.splitext(self.technique)[0]
8                 else:
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")
17
18                 count = len(self.materials)
19                 size = 1
20                 while size*size*2<count:
21                         size *= 2
22                 if size*size>=count:
23                         self.size = (size, size)
24                 else:
25                         self.size = (size*2, size)
26
27                 from .util import get_colormap
28
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)
35
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])
41
42 def create_material_map(context, material):
43         if not material.material_map:
44                 raise Exception("Material is not part of a material map")
45
46         tech = material.technique
47         materials = []
48         for m in context.blend_data.materials:
49                 if m.material_map and m.technique==tech:
50                         materials.append(m)
51
52         mat_map = MaterialMap(materials)
53
54         return mat_map