From 7b6efe858ba6e649b148c1eb7b2680eed30ee75b Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 24 May 2019 00:06:05 +0300 Subject: [PATCH] Add a missing file It was supposed to be in bde3ec8. --- blender/io_mspgl/material.py | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 blender/io_mspgl/material.py diff --git a/blender/io_mspgl/material.py b/blender/io_mspgl/material.py new file mode 100644 index 00000000..54b2c5d8 --- /dev/null +++ b/blender/io_mspgl/material.py @@ -0,0 +1,49 @@ +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: + 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 -- 2.45.2