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