]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_material.py
Fixes and improvements to exporting materials
[libs/gl.git] / blender / io_mspgl / export_material.py
1 import os
2
3 class MaterialExporter:
4         def __init__(self):
5                 self.single_file = True
6                 self.use_textures = True
7
8         def create_texture_exporter(self):
9                 from .export_texture import TextureExporter
10                 texture_export = TextureExporter()
11                 texture_export.inline_data = self.single_file
12                 return texture_export
13
14         def export_technique_resources(self, material, resources):
15                 texture_export = self.create_texture_exporter()
16
17                 mat_name = material.name+".mat"
18                 if mat_name not in resources:
19                         resources[mat_name] = self.export_material(material)
20
21                 if self.use_textures:
22                         for s in material.texture_slots:
23                                 if s and s.texture.type=='IMAGE' and s.texture.image:
24                                         tex_name = s.texture.name+".tex2d"
25                                         if tex_name not in resources:
26                                                 resources[tex_name] = texture_export.export_texture(s.texture)
27
28         def export_technique(self, material, *, resources):
29                 from .datafile import Resource, Statement
30                 tech_res = Resource(material.name+".tech")
31
32                 mat_res = resources[material.name+".mat"]
33                 textures = {}
34                 if self.use_textures:
35                         image_texture_slots = [s for s in material.texture_slots if s and s.texture.type=='IMAGE' and s.texture.image]
36                         for s in image_texture_slots:
37                                 if s.use_map_color_diffuse:
38                                         textures["diffuse_map"] = s.texture
39                                 elif s.use_map_normal:
40                                         textures["normal_map"] = s.texture
41
42                 if material.technique:
43                         if not material.inherit_tech:
44                                 return tech_res
45
46                         if self.single_file:
47                                 raise Exception("Can't export inherited technique to a single file")
48
49                         st = Statement("inherit", material.technique)
50                         for s, t in textures.items():
51                                 if t.default_filter:
52                                         st.sub.append(Statement("texture", s, image_name(t.image)))
53                                 else:
54                                         st.sub.append(tech_res.create_reference_statement("texture", s, resources[t.name+".tex2d"]))
55                         if material.override_material:
56                                 st.sub.append(tech_res.create_reference_statement("material", "surface", mat_res))
57                         tech_res.statements.append(st)
58                 else:
59                         st = Statement("pass", "")
60                         if self.single_file:
61                                 st.sub.append(tech_res.create_embed_statement("material", mat_res))
62                         else:
63                                 st.sub.append(tech_res.create_reference_statement("material", mat_res))
64
65                         if "diffuse_map" in textures:
66                                 diffuse_tex = textures["diffuse_map"]
67                                 tex_res = resources[diffuse_tex.name+".tex2d"]
68                                 ss = Statement("texunit", 0)
69                                 if self.single_file:
70                                         ss.sub.append(tech_res.create_embed_statement("texture2d", tex_res))
71                                 elif not diffuse_tex.default_filter:
72                                         ss.sub.append(tech_res.create_reference_statement("texture", tex_res))
73                                 else:
74                                         ss.sub.append(Statement("texture", image_name(diffuse_tex.image)))
75                                 st.sub.append(ss)
76
77                         tech_res.statements.append(st)
78
79                 return tech_res
80
81         def export_material(self, material):
82                 from .datafile import Resource, Statement
83                 mat_res = Resource(material.name+".mat")
84                 statements = mat_res.statements
85
86                 from .util import get_colormap
87                 cm = get_colormap(material.srgb_colors)
88
89                 if any(s.use_map_color_diffuse for s in material.texture_slots if s):
90                         statements.append(Statement("diffuse", 1.0, 1.0, 1.0, 1.0))
91                         amb = cm(material.ambient)
92                         statements.append(Statement("ambient", amb, amb, amb, 1.0))
93                 else:
94                         diff = material.diffuse_color*material.diffuse_intensity
95                         statements.append(Statement("diffuse", cm(diff.r), cm(diff.g), cm(diff.b), 1.0))
96                         amb = diff*material.ambient
97                         statements.append(Statement("ambient", cm(amb.r), cm(amb.g), cm(amb.b), 1.0))
98                 spec = material.specular_color*material.specular_intensity
99                 statements.append(Statement("specular", cm(spec.r), cm(spec.g), cm(spec.g), 1.0))
100                 statements.append(Statement("shininess", material.specular_hardness))
101
102                 return mat_res
103
104
105 class MaterialMapExporter:
106         def __init__(self):
107                 self.single_file = True
108
109         def export_technique_resources(self, material_map, resources):
110                 from .datafile import Resource, Statement, Token
111                 diffuse_name = material_map.name+"_diffuse.tex2d"
112                 if diffuse_name not in resources:
113                         diffuse_res = Resource(diffuse_name)
114
115                         fmt = 'SRGB_ALPHA' if material_map.srgb_colors else 'RGBA'
116
117                         diffuse_res.statements.append(Statement("min_filter", Token('NEAREST')))
118                         diffuse_res.statements.append(Statement("mag_filter", Token('NEAREST')))
119                         diffuse_res.statements.append(Statement("storage", Token(fmt), *material_map.size))
120                         diffuse_res.statements.append(Statement("raw_data", material_map.diffuse_data))
121
122                         resources[diffuse_name] = diffuse_res
123
124                 if "basic_white.mat" not in resources:
125                         mat_res = Resource("basic_white.mat")
126                         mat_res.statements.append(Statement("diffuse", 1.0, 1.0, 1.0, 1.0))
127
128                         resources["basic_white.mat"] = mat_res
129
130         def export_technique(self, material_map, *, resources=None):
131                 from .datafile import Resource, Statement
132                 tech_res = Resource(material_map.name+".tech")
133
134                 mat_res = resources["basic_white.mat"]
135                 diffuse_res = resources[material_map.name+"_diffuse.tex2d"]
136
137                 if material_map.technique:
138                         if self.single_file:
139                                 raise Exception("Can't export inherited technique to a single file")
140
141                         st = Statement("inherit", material_map.technique)
142                         st.sub.append(tech_res.create_reference_statement("texture", "diffuse_map", diffuse_res))
143                         st.sub.append(tech_res.create_reference_statement("material", "surface", mat_res))
144                         tech_res.statements.append(st)
145                 else:
146                         st = Statement("pass", "")
147                         if self.single_file:
148                                 st.sub.append(tech_res.create_embed_statement("material", mat_res))
149                         else:
150                                 st.sub.append(tech_res.create_reference_statement("material", mat_res))
151                         ss = Statement("texunit", 0)
152                         if self.single_file:
153                                 ss.sub.append(tech_res.create_embed_statement("texture2d", diffuse_res))
154                         else:
155                                 ss.sub.append(tech_res.create_reference_statement("texture", diffuse_res))
156                         st.sub.append(ss)
157                         tech_res.statements.append(st)
158
159                 return tech_res