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