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