]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_material.py
Refactor material property handling in the Blender exporter
[libs/gl.git] / blender / io_mspgl / export_material.py
1 import os
2
3 def create_technique_resource(material, resources):
4         from .datafile import Resource, Statement
5         tech_res = Resource(material.name+".tech", "technique")
6
7         mat_res = resources[material.name+".mat"]
8
9         st = Statement("pass", "")
10         st.sub.append(tech_res.create_embed_statement("material", mat_res))
11
12         if material.render_mode=='CUSTOM':
13                 st.sub.append(Statement("shader", material.shader))
14
15         tech_res.statements.append(st)
16
17         return tech_res
18
19 class MaterialExporter:
20         def __init__(self):
21                 self.use_textures = True
22                 self.inline_texture_data = False
23
24         def create_texture_exporter(self):
25                 from .export_texture import TextureExporter
26                 texture_export = TextureExporter()
27                 texture_export.inline_data = self.inline_texture_data
28                 return texture_export
29
30         def export_technique_resources(self, material, resources):
31                 from .export_texture import SamplerExporter
32                 texture_export = self.create_texture_exporter()
33                 sampler_export = SamplerExporter()
34
35                 from .material import Material
36                 material = Material(material)
37
38                 if self.use_textures:
39                         for p in material.properties.values():
40                                 if p.texture:
41                                         tex_name = p.texture.image.name+".tex2d"
42                                         if tex_name not in resources:
43                                                 resources[tex_name] = texture_export.export_texture(p.texture, p.tex_usage)
44
45                                         samp_name = sampler_export.get_sampler_name(p.texture)
46                                         if samp_name not in resources:
47                                                 resources[samp_name] = sampler_export.export_sampler(p.texture)
48
49                 mat_name = material.name+".mat"
50                 if mat_name not in resources:
51                         resources[mat_name] = self.export_material(material, resources=resources)
52
53         def export_technique(self, material, *, resources):
54                 return create_technique_resource(material, resources)
55
56         def export_material(self, material, *, resources):
57                 from .datafile import Resource, Statement
58                 mat_res = Resource(material.name+".mat", "material")
59
60                 st = Statement("pbr")
61                 for kw, p in material.properties.items():
62                         ss = self.create_property_statement(mat_res, p, kw, resources)
63                         if ss:
64                                 st.sub.append(ss)
65                 if self.use_textures:
66                         first_tex = (p.texture for p in material.properties.values() if p.texture).__next__()
67                         if first_tex and not first_tex.default_filter:
68                                 from .export_texture import SamplerExporter
69                                 sampler_export = SamplerExporter()
70                                 st.sub.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(first_tex)]))
71                 mat_res.statements.append(st)
72
73                 return mat_res
74
75         def create_property_statement(self, mat_res, prop, keyword, resources):
76                 from .datafile import Statement
77                 if self.use_textures and prop.texture:
78                         tex_res = resources[prop.texture.image.name+".tex2d"]
79                         fn = os.path.basename(prop.texture.image.filepath)
80                         if prop.texture.default_filter and fn:
81                                 return Statement(keyword+"_map", fn)
82                         else:
83                                 return mat_res.create_reference_statement(keyword+"_map", tex_res)
84                 elif prop.value is None:
85                         return
86                 elif type(prop.value)==tuple:
87                         return Statement(keyword, *prop.value)
88                 else:
89                         return Statement(keyword, prop.value)
90
91
92 class MaterialMapExporter:
93         def __init__(self):
94                 pass
95
96         def export_technique_resources(self, material_map, resources):
97                 from .datafile import Resource, Statement, Token
98                 base_color_name = material_map.name+"_base_color.tex2d"
99                 base_color_res = resources.get(base_color_name)
100                 if not base_color_res:
101                         base_color_res = Resource(base_color_name, "texture2d")
102
103                         base_color_res.statements.append(Statement("storage", Token('SRGB_ALPHA'), *material_map.size))
104                         base_color_res.statements.append(Statement("raw_data", material_map.base_color_data))
105
106                         resources[base_color_name] = base_color_res
107
108                 sampler_name = "nearest.samp"
109                 sampler_res = resources.get(sampler_name)
110                 if not sampler_res:
111                         sampler_res = Resource(sampler_name, "sampler")
112
113                         sampler_res.statements.append(Statement("filter", Token('NEAREST')))
114
115                         resources[sampler_name] = sampler_res
116
117                 mat_name = material_map.name+".mat"
118                 if mat_name not in resources:
119                         mat_res = Resource(mat_name, "material")
120                         st = Statement("pbr")
121                         st.sub.append(mat_res.create_reference_statement("base_color_map", base_color_res))
122                         st.sub.append(mat_res.create_reference_statement("sampler", sampler_res))
123                         mat_res.statements.append(st)
124
125                         resources[mat_name] = mat_res
126
127         def export_technique(self, material_map, *, resources):
128                 return create_technique_resource(material_map, resources)