]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_texture.py
9f520cbd7dea5edac5c8cdb690cc8cedd037d9bb
[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, texture):
8                 from .datafile import Resource, Statement, Token
9                 tex_res = Resource(texture.name+".tex2d")
10
11                 if texture.use_interpolation:
12                         if texture.use_mipmap:
13                                 tex_res.statements.append(Statement("filter", Token('LINEAR_MIPMAP_LINEAR')))
14                                 tex_res.statements.append(Statement("generate_mipmap", True))
15                         else:
16                                 tex_res.statements.append(Statement("filter", Token('LINEAR')))
17                         tex_res.statements.append(Statement("max_anisotropy", texture.filter_eccentricity))
18                 else:
19                         if texture.use_mipmap:
20                                 tex_res.statements.append(Statement("filter", Token('NEAREST_MIPMAP_NEAREST')))
21                                 tex_res.statements.append(Statement("generate_mipmap", True))
22                         else:
23                                 tex_res.statements.append(Statement("filter", Token('NEAREST')))
24
25                 if not self.inline_data and texture.image.filepath:
26                         tex_res.statements.append(Statement("external_image", os.path.basename(texture.image.filepath)))
27                 else:
28                         texdata = ""
29                         colorspace = texture.image.colorspace_settings.name
30                         if texture.use_alpha:
31                                 fmt = 'SRGB_ALPHA' if colorspace=='sRGB' else 'RGBA'
32                                 for p in texture.image.pixels:
33                                         texdata += "\\x{:02X}".format(int(p*255))
34                         else:
35                                 fmt = 'SRGB' if colorspace=='sRGB' else 'RGB'
36                                 for i in range(0, len(texture.image.pixels), 4):
37                                         for j in range(3):
38                                                 texdata += "\\x{:02X}".format(int(texture.image.pixels[i+j]*255))
39                         tex_res.statements.append(Statement("storage", Token(fmt), texture.image.size[0], texture.image.size[1]))
40                         tex_res.statements.append(Statement("raw_data", texdata))
41
42                 return tex_res