]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_object.py
b910add12fad3ed86ef397861de92fc8bb6a50b9
[libs/gl.git] / blender / io_mspgl / export_object.py
1 import os
2
3 def linear_to_srgb(l):
4         if l<0.0031308:
5                 return 12.92*l
6         else:
7                 return 1.055*(l**(1/2.4))-0.055
8
9 def image_name(i):
10         fp = i.filepath
11         if fp:
12                 return os.path.split(fp)[1]
13         else:
14                 return i.name
15
16 def external_name(out_file, ext, index):
17         path, base = os.path.split(out_file.filename)
18         base = os.path.splitext(base)[0]
19         if index>0:
20                 base += "_lod{}".format(index)
21         return path, base+ext
22
23
24 class ObjectExporter:
25         def __init__(self):
26                 self.material_tex = False
27                 self.srgb_colors = True
28                 self.textures = "REF"
29                 self.separate_mesh = False
30                 self.separate_tech = False
31                 self.external_tech = True
32                 self.shared_tech = True
33                 self.export_lods = True
34
35         def export(self, context, out_file, objs=None, progress=None):
36                 if objs is None:
37                         obj = context.active_object
38                 else:
39                         obj = objs[0]
40
41                 lods = [obj]
42                 for c in obj.children:
43                         if c.lod_for_parent:
44                                 if c.lod_index>=len(lods):
45                                         lods += [None]*(c.lod_index+1-len(lods))
46                                 lods[c.lod_index] = c
47
48                 from .outfile import open_output
49                 out_file = open_output(out_file)
50
51                 prev_mesh = None
52                 prev_tech = (None, None)
53                 for i, l in enumerate(lods):
54                         if i>0:
55                                 out_file.begin("level_of_detail", i)
56                                 objs = [l]
57
58                         same_mesh = (l.data.name==prev_mesh)
59                         if i==0 or not same_mesh:
60                                 mesh = self.export_object_mesh(context, out_file, i, objs, progress)
61                                 prev_mesh = l.data.name
62
63                         same_tech = True
64                         mat = None
65                         if l.material_slots and l.material_slots[0].material:
66                                 mat = l.material_slots[0].material.name
67                                 if mat!=prev_tech[1]:
68                                         same_tech = False
69                         if self.external_tech and l.technique!=prev_tech[0]:
70                                 same_tech = False
71                         if i==0 or not same_tech:
72                                 self.export_object_technique(l, mesh, out_file, i)
73                                 prev_tech = (l.technique, mat)
74
75                         if i>0:
76                                 out_file.end()
77
78         def export_object_mesh(self, context, out_file, lod_index, objs, progress):
79                 from .export_mesh import MeshExporter
80                 mesh_export = MeshExporter()
81                 for k, v in self.__dict__.items():
82                         setattr(mesh_export, k, v)
83
84                 if self.separate_mesh:
85                         from .outfile import open_output
86                         path, name = external_name(out_file, ".mesh", lod_index)
87                         mesh_out = open_output(os.path.join(path, name))
88                         mesh = mesh_export.export(context, mesh_out, objs, progress)
89                         out_file.write("mesh", '"{}"'.format(name))
90                 else:
91                         out_file.begin("mesh")
92                         mesh = mesh_export.export(context, out_file, objs, progress)
93                         out_file.end()
94
95                 return mesh
96
97         def export_object_technique(self, obj, mesh, out_file, lod_index):
98                 if self.srgb_colors:
99                         self.colormap = linear_to_srgb
100                 else:
101                         self.colormap = lambda x: x
102
103                 material = None
104                 if obj.material_slots:
105                         material = obj.material_slots[0].material
106
107                 from .outfile import open_output
108                 path, name = external_name(out_file, ".tech", lod_index)
109
110                 if self.external_tech and obj.technique:
111                         if obj.inherit_tech and material and (obj.override_material or material.texture_slots):
112                                 out_file.begin("technique")
113                                 out_file.begin("inherit", '"{}"'.format(obj.technique))
114                                 for slot in material.texture_slots:
115                                         if slot and slot.texture.type=="IMAGE":
116                                                 name = image_name(slot.texture.image)
117                                                 if slot.use_map_color_diffuse:
118                                                         out_file.write("texture", '"diffuse_map"', '"{}"'.format(name))
119                                                 elif slot.use_map_normal:
120                                                         out_file.write("texture", '"normal_map"', '"{}"'.format(name))
121                                 if obj.override_material:
122                                         mat_name = material.name+".mat"
123                                         mat_out = open_output(os.path.join(path, mat_name))
124                                         self.export_material(material, mat_out)
125                                         out_file.write("material", '""', '"{}"'.format(mat_name))
126                                 out_file.end()
127                                 out_file.end()
128                         else:
129                                 out_file.write("technique", '"{}"'.format(obj.technique))
130                 elif self.separate_tech:
131                         if self.shared_tech and material:
132                                 name = material.name+".tech"
133                         tech_out = open_output(os.path.join(path, name))
134                         self.export_technique_definition(material, mesh, tech_out)
135                         out_file.write("technique", '"{}"'.format(name))
136                 else:
137                         out_file.begin("technique")
138                         self.export_technique_definition(material, mesh, out_file)
139                         out_file.end()
140
141         def export_technique_definition(self, material, mesh, out_file):
142                 out_file.begin("pass", '""')
143                 if material:
144                         cm = self.colormap
145
146                         if self.material_tex:
147                                 out_file.begin("material")
148                                 out_file.write("diffuse", 1.0, 1.0, 1.0, 1.0)
149                                 out_file.end()
150                                 index = 0
151                                 for u in mesh.uv_layers:
152                                         if u.name=="material_tex":
153                                                 index = u.unit
154                                 out_file.begin("texunit", index)
155                                 out_file.begin("texture2d")
156                                 out_file.write("min_filter", "NEAREST")
157                                 out_file.write("mag_filter", "NEAREST")
158                                 out_file.write("storage", "RGB", len(mesh.materials), 1)
159                                 texdata = '"'
160                                 for m in mesh.materials:
161                                         color = [int(cm(c)*255) for c in m.diffuse_color*mat.diffuse_intensity]
162                                         texdata += "\\x%02X\\x%02X\\x%02X"%tuple(color)
163                                 texdata += '"'
164                                 out_file.write("raw_data", texdata)
165                                 out_file.end()
166                                 out_file.end()
167                         else:
168                                 out_file.begin("material")
169                                 self.export_material(material, out_file)
170                                 out_file.end()
171
172                         if self.textures!="NONE":
173                                 for slot in material.texture_slots:
174                                         if not slot:
175                                                 continue
176
177                                         tex = slot.texture
178                                         if tex.type!="IMAGE":
179                                                 continue
180
181                                         if slot.uv_layer:
182                                                 for u in mesh.uv_layers:
183                                                         if u.name==slot.uv_layer:
184                                                                 index = u.unit
185                                         else:
186                                                 index = mesh.uv_layers[0].unit
187
188                                         out_file.begin("texunit", index)
189                                         if self.textures=="INLINE":
190                                                 out_file.begin("texture2d")
191                                                 out_file.write("min_filter", "LINEAR")
192                                                 out_file.write("storage", "RGBA", tex.image.size[0], tex.image.size[1])
193                                                 texdata = '"'
194                                                 for p in tex.image.pixels:
195                                                         texdata += "\\x%02X"%int(p*255)
196                                                 texdata += '"'
197                                                 out_file.write("raw_data", texdata)
198                                                 out_file.end()
199                                         else:
200                                                 out_file.write("texture", '"%s"'%image_name(tex.image))
201                                         out_file.end()
202
203                 out_file.end()
204
205         def export_material(self, mat, out_file):
206                 cm = self.colormap
207                 if any((s and s.use_map_color_diffuse) for s in mat.texture_slots):
208                         out_file.write("diffuse", 1.0, 1.0, 1.0, 1.0)
209                         amb = cm(mat.ambient)
210                         out_file.write("ambient", amb, amb, amb, 1.0)
211                 else:
212                         diff = mat.diffuse_color*mat.diffuse_intensity
213                         out_file.write("diffuse", cm(diff.r), cm(diff.g), cm(diff.b), 1.0)
214                         amb = diff*mat.ambient
215                         out_file.write("ambient", cm(amb.r), cm(amb.g), cm(amb.b), 1.0)
216                 spec = mat.specular_color*mat.specular_intensity
217                 out_file.write("specular", spec.r, spec.g, spec.b, 1.0)
218                 out_file.write("shininess", mat.specular_hardness);