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