+import os
+
+class DataExporter:
+ def __init__(self):
+ self.show_progress = True
+ self.collection = False
+ self.shared_resources = True
+
+ def export_to_file(self, context, out_fn):
+ from .util import Progress
+ progress = Progress(self.show_progress and context)
+
+ objects = context.selected_objects
+
+ resources = {}
+ material_atlases = {}
+
+ dummy_res = self.export_resources(context, objects, resources, material_atlases, progress)
+
+ path, base = os.path.split(out_fn)
+ base, ext = os.path.splitext(base)
+
+ refs = dummy_res.collect_references()
+ if not self.shared_resources:
+ numbers = {}
+ for r in refs:
+ res_ext = os.path.splitext(r.name)[1]
+ n = numbers.get(res_ext, 0)
+ if n>0:
+ r.name = "{}_{}{}".format(base, n, res_ext)
+ else:
+ r.name = base+res_ext
+ numbers[res_ext] = n+1
+
+ if self.collection:
+ dummy_res.write_collection(out_fn, exclude_self=True)
+ else:
+ for r in refs:
+ r.write_to_file(os.path.join(path, r.name))
+
+ def export_resources(self, context, objects, resources, material_atlases, progress):
+ if material_atlases is None:
+ material_atlases = {}
+
+ object_exporter = None
+ camera_exporter = None
+ armature_exporter = None
+
+ from .datafile import Resource
+ dummy_res = Resource("dummy", "dummy")
+
+ for i, obj in enumerate(objects):
+ progress.push_task_slice(obj.name, i, len(objects))
+ res = None
+ if obj.type=='MESH':
+ if not object_exporter:
+ from .export_object import ObjectExporter
+ object_exporter = ObjectExporter()
+ object_exporter.export_object_resources(context, obj, resources, material_atlases, progress)
+ res = object_exporter.export_object(obj, resources, progress)
+ elif obj.type=='CAMERA':
+ if not camera_exporter:
+ from .export_camera import CameraExporter
+ camera_exporter = CameraExporter()
+ res = camera_exporter.export_camera(obj)
+ elif obj.type=='ARMATURE':
+ if not armature_exporter:
+ from .export_armature import ArmatureExporter
+ armature_exporter = ArmatureExporter()
+ res = armature_exporter.export_armature(context, obj)
+
+ if res:
+ resources[res.name] = res
+ dummy_res.create_reference_statement("ref", res)
+
+ progress.pop_task()
+
+ return dummy_res