]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_scene.py
Use the same mesh for multiple instances of an object in scene exporter
[libs/gl.git] / blender / io_mspgl / export_scene.py
1 import math
2 import os
3
4 class SceneExporter:
5         def __init__(self):
6                 self.external_tech = True
7                 self.resource_collection = True
8
9         def export(self, context, out_file):
10                 objs = context.selected_objects
11                 objs = [o for o in objs if o.type=="MESH" and (not o.compound or o.parent not in objs) and not o.lod_for_parent]
12
13                 from .outfile import open_output
14                 out_file = open_output(out_file)
15
16                 path, base = os.path.split(out_file.filename)
17                 base, ext = os.path.splitext(base)
18
19                 from .export_object import ObjectExporter
20                 object_export = ObjectExporter()
21                 object_export.compound = True
22                 object_export.external_tech = self.external_tech
23
24                 object_prototypes = {}
25                 unique_objects = []
26                 for o in objs:
27                         if o.name in object_prototypes:
28                                 continue
29
30                         clones = [o]
31                         if not any(s.link=="OBJECT" for s in o.material_slots):
32                                 for u in objs:
33                                         if u is o:
34                                                 continue
35                                         if u.data.name!=o.data.name:
36                                                 continue
37                                         if u.technique!=o.technique:
38                                                 continue
39                                         if any(s.link=="OBJECT" for s in u.material_slots):
40                                                 continue
41
42                                         clones.append(u)
43
44                         unique_objects.append(o)
45                         for c in clones:
46                                 object_prototypes[c.name] = o
47
48                 from .util import Progress
49                 progress = Progress(context)
50                 if self.resource_collection:
51                         res_out = open_output(os.path.join(path, base+"_resources.mdc"))
52
53                         # TODO Export techniques as separate items in the collection
54                         for i, o in enumerate(unique_objects):
55                                 res_out.begin("object", '"{}.object"'.format(o.name))
56                                 progress.push_task(o.name, i/len(objs), (i+1)/len(objs))
57                                 object_export.export(context, res_out, [o], progress)
58                                 progress.pop_task()
59                                 res_out.end()
60                 else:
61                         object_export.separate_mesh = True
62                         object_export.separate_tech = True
63                         res_dir = os.path.join(path, base+"_resources")
64                         if not os.path.exists(res_dir):
65                                 os.makedirs(res_dir)
66                         for i, o in enumerate(unique_objects):
67                                 obj_out = open_output(os.path.join(res_dir, o.name+".object"))
68                                 progress.push_task(o.name, i/len(objs), (i+1)/len(objs))
69                                 object_export.export(context, obj_out, [o], progress)
70                                 progress.pop_task()
71
72                 for o in objs:
73                         out_file.begin("object", '"{}.object"'.format(object_prototypes[o.name].name))
74                         # XXX Parent relationships screw up the location and rotation
75                         out_file.write("position", o.location[0], o.location[1], o.location[2])
76                         if o.rotation_mode=="AXIS_ANGLE":
77                                 angle = o.rotation_axis_angle[0]
78                                 axis = o.rotation_axis_angle[1:]
79                         else:
80                                 if o.rotation_mode=="QUATERNION":
81                                         q = o.rotation_quaternion
82                                 else:
83                                         q = o.rotation_euler.to_quaternion()
84                                 angle = q.angle
85                                 axis = q.axis
86                         out_file.write("rotation", angle*180/math.pi, axis[0], axis[1], axis[2])
87                         out_file.write("scale", o.scale[0], o.scale[1], o.scale[2])
88                         out_file.end();