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