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