]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_scene.py
f6776107413955964d0e3e757bc5594e3a69d03f
[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_to_file(self, context, out_fn):
10                 objs = [o for o in context.selected_objects if o.type=="MESH" and not o.lod_for_parent]
11                 objs = [o for o in objs if (not o.compound or o.parent not in objs)]
12
13                 path, base = os.path.split(out_fn)
14                 base, ext = os.path.splitext(base)
15
16                 from .export_object import ObjectExporter
17                 object_export = ObjectExporter()
18
19                 object_prototypes = {}
20                 unique_objects = []
21                 for o in objs:
22                         if o.name in object_prototypes:
23                                 continue
24
25                         clones = [o]
26                         if not any(s.link=="OBJECT" for s in o.material_slots):
27                                 for u in objs:
28                                         if u is o:
29                                                 continue
30                                         if u.data.name!=o.data.name:
31                                                 continue
32                                         if u.technique!=o.technique:
33                                                 continue
34                                         if any(s.link=="OBJECT" for s in u.material_slots):
35                                                 continue
36
37                                         clones.append(u)
38
39                         unique_objects.append(o)
40                         for c in clones:
41                                 object_prototypes[c.name] = o
42
43                 from .util import Progress
44                 progress = Progress(self.show_progress and context)
45
46                 from .export_object import ObjectExporter
47                 object_export = ObjectExporter()
48
49                 from .datafile import Statement
50                 if self.resource_collection:
51                         with open(os.path.join(path, base+"_resources.mdc"), "w") as res_out:
52                                 for i, o in enumerate(unique_objects):
53                                         progress.push_task_slice(o.name, i, len(unique_objects))
54                                         st = Statement("object", "{}.object".format(o.name))
55                                         st.sub = object_export.export_object(context, o, progress)
56                                         st.write_to_file(res_out)
57                                         progress.pop_task()
58                 else:
59                         res_dir = os.path.join(path, base+"_resources")
60                         if not os.path.exists(res_dir):
61                                 os.makedirs(res_dir)
62                         for i, o in enumerate(unique_objects):
63                                 progress.push_task_slice(o.name, i, len(unique_objects))
64                                 st = object_export.export_object(context, o, progress)
65                                 with open(os.path.join(res_dir, o.name+".object"), "w") as obj_out:
66                                         for s in st:
67                                                 s.write_to_file(obj_out)
68                                 progress.pop_task()
69
70                 statements = self.export_scene(context, objs, progress, prototypes=object_prototypes)
71
72                 with open(out_fn, "w") as out_file:
73                         for s in statements:
74                                 s.write_to_file(out_file)
75
76         def export_scene(self, context, objs, progress, *, prototypes=None):
77                 from .datafile import Statement
78                 statements = []
79
80                 for o in objs:
81                         st = Statement("object", "{}.object".format(prototypes[o.name].name))
82                         # XXX Parent relationships screw up the location and rotation
83                         st.sub.append(Statement("position", o.location[0], o.location[1], o.location[2]))
84                         if o.rotation_mode=="AXIS_ANGLE":
85                                 angle = o.rotation_axis_angle[0]
86                                 axis = o.rotation_axis_angle[1:]
87                         else:
88                                 if o.rotation_mode=="QUATERNION":
89                                         q = o.rotation_quaternion
90                                 else:
91                                         q = o.rotation_euler.to_quaternion()
92                                 angle = q.angle
93                                 axis = q.axis
94                         st.sub.append(Statement("rotation", angle*180/math.pi, axis[0], axis[1], axis[2]))
95                         st.sub.append(Statement("scale", o.scale[0], o.scale[1], o.scale[2]))
96                         statements.append(st)
97
98                 progress.set_progress(1.0)
99
100                 return statements