]> 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         if mat_res:
11                 st.sub.append(tech_res.create_embed_statement("material", mat_res))
12
13         if material.render_mode=='CUSTOM':
14                 shader = material.shader
15                 if shader.endswith(".glsl"):
16                         shader += ".shader"
17                 st.sub.append(Statement("shader", shader))
18
19         tech_res.statements.append(st)
20
21         return tech_res
22
23 class MaterialExporter:
24         def __init__(self):
25                 self.use_textures = True
26                 self.inline_texture_data = False
27
28         def create_texture_exporter(self):
29                 from .export_texture import TextureExporter
30                 texture_export = TextureExporter()
31                 texture_export.inline_data = self.inline_texture_data
32                 return texture_export
33
34         def export_technique_resources(self, material, resources):
35                 from .export_texture import SamplerExporter
36                 texture_export = self.create_texture_exporter()
37                 sampler_export = SamplerExporter()
38
39                 from .material import Material
40                 material = Material(material)
41
42                 if self.use_textures:
43                         for p in material.properties:
44                                 if p.texture:
45                                         tex_name = p.texture.image.name+".tex2d"
46                                         if tex_name not in resources:
47                                                 resources[tex_name] = texture_export.export_texture(p.texture, p.tex_usage)
48
49                                         samp_name = sampler_export.get_sampler_name(p.texture)
50                                         if samp_name not in resources:
51                                                 resources[samp_name] = sampler_export.export_sampler(p.texture)
52
53                 mat_name = material.name+".mat"
54                 if mat_name not in resources:
55                         if material.type:
56                                 resources[mat_name] = self.export_material(material, resources=resources)
57                         else:
58                                 resources[mat_name] = None
59
60         def export_technique(self, material, *, resources):
61                 return create_technique_resource(material, resources)
62
63         def export_material(self, material, *, resources):
64                 from .datafile import Resource, Statement
65                 mat_res = Resource(material.name+".mat", "material")
66
67                 if material.type!="pbr":
68                         raise Exception("Can't export unknown material type "+material.type)
69
70                 st = Statement(material.type)
71                 for p in material.properties:
72                         ss = self.create_property_statement(mat_res, p, resources)
73                         if ss:
74                                 st.sub.append(ss)
75                 if self.use_textures:
76                         first_tex = (p.texture for p in material.properties if p.texture).__next__()
77                         if first_tex and not first_tex.default_filter:
78                                 from .export_texture import SamplerExporter
79                                 sampler_export = SamplerExporter()
80                                 st.sub.append(mat_res.create_reference_statement("sampler", resources[sampler_export.get_sampler_name(first_tex)]))
81                 mat_res.statements.append(st)
82
83                 return mat_res
84
85         def create_property_statement(self, mat_res, prop, resources):
86                 from .datafile import Statement
87                 if self.use_textures and prop.texture:
88                         tex_res = resources[prop.texture.image.name+".tex2d"]
89                         from .util import basename
90                         fn = basename(prop.texture.image.filepath)
91                         if prop.texture.default_filter and fn:
92                                 return Statement(prop.tex_keyword, fn)
93                         else:
94                                 return mat_res.create_reference_statement(prop.tex_keyword, tex_res)
95                 elif not prop.keyword:
96                         return
97                 elif type(prop.value)==tuple:
98                         return Statement(prop.keyword, *prop.value)
99                 else:
100                         return Statement(prop.keyword, prop.value)
101
102
103 class MaterialMapExporter:
104         def __init__(self):
105                 pass
106
107         def export_technique_resources(self, material_map, resources):
108                 from .datafile import Resource, Statement, Token
109                 base_color_name = material_map.name+"_base_color.tex2d"
110                 base_color_res = resources.get(base_color_name)
111                 if not base_color_res:
112                         base_color_res = Resource(base_color_name, "texture2d")
113
114                         base_color_res.statements.append(Statement("storage", Token('SRGB_ALPHA'), *material_map.size))
115                         base_color_res.statements.append(Statement("raw_data", material_map.base_color_data))
116
117                         resources[base_color_name] = base_color_res
118
119                 sampler_name = "nearest.samp"
120                 sampler_res = resources.get(sampler_name)
121                 if not sampler_res:
122                         sampler_res = Resource(sampler_name, "sampler")
123
124                         sampler_res.statements.append(Statement("filter", Token('NEAREST')))
125
126                         resources[sampler_name] = sampler_res
127
128                 mat_name = material_map.name+".mat"
129                 if mat_name not in resources:
130                         mat_res = Resource(mat_name, "material")
131                         st = Statement("pbr")
132                         st.sub.append(mat_res.create_reference_statement("base_color_map", base_color_res))
133                         st.sub.append(mat_res.create_reference_statement("sampler", sampler_res))
134                         mat_res.statements.append(st)
135
136                         resources[mat_name] = mat_res
137
138         def export_technique(self, material_map, *, resources):
139                 return create_technique_resource(material_map, resources)