]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_scene.py
Import fixes for 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_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                 export_names = {}
22                 for o in objs:
23                         if o.name in object_prototypes:
24                                 continue
25
26                         clones = [o]
27                         if not any(s.link=="OBJECT" for s in o.material_slots):
28                                 for u in objs:
29                                         if u is o:
30                                                 continue
31                                         if u.data.name!=o.data.name:
32                                                 continue
33                                         if any(s.link=="OBJECT" for s in u.material_slots):
34                                                 continue
35
36                                         clones.append(u)
37
38                         prefix = o.name
39                         for c in clones:
40                                 while not c.name.startswith(prefix):
41                                         pos = max(prefix.rfind(' '), prefix.rfind('.'))
42                                         if pos<0:
43                                                 break;
44                                         prefix = prefix[:pos]
45
46                         if prefix:
47                                 export_names[o.name+".object"] = prefix.strip(" .")+".object"
48
49                         unique_objects.append(o)
50                         for c in clones:
51                                 object_prototypes[c.name] = o
52
53                 from .util import Progress
54                 progress = Progress(self.show_progress and context)
55
56                 resources = {}
57                 self.export_scene_resources(context, unique_objects, resources, progress)
58                 for n, r in resources.items():
59                         if r.name in export_names:
60                                 r.name = export_names[r.name]
61
62                 scene_res = self.export_scene(context, objs, progress, prototypes=object_prototypes, resources=resources)
63                 refs = scene_res.collect_references()
64
65                 if self.resource_collection:
66                         from .datafile import Statement
67                         keywords = { ".mat": "material",
68                                 ".mesh": "mesh",
69                                 ".object": "object",
70                                 ".tech": "technique",
71                                 ".tex2d": "texture2d" }
72                         with open(os.path.join(path, base+"_resources.mdc"), "w") as res_out:
73                                 for r in refs:
74                                         st = Statement(keywords[os.path.splitext(r.name)[1]], r.name)
75                                         st.sub = r.statements
76                                         st.write_to_file(res_out)
77                 else:
78                         res_dir = os.path.join(path, base+"_resources")
79                         if not os.path.exists(res_dir):
80                                 os.makedirs(res_dir)
81                         for r in refs:
82                                 with open(os.path.join(res_dir, r.name), "w") as res_out:
83                                         for s in r.statements:
84                                                 s.write_to_file(res_out)
85
86                 with open(out_fn, "w") as out_file:
87                         for s in scene_res.statements:
88                                 s.write_to_file(out_file)
89
90         def export_scene_resources(self, context, objs, resources, progress):
91                 from .export_object import ObjectExporter
92                 object_export = ObjectExporter()
93                 object_export.single_file = False
94
95                 material_maps = {}
96
97                 for i, o in enumerate(objs):
98                         progress.push_task_slice(o.name, i, len(objs))
99                         object_export.export_object_resources(context, o, resources, progress, material_maps=material_maps)
100                         obj_name = o.name+".object"
101                         resources[obj_name] = object_export.export_object(context, o, progress, resources=resources)
102                         progress.pop_task()
103
104         def export_scene(self, context, objs, progress, *, prototypes, resources):
105                 from .datafile import Resource, Statement
106                 scene_res = Resource("scene.scene")
107
108                 for o in objs:
109                         obj_res = resources[prototypes[o.name].name+".object"]
110                         st = scene_res.create_reference_statement("object", obj_res, o.name)
111
112                         ss = Statement("transform")
113
114                         loc = o.matrix_world.to_translation()
115                         ss.sub.append(Statement("position", *tuple(loc)))
116
117                         quat = o.matrix_world.to_quaternion()
118                         if o.rotation_mode in ('XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX'):
119                                 angles = [a*180/math.pi for a in quat.to_euler()]
120                                 ss.sub.append(Statement("euler", *angles));
121                         else:
122                                 ss.sub.append(Statement("rotation", quat.angle*180/math.pi, *tuple(quat.axis)))
123
124                         scale = o.matrix_world.to_scale()
125                         ss.sub.append(Statement("scale", *tuple(scale)))
126
127                         st.sub.append(ss)
128                         scene_res.statements.append(st)
129
130                 progress.set_progress(1.0)
131
132                 return scene_res