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