]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_object.py
12b15b63c26586abe203629992782d594b266a64
[libs/gl.git] / blender / io_mspgl / export_object.py
1 def linear_to_srgb(l):
2         if l<0.0031308:
3                 return 12.92*l
4         else:
5                 return 1.055*(l**(1/2.4))-0.055
6
7
8 class ObjectExporter:
9         def __init__(self):
10                 self.material_tex = False
11                 self.srgb_colors = True
12                 self.textures = "REF"
13
14         def export(self, context, out_file):
15                 from .outfile import open_output
16                 out_file = open_output(out_file)
17
18                 from .export_mesh import MeshExporter
19                 mesh_export = MeshExporter()
20                 for k, v in self.__dict__.items():
21                         setattr(mesh_export, k, v)
22
23                 out_file.begin("mesh")
24                 mesh = mesh_export.export(context, out_file)
25                 out_file.end()
26
27                 out_file.begin("technique")
28                 out_file.begin("pass", '""')
29                 if mesh.materials:
30                         if self.srgb_colors:
31                                 cm = linear_to_srgb
32                         else:
33                                 cm = lambda x: x
34
35                         if self.material_tex:
36                                 out_file.begin("material")
37                                 out_file.write("diffuse", 1.0, 1.0, 1.0, 1.0)
38                                 out_file.end()
39                                 index = 0
40                                 for u in mesh.uv_layers:
41                                         if u.name=="material_tex":
42                                                 index = u.unit
43                                 out_file.begin("texunit", index)
44                                 out_file.begin("texture2d")
45                                 out_file.write("min_filter", "NEAREST")
46                                 out_file.write("mag_filter", "NEAREST")
47                                 out_file.write("storage", "RGB", len(mesh.materials), 1)
48                                 texdata = '"'
49                                 for m in mesh.materials:
50                                         color = [int(cm(c)*255) for c in m.diffuse_color]
51                                         texdata += "\\x%02X\\x%02X\\x%02X"%tuple(color)
52                                 texdata += '"'
53                                 out_file.write("raw_data", texdata)
54                                 out_file.end()
55                                 out_file.end()
56                         else:
57                                 mat = mesh.materials[0]
58                                 out_file.begin("material")
59                                 if any((s and s.use_map_color_diffuse) for s in mat.texture_slots):
60                                         out_file.write("diffuse", 1.0, 1.0, 1.0, 1.0)
61                                         amb = cm(mat.ambient)
62                                         out_file.write("ambient", amb, amb, amb, 1.0)
63                                 else:
64                                         diff = mat.diffuse_color
65                                         out_file.write("diffuse", cm(diff.r), cm(diff.g), cm(diff.b), 1.0)
66                                         amb = diff*mat.ambient
67                                         out_file.write("ambient", cm(amb.r), cm(amb.g), cm(amb.b), 1.0)
68                                 spec = mat.specular_color*mat.specular_intensity
69                                 out_file.write("specular", spec.r, spec.g, spec.b, 1.0)
70                                 out_file.write("shininess", mat.specular_hardness);
71                                 out_file.end()
72
73                         if self.textures!="NONE":
74                                 for slot in mesh.materials[0].texture_slots:
75                                         if not slot:
76                                                 continue
77
78                                         tex = slot.texture
79                                         if tex.type!="IMAGE":
80                                                 continue
81
82                                         if slot.uv_layer:
83                                                 for u in mesh.uv_layers:
84                                                         if u.name==slot.uv_layer:
85                                                                 index = u.unit
86                                         else:
87                                                 index = mesh.uv_layers[0].unit
88
89                                         out_file.begin("texunit", index)
90                                         if self.textures=="INLINE":
91                                                 out_file.begin("texture2d")
92                                                 out_file.write("min_filter", "LINEAR")
93                                                 out_file.write("storage", "RGBA", tex.image.size[0], tex.image.size[1])
94                                                 texdata = '"'
95                                                 for p in tex.image.pixels:
96                                                         texdata += "\\x%02X"%int(p*255)
97                                                 texdata += '"'
98                                                 out_file.write("raw_data", texdata)
99                                                 out_file.end()
100                                         else:
101                                                 out_file.write("texture", '"%s"'%tex.image.name)
102                                         out_file.end()
103
104                 out_file.end()
105                 out_file.end()