]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_texture.py
Add an intermediate node type for statements
[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                 fn = os.path.basename(texture.image.filepath)
26                 if not self.inline_data and fn:
27                         tex_res.statements.append(Statement("external_image", fn))
28                 else:
29                         texdata = ""
30                         colorspace = texture.image.colorspace_settings.name
31                         if texture.use_alpha:
32                                 fmt = 'SRGB_ALPHA' if colorspace=='sRGB' else 'RGBA'
33                                 for p in texture.image.pixels:
34                                         texdata += "\\x{:02X}".format(int(p*255))
35                         else:
36                                 fmt = 'SRGB' if colorspace=='sRGB' else 'RGB'
37                                 for i in range(0, len(texture.image.pixels), 4):
38                                         for j in range(3):
39                                                 texdata += "\\x{:02X}".format(int(texture.image.pixels[i+j]*255))
40                         tex_res.statements.append(Statement("storage", Token(fmt), texture.image.size[0], texture.image.size[1]))
41                         tex_res.statements.append(Statement("raw_data", texdata))
42
43                 return tex_res