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