]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_scene.py
18cdcbca2404cea30f14b89f863e9aecef512c38
[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.visible_collections = 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.visible_collections:
18                         collections = [c.collection for c in context.view_layer.layer_collection.children if not (c.hide_viewport or c.collection.hide_viewport)]
19                         objs = [o for o in objs if any((o.name in c.all_objects) for c in collections)]
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                 objs.sort(key=lambda x:x.name)
23
24                 path, base = os.path.split(out_fn)
25                 base, ext = os.path.splitext(base)
26
27                 from .export_object import ObjectExporter
28                 object_export = ObjectExporter()
29
30                 object_prototypes = {}
31                 unique_objects = []
32                 export_names = {}
33                 for o in objs:
34                         if o.name in object_prototypes:
35                                 continue
36
37                         clones = [o]
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(m1.name!=m2.name for m1, m2 in zip(o.material_slots, 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                         filter = None
77                         if self.skip_existing:
78                                 filter = lambda r: not os.path.exists(os.path.join(path, r.name))
79                         scene_res.write_collection(os.path.join(path, base+"_resources.mdc"), exclude_self=True, filter=filter)
80                 else:
81                         res_dir = os.path.join(path, base+"_resources")
82                         if not os.path.exists(res_dir):
83                                 os.makedirs(res_dir)
84                         for r in refs:
85                                 r.write_to_file(os.path.join(res_dir, r.name))
86
87                 scene_res.write_to_file(out_fn)
88
89         def export_scene_resources(self, context, objs, resources, progress):
90                 from .export_object import ObjectExporter
91                 object_export = ObjectExporter()
92                 object_export.single_file = False
93
94                 material_atlases = {}
95
96                 for i, o in enumerate(objs):
97                         progress.push_task_slice(o.name, i, len(objs))
98                         object_export.export_object_resources(context, o, resources, progress, material_atlases=material_atlases)
99                         obj_name = o.name+".object"
100                         resources[obj_name] = object_export.export_object(context, o, progress, resources=resources)
101                         progress.pop_task()
102
103         def export_scene(self, context, objs, progress, *, prototypes, resources):
104                 from .datafile import Resource, Statement
105                 scene_res = Resource("scene.scene", "scene")
106
107                 for o in objs:
108                         obj_res = resources[prototypes[o.name].name+".object"]
109                         st = scene_res.create_reference_statement("object", obj_res, o.name)
110
111                         ss = Statement("transform")
112
113                         loc = o.matrix_world.to_translation()
114                         ss.sub.append(Statement("position", *tuple(loc)))
115
116                         quat = o.matrix_world.to_quaternion()
117                         if o.rotation_mode in ('XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX'):
118                                 angles = [a*180/math.pi for a in quat.to_euler()]
119                                 ss.sub.append(Statement("euler", *angles));
120                         else:
121                                 ss.sub.append(Statement("rotation", quat.angle*180/math.pi, *tuple(quat.axis)))
122
123                         scale = o.matrix_world.to_scale()
124                         ss.sub.append(Statement("scale", *tuple(scale)))
125
126                         st.sub.append(ss)
127                         scene_res.statements.append(st)
128
129                 progress.set_progress(1.0)
130
131                 return scene_res