]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_scene.py
Improve progress reporting in the Blender 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.resource_collection = True
7                 self.show_progress = 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
22                 object_prototypes = {}
23                 unique_objects = []
24                 for o in objs:
25                         if o.name in object_prototypes:
26                                 continue
27
28                         clones = [o]
29                         if not any(s.link=="OBJECT" for s in o.material_slots):
30                                 for u in objs:
31                                         if u is o:
32                                                 continue
33                                         if u.data.name!=o.data.name:
34                                                 continue
35                                         if u.technique!=o.technique:
36                                                 continue
37                                         if any(s.link=="OBJECT" for s in u.material_slots):
38                                                 continue
39
40                                         clones.append(u)
41
42                         unique_objects.append(o)
43                         for c in clones:
44                                 object_prototypes[c.name] = o
45
46                 from .util import Progress
47                 progress = Progress(self.show_progress and context)
48
49                 if self.resource_collection:
50                         res_out = open_output(os.path.join(path, base+"_resources.mdc"))
51
52                         # TODO Export techniques as separate items in the collection
53                         for i, o in enumerate(unique_objects):
54                                 res_out.begin("object", '"{}.object"'.format(o.name))
55                                 progress.push_task_slice(o.name, i, len(objs))
56                                 object_export.export(context, res_out, o, progress)
57                                 progress.pop_task()
58                                 res_out.end()
59                 else:
60                         object_export.separate_mesh = True
61                         object_export.separate_tech = True
62                         res_dir = os.path.join(path, base+"_resources")
63                         if not os.path.exists(res_dir):
64                                 os.makedirs(res_dir)
65                         for i, o in enumerate(unique_objects):
66                                 obj_out = open_output(os.path.join(res_dir, o.name+".object"))
67                                 progress.push_task_slice(o.name, i, len(objs))
68                                 object_export.export(context, obj_out, o, progress)
69                                 progress.pop_task()
70
71                 for o in objs:
72                         out_file.begin("object", '"{}.object"'.format(object_prototypes[o.name].name))
73                         # XXX Parent relationships screw up the location and rotation
74                         out_file.write("position", o.location[0], o.location[1], o.location[2])
75                         if o.rotation_mode=="AXIS_ANGLE":
76                                 angle = o.rotation_axis_angle[0]
77                                 axis = o.rotation_axis_angle[1:]
78                         else:
79                                 if o.rotation_mode=="QUATERNION":
80                                         q = o.rotation_quaternion
81                                 else:
82                                         q = o.rotation_euler.to_quaternion()
83                                 angle = q.angle
84                                 axis = q.axis
85                         out_file.write("rotation", angle*180/math.pi, axis[0], axis[1], axis[2])
86                         out_file.write("scale", o.scale[0], o.scale[1], o.scale[2])
87                         out_file.end()