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