]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_texture.py
Refactor writing files in the Blender exporter
[libs/gl.git] / blender / io_mspgl / export_texture.py
1 import os
2
3 class TextureExporter:
4         def __init__(self):
5                 self.inline_data = True
6
7         def export_texture(self, tex_node, usage='RGB'):
8                 image = tex_node.image
9                 from .datafile import Resource, Statement, Token
10                 tex_res = Resource(image.name+".tex2d", "texture2d")
11
12                 use_interpolation = tex_node.interpolation!='Closest'
13                 if use_interpolation:
14                         if tex_node.use_mipmap:
15                                 tex_res.statements.append(Statement("filter", Token('LINEAR_MIPMAP_LINEAR')))
16                                 tex_res.statements.append(Statement("generate_mipmap", True))
17                         else:
18                                 tex_res.statements.append(Statement("filter", Token('LINEAR')))
19                         tex_res.statements.append(Statement("max_anisotropy", tex_node.max_anisotropy))
20                 else:
21                         if tex_node.use_mipmap:
22                                 tex_res.statements.append(Statement("filter", Token('NEAREST_MIPMAP_NEAREST')))
23                                 tex_res.statements.append(Statement("generate_mipmap", True))
24                         else:
25                                 tex_res.statements.append(Statement("filter", Token('NEAREST')))
26
27                 colorspace = image.colorspace_settings.name
28                 if usage=='RGBA':
29                         fmt = 'SRGB_ALPHA' if colorspace=='sRGB' else 'RGBA'
30                 elif usage=='GRAY':
31                         if colorspace=='sRGB':
32                                 raise Exception("Grayscale textures with sRGB colorspace are not supported")
33                         fmt = 'LUMINANCE'
34                 else:
35                         fmt = 'SRGB' if colorspace=='sRGB' else 'RGB'
36
37                 tex_res.statements.append(Statement("storage", Token(fmt), image.size[0], image.size[1]))
38
39                 fn = os.path.basename(image.filepath)
40                 if not self.inline_data and fn:
41                         tex_res.statements.append(Statement("external_image", fn))
42                 else:
43                         texdata = ""
44                         if usage=='RGBA':
45                                 for p in image.pixels:
46                                         texdata += "\\x{:02X}".format(int(p*255))
47                         elif usage=='GRAY':
48                                 for i in range(0, len(image.pixels), 4):
49                                         texdata += "\\x{:02X}".format(image.pixels[i])
50                         else:
51                                 for i in range(0, len(image.pixels), 4):
52                                         for j in range(3):
53                                                 texdata += "\\x{:02X}".format(int(image.pixels[i+j]*255))
54                         tex_res.statements.append(Statement("raw_data", texdata))
55
56                 return tex_res