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