]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/material.py
Update the Blender exporter for version 2.80
[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.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")
18
19                 count = len(self.materials)
20                 size = 1
21                 while size*size*2<count:
22                         size *= 2
23                 if size*size>=count:
24                         self.size = (size, size)
25                 else:
26                         self.size = (size*2, size)
27
28                 from .util import get_colormap
29
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)
36
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])
42
43 def create_material_map(context, material):
44         if not material.material_map:
45                 raise Exception("Material is not part of a material map")
46
47         tech = material.technique
48         materials = []
49         for m in context.blend_data.materials:
50                 if m.material_map and m.technique==tech:
51                         materials.append(m)
52
53         mat_map = MaterialMap(materials)
54
55         return mat_map