]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_object.py
Combine shared_mesh and shared_tech into a single option
[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.separate_tech = False
36                 self.shared_resources = True
37                 self.export_lods = True
38
39         def export(self, context, out_file, obj=None, progress=None):
40                 if obj is None:
41                         obj = context.active_object
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
71                         if i==0 or l.data.name!=prev_mesh:
72                                 mesh = self.export_object_mesh(context, out_file, l, progress)
73                                 prev_mesh = l.data.name
74
75                         same_tech = True
76                         mat = None
77                         if l.material_slots and l.material_slots[0].material:
78                                 mat = l.material_slots[0].material.name
79                                 if mat!=prev_tech[1]:
80                                         same_tech = False
81                         if l.technique!=prev_tech[0]:
82                                 same_tech = False
83                         if i==0 or not same_tech:
84                                 self.export_object_technique(l, out_file, i)
85                                 prev_tech = (l.technique, mat)
86
87                         if i>0:
88                                 out_file.end()
89
90         def export_object_mesh(self, context, out_file, obj, progress):
91                 from .export_mesh import MeshExporter
92                 mesh_export = MeshExporter()
93                 for k, v in self.__dict__.items():
94                         setattr(mesh_export, k, v)
95
96                 lod_index = 0
97                 if obj.lod_for_parent:
98                         lod_index = obj.lod_index
99
100                 if self.separate_mesh:
101                         from .outfile import open_output
102                         path, name = external_name(out_file, ".mesh", lod_index)
103                         if self.shared_resources:
104                                 name = obj.data.name+".mesh"
105                         mesh_out = open_output(os.path.join(path, name))
106                         mesh = mesh_export.export(context, mesh_out, obj, progress)
107                         out_file.write("mesh", '"{}"'.format(name))
108                 else:
109                         out_file.begin("mesh")
110                         mesh = mesh_export.export(context, out_file, obj, progress)
111                         out_file.end()
112
113                 return mesh
114
115         def export_object_technique(self, obj, out_file, lod_index):
116                 material = None
117                 if obj.material_slots:
118                         material = obj.material_slots[0].material
119
120                 from .outfile import open_output
121                 path, name = external_name(out_file, ".tech", lod_index)
122
123                 if obj.technique:
124                         if obj.inherit_tech and material and (obj.override_material or material.texture_slots):
125                                 out_file.begin("technique")
126                                 out_file.begin("inherit", '"{}"'.format(obj.technique))
127                                 for slot in material.texture_slots:
128                                         if slot and slot.texture.type=="IMAGE":
129                                                 name = image_name(slot.texture.image)
130                                                 if slot.use_map_color_diffuse:
131                                                         out_file.write("texture", '"diffuse_map"', '"{}"'.format(name))
132                                                 elif slot.use_map_normal:
133                                                         out_file.write("texture", '"normal_map"', '"{}"'.format(name))
134                                 if obj.override_material:
135                                         mat_name = material.name+".mat"
136                                         mat_out = open_output(os.path.join(path, mat_name))
137                                         self.export_material(material, mat_out)
138                                         out_file.write("material", '"surface"', '"{}"'.format(mat_name))
139                                 out_file.end()
140                                 out_file.end()
141                         else:
142                                 out_file.write("technique", '"{}"'.format(obj.technique))
143                 elif self.separate_tech:
144                         if self.shared_resources and material:
145                                 name = material.name+".tech"
146                         tech_out = open_output(os.path.join(path, name))
147                         self.export_technique_definition(material, tech_out)
148                         out_file.write("technique", '"{}"'.format(name))
149                 else:
150                         out_file.begin("technique")
151                         self.export_technique_definition(material, out_file)
152                         out_file.end()
153
154         def export_technique_definition(self, material, out_file):
155                 out_file.begin("pass", '""')
156                 if material:
157                         out_file.begin("material")
158                         self.export_material(material, out_file)
159                         out_file.end()
160
161                         if self.textures!="NONE":
162                                 diffuse_tex = None
163                                 for slot in material.texture_slots:
164                                         if slot and slot.texture.type=="IMAGE" and slot.use_map_color_diffuse:
165                                                 diffuse_tex = slot.texture
166                                                 break
167
168                                 if diffuse_tex:
169                                         out_file.begin("texunit", 0)
170                                         if self.textures=="INLINE":
171                                                 out_file.begin("texture2d")
172                                                 out_file.write("min_filter", "LINEAR")
173                                                 out_file.write("storage", "RGBA", diffuse_tex.image.size[0], diffuse_tex.image.size[1])
174                                                 texdata = '"'
175                                                 for p in diffuse_tex.image.pixels:
176                                                         texdata += "\\x%02X"%int(p*255)
177                                                 texdata += '"'
178                                                 out_file.write("raw_data", texdata)
179                                                 out_file.end()
180                                         else:
181                                                 out_file.write("texture", '"%s"'%image_name(diffuse_tex.image))
182                                         out_file.end()
183
184                 out_file.end()
185
186         def export_material(self, mat, out_file):
187                 cm = get_colormap(mat.srgb_colors)
188                 if any((s and s.use_map_color_diffuse) for s in mat.texture_slots):
189                         out_file.write("diffuse", 1.0, 1.0, 1.0, 1.0)
190                         amb = cm(mat.ambient)
191                         out_file.write("ambient", amb, amb, amb, 1.0)
192                 else:
193                         diff = mat.diffuse_color*mat.diffuse_intensity
194                         out_file.write("diffuse", cm(diff.r), cm(diff.g), cm(diff.b), 1.0)
195                         amb = diff*mat.ambient
196                         out_file.write("ambient", cm(amb.r), cm(amb.g), cm(amb.b), 1.0)
197                 spec = mat.specular_color*mat.specular_intensity
198                 out_file.write("specular", spec.r, spec.g, spec.b, 1.0)
199                 out_file.write("shininess", mat.specular_hardness)