]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_object.py
Display overall progress when exporting scenes
[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                 self.separate_tech = False
17                 self.external_tech = True
18                 self.shared_tech = True
19
20         def export(self, context, out_file, objs=None, progress=None):
21                 if objs is None:
22                         obj = context.active_object
23                 else:
24                         obj = objs[0]
25
26                 from .outfile import open_output
27                 out_file = open_output(out_file)
28
29                 from .export_mesh import MeshExporter
30                 mesh_export = MeshExporter()
31                 for k, v in self.__dict__.items():
32                         setattr(mesh_export, k, v)
33
34                 if self.separate_mesh:
35                         path, base = os.path.split(out_file.filename)
36                         base, ext = os.path.splitext(base)
37                         mesh_out = open_output(os.path.join(path, base+".mesh"))
38                         mesh = mesh_export.export(context, mesh_out, objs, progress)
39                         out_file.write("mesh", '"{}.mesh"'.format(base))
40                 else:
41                         out_file.begin("mesh")
42                         mesh = mesh_export.export(context, out_file, objs, progress)
43                         out_file.end()
44
45                 if self.external_tech and obj.technique:
46                         out_file.write("technique", '"{}"'.format(obj.technique))
47                 elif self.separate_tech:
48                         path, base = os.path.split(out_file.filename)
49                         if self.shared_tech and mesh.materials:
50                                 tech_name = mesh.materials[0].name+".tech"
51                         else:
52                                 base, ext = os.path.splitext(base)
53                                 tech_name = base+".tech"
54                         tech_out = open_output(os.path.join(path, tech_name))
55                         self.export_technique(mesh, tech_out)
56                         out_file.write("technique", '"{}"'.format(tech_name))
57                 else:
58                         out_file.begin("technique")
59                         self.export_technique(mesh, out_file)
60                         out_file.end()
61
62         def export_technique(self, mesh, out_file):
63                 out_file.begin("pass", '""')
64                 if mesh.materials:
65                         if self.srgb_colors:
66                                 cm = linear_to_srgb
67                         else:
68                                 cm = lambda x: x
69
70                         if self.material_tex:
71                                 out_file.begin("material")
72                                 out_file.write("diffuse", 1.0, 1.0, 1.0, 1.0)
73                                 out_file.end()
74                                 index = 0
75                                 for u in mesh.uv_layers:
76                                         if u.name=="material_tex":
77                                                 index = u.unit
78                                 out_file.begin("texunit", index)
79                                 out_file.begin("texture2d")
80                                 out_file.write("min_filter", "NEAREST")
81                                 out_file.write("mag_filter", "NEAREST")
82                                 out_file.write("storage", "RGB", len(mesh.materials), 1)
83                                 texdata = '"'
84                                 for m in mesh.materials:
85                                         color = [int(cm(c)*255) for c in m.diffuse_color]
86                                         texdata += "\\x%02X\\x%02X\\x%02X"%tuple(color)
87                                 texdata += '"'
88                                 out_file.write("raw_data", texdata)
89                                 out_file.end()
90                                 out_file.end()
91                         else:
92                                 mat = mesh.materials[0]
93                                 out_file.begin("material")
94                                 if any((s and s.use_map_color_diffuse) for s in mat.texture_slots):
95                                         out_file.write("diffuse", 1.0, 1.0, 1.0, 1.0)
96                                         amb = cm(mat.ambient)
97                                         out_file.write("ambient", amb, amb, amb, 1.0)
98                                 else:
99                                         diff = mat.diffuse_color
100                                         out_file.write("diffuse", cm(diff.r), cm(diff.g), cm(diff.b), 1.0)
101                                         amb = diff*mat.ambient
102                                         out_file.write("ambient", cm(amb.r), cm(amb.g), cm(amb.b), 1.0)
103                                 spec = mat.specular_color*mat.specular_intensity
104                                 out_file.write("specular", spec.r, spec.g, spec.b, 1.0)
105                                 out_file.write("shininess", mat.specular_hardness);
106                                 out_file.end()
107
108                         if self.textures!="NONE":
109                                 for slot in mesh.materials[0].texture_slots:
110                                         if not slot:
111                                                 continue
112
113                                         tex = slot.texture
114                                         if tex.type!="IMAGE":
115                                                 continue
116
117                                         if slot.uv_layer:
118                                                 for u in mesh.uv_layers:
119                                                         if u.name==slot.uv_layer:
120                                                                 index = u.unit
121                                         else:
122                                                 index = mesh.uv_layers[0].unit
123
124                                         out_file.begin("texunit", index)
125                                         if self.textures=="INLINE":
126                                                 out_file.begin("texture2d")
127                                                 out_file.write("min_filter", "LINEAR")
128                                                 out_file.write("storage", "RGBA", tex.image.size[0], tex.image.size[1])
129                                                 texdata = '"'
130                                                 for p in tex.image.pixels:
131                                                         texdata += "\\x%02X"%int(p*255)
132                                                 texdata += '"'
133                                                 out_file.write("raw_data", texdata)
134                                                 out_file.end()
135                                         else:
136                                                 out_file.write("texture", '"%s"'%tex.image.name)
137                                         out_file.end()
138
139                 out_file.end()