]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export.py
96f39320ec7d35e590840573d7d9b7d75b53df8b
[libs/gl.git] / blender / io_mspgl / export.py
1 import os
2
3 class DataExporter:
4         def __init__(self):
5                 self.show_progress = True
6                 self.collection = False
7                 self.shared_resources = True
8
9         def export_to_file(self, context, out_fn):
10                 from .util import Progress
11                 progress = Progress(self.show_progress and context)
12
13                 objects = context.selected_objects
14
15                 resources = {}
16                 material_atlases = {}
17
18                 dummy_res = self.export_resources(context, objects, resources, material_atlases, progress)
19
20                 path, base = os.path.split(out_fn)
21                 base, ext = os.path.splitext(base)
22
23                 refs = dummy_res.collect_references()
24                 if not self.shared_resources:
25                         numbers = {}
26                         for r in refs:
27                                 res_ext = os.path.splitext(r.name)[1]
28                                 n = numbers.get(res_ext, 0)
29                                 if n>0:
30                                         r.name = "{}_{}{}".format(base, n, res_ext)
31                                 else:
32                                         r.name = base+res_ext
33                                 numbers[res_ext] = n+1
34
35                 if self.collection:
36                         dummy_res.write_collection(out_fn, exclude_self=True)
37                 else:
38                         for r in refs:
39                                 r.write_to_file(os.path.join(path, r.name))
40
41         def export_resources(self, context, objects, resources, material_atlases, progress):
42                 if material_atlases is None:
43                         material_atlases = {}
44
45                 object_exporter = None
46                 camera_exporter = None
47                 armature_exporter = None
48
49                 from .datafile import Resource
50                 dummy_res = Resource("dummy", "dummy")
51
52                 for i, obj in enumerate(objects):
53                         progress.push_task_slice(obj.name, i, len(objects))
54                         res = None
55                         if obj.type=='MESH':
56                                 if not object_exporter:
57                                         from .export_object import ObjectExporter
58                                         object_exporter = ObjectExporter()
59                                 object_exporter.export_object_resources(context, obj, resources, material_atlases, progress)
60                                 res = object_exporter.export_object(obj, resources, progress)
61                         elif obj.type=='CAMERA':
62                                 if not camera_exporter:
63                                         from .export_camera import CameraExporter
64                                         camera_exporter = CameraExporter()
65                                 res = camera_exporter.export_camera(obj)
66                         elif obj.type=='ARMATURE':
67                                 if not armature_exporter:
68                                         from .export_armature import ArmatureExporter
69                                         armature_exporter = ArmatureExporter()
70                                 res = armature_exporter.export_armature(context, obj)
71
72                         if res:
73                                 resources[res.name] = res
74                                 dummy_res.create_reference_statement("ref", res)
75
76                         progress.pop_task()
77
78                 return dummy_res