]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_object.py
Remove the compound property from exporter
[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, obj=None, progress=None):
42                 if obj is None:
43                         obj = context.active_object
44
45                 lods = [obj]
46                 for c in obj.children:
47                         if c.lod_for_parent:
48                                 if c.lod_index>=len(lods):
49                                         lods += [None]*(c.lod_index+1-len(lods))
50                                 lods[c.lod_index] = c
51
52                 from .outfile import open_output
53                 out_file = open_output(out_file)
54
55                 p1 = max(((v.co, v.co.length) for v in obj.data.vertices), key=lambda x:x[1])[0]
56                 p2 = max(((v.co, (v.co-p1).length) for v in obj.data.vertices), key=lambda x:x[1])[0]
57                 center = (p1+p2)/2
58                 radius = (p1-p2).length/2
59                 for v in obj.data.vertices:
60                         d = v.co-center
61                         if d.length>radius:
62                                 center += d*(1-radius/d.length)/2
63                                 radius = (radius+d.length)/2
64
65                 out_file.write("bounding_sphere_hint", center[0], center[1], center[2], radius)
66
67                 prev_mesh = None
68                 prev_tech = (None, None)
69                 for i, l in enumerate(lods):
70                         if i>0:
71                                 out_file.begin("level_of_detail", i)
72
73                         if i==0 or l.data.name!=prev_mesh:
74                                 mesh = self.export_object_mesh(context, out_file, l, 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, obj, 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 obj.lod_for_parent:
100                         lod_index = obj.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 = obj.data.name+".mesh"
107                         mesh_out = open_output(os.path.join(path, name))
108                         mesh = mesh_export.export(context, mesh_out, obj, progress)
109                         out_file.write("mesh", '"{}"'.format(name))
110                 else:
111                         out_file.begin("mesh")
112                         mesh = mesh_export.export(context, out_file, obj, progress)
113                         out_file.end()
114
115                 return mesh
116
117         def export_object_technique(self, obj, mesh, out_file, lod_index):
118                 material = None
119                 if obj.material_slots:
120                         material = obj.material_slots[0].material
121
122                 from .outfile import open_output
123                 path, name = external_name(out_file, ".tech", lod_index)
124
125                 if self.external_tech and obj.technique:
126                         if obj.inherit_tech and material and (obj.override_material or material.texture_slots):
127                                 out_file.begin("technique")
128                                 out_file.begin("inherit", '"{}"'.format(obj.technique))
129                                 for slot in material.texture_slots:
130                                         if slot and slot.texture.type=="IMAGE":
131                                                 name = image_name(slot.texture.image)
132                                                 if slot.use_map_color_diffuse:
133                                                         out_file.write("texture", '"diffuse_map"', '"{}"'.format(name))
134                                                 elif slot.use_map_normal:
135                                                         out_file.write("texture", '"normal_map"', '"{}"'.format(name))
136                                 if obj.override_material:
137                                         mat_name = material.name+".mat"
138                                         mat_out = open_output(os.path.join(path, mat_name))
139                                         self.export_material(material, mat_out)
140                                         out_file.write("material", '"surface"', '"{}"'.format(mat_name))
141                                 out_file.end()
142                                 out_file.end()
143                         else:
144                                 out_file.write("technique", '"{}"'.format(obj.technique))
145                 elif self.separate_tech:
146                         if self.shared_tech and material:
147                                 name = material.name+".tech"
148                         tech_out = open_output(os.path.join(path, name))
149                         self.export_technique_definition(material, mesh, tech_out)
150                         out_file.write("technique", '"{}"'.format(name))
151                 else:
152                         out_file.begin("technique")
153                         self.export_technique_definition(material, obj.material_tex, mesh, out_file)
154                         out_file.end()
155
156         def export_technique_definition(self, material, material_tex, mesh, out_file):
157                 out_file.begin("pass", '""')
158                 if material:
159                         if material_tex:
160                                 out_file.begin("material")
161                                 out_file.write("diffuse", 1.0, 1.0, 1.0, 1.0)
162                                 out_file.end()
163                                 index = 0
164                                 for u in mesh.uv_layers:
165                                         if u.name=="material_tex":
166                                                 index = u.unit
167                                 out_file.begin("texunit", index)
168                                 out_file.begin("texture2d")
169                                 out_file.write("min_filter", "NEAREST")
170                                 out_file.write("mag_filter", "NEAREST")
171                                 out_file.write("storage", "RGB", len(mesh.materials), 1)
172                                 texdata = '"'
173                                 for m in mesh.materials:
174                                         cm = get_colormap(m.srgb_colors)
175                                         color = [int(cm(c)*255) for c in m.diffuse_color*m.diffuse_intensity]
176                                         texdata += "\\x%02X\\x%02X\\x%02X"%tuple(color)
177                                 texdata += '"'
178                                 out_file.write("raw_data", texdata)
179                                 out_file.end()
180                                 out_file.end()
181                         else:
182                                 out_file.begin("material")
183                                 self.export_material(material, out_file)
184                                 out_file.end()
185
186                         if self.textures!="NONE":
187                                 for slot in material.texture_slots:
188                                         if not slot:
189                                                 continue
190
191                                         tex = slot.texture
192                                         if tex.type!="IMAGE":
193                                                 continue
194
195                                         if slot.uv_layer:
196                                                 for u in mesh.uv_layers:
197                                                         if u.name==slot.uv_layer:
198                                                                 index = u.unit
199                                         else:
200                                                 index = mesh.uv_layers[0].unit
201
202                                         out_file.begin("texunit", index)
203                                         if self.textures=="INLINE":
204                                                 out_file.begin("texture2d")
205                                                 out_file.write("min_filter", "LINEAR")
206                                                 out_file.write("storage", "RGBA", tex.image.size[0], tex.image.size[1])
207                                                 texdata = '"'
208                                                 for p in tex.image.pixels:
209                                                         texdata += "\\x%02X"%int(p*255)
210                                                 texdata += '"'
211                                                 out_file.write("raw_data", texdata)
212                                                 out_file.end()
213                                         else:
214                                                 out_file.write("texture", '"%s"'%image_name(tex.image))
215                                         out_file.end()
216
217                 out_file.end()
218
219         def export_material(self, mat, out_file):
220                 cm = get_colormap(mat.srgb_colors)
221                 if any((s and s.use_map_color_diffuse) for s in mat.texture_slots):
222                         out_file.write("diffuse", 1.0, 1.0, 1.0, 1.0)
223                         amb = cm(mat.ambient)
224                         out_file.write("ambient", amb, amb, amb, 1.0)
225                 else:
226                         diff = mat.diffuse_color*mat.diffuse_intensity
227                         out_file.write("diffuse", cm(diff.r), cm(diff.g), cm(diff.b), 1.0)
228                         amb = diff*mat.ambient
229                         out_file.write("ambient", cm(amb.r), cm(amb.g), cm(amb.b), 1.0)
230                 spec = mat.specular_color*mat.specular_intensity
231                 out_file.write("specular", spec.r, spec.g, spec.b, 1.0)
232                 out_file.write("shininess", mat.specular_hardness);