]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export.py
Ignore already existing resources in DataExporter
[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_name = None
55                         res = None
56                         if obj.type=='MESH':
57                                 res_name = obj.name+".object"
58                                 if res_name not in resources:
59                                         if not object_exporter:
60                                                 from .export_object import ObjectExporter
61                                                 object_exporter = ObjectExporter()
62                                         object_exporter.export_object_resources(context, obj, resources, material_atlases, progress)
63                                         res = object_exporter.export_object(obj, resources, progress)
64                         elif obj.type=='CAMERA':
65                                 res_name = obj.name+".camera"
66                                 if res_name not in resources:
67                                         if not camera_exporter:
68                                                 from .export_camera import CameraExporter
69                                                 camera_exporter = CameraExporter()
70                                         res = camera_exporter.export_camera(obj)
71                         elif obj.type=='ARMATURE':
72                                 res_name = obj.name+".arma"
73                                 if res_name not in resources:
74                                         if not armature_exporter:
75                                                 from .export_armature import ArmatureExporter
76                                                 armature_exporter = ArmatureExporter()
77                                         res = armature_exporter.export_armature(context, obj)
78
79                         if res:
80                                 resources[res_name] = res
81                                 dummy_res.create_reference_statement("ref", res)
82
83                         progress.pop_task()
84
85                 return dummy_res