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