import math
import os
-from .export_object import ObjectExporter
-from .outfile import OutFile
class SceneExporter:
def __init__(self):
- self.external_tech = True
- self.resource_collection = True
+ self.selected_only = False
+ self.visible_only = True
+ self.collection = True
+ self.skip_existing = True
+ self.show_progress = True
- def export(self, context, out_file):
- objs = context.selected_objects
- objs = [o for o in objs if o.type=="MESH" and (not o.compound or o.parent not in objs)]
+ def export_to_file(self, context, out_fn):
+ from .scene import create_scene_from_current
+ scene = create_scene_from_current(context, selected_only=self.selected_only, visible_only=self.visible_only)
- from .outfile import open_output
- out_file = open_output(out_file)
-
- path, base = os.path.split(out_file.filename)
+ path, base = os.path.split(out_fn)
base, ext = os.path.splitext(base)
- object_export = ObjectExporter()
- object_export.compound = True
- object_export.external_tech = self.external_tech
+ export_names = {}
+ used_names = set()
+ for p in scene.prototypes:
+ clones = [i for i in scene.instances if i.prototype==p.name]
+
+ prefix = p.name
+ for c in clones:
+ while not c.name.startswith(prefix):
+ pos = max(prefix.rfind(' '), prefix.rfind('.'))
+ if pos<0:
+ break;
+ prefix = prefix[:pos]
+
+ if prefix:
+ export_names[p.name+".object"] = prefix.strip(" .")
+ else:
+ used_names.add(p.name)
+
+ for n, e in export_names.items():
+ if e in used_names:
+ number = 1
+ while "{}_{}".format(e, number) in used_names:
+ number += 1
+ e += "_{}".format(number)
+ export_names[n] = e+".object"
+ used_names.add(e)
- if self.resource_collection:
- res_out = open_output(os.path.join(path, base+"_resources.mdc"))
+ from .util import Progress
+ progress = Progress(self.show_progress and context)
- # TODO Export techniques as separate items in the collection
- for o in objs:
- res_out.begin("object", '"{}.object"'.format(o.name))
- object_export.export(context, res_out, [o])
- res_out.end()
+ from .export import DataExporter
+ data_exporter = DataExporter()
+
+ resources = {}
+ data_exporter.export_resources(context, scene.prototypes, resources, None, progress)
+ for n, r in resources.items():
+ if r.name in export_names:
+ r.name = export_names[r.name]
+
+ scene_res = self.export_scene(scene, resources, progress)
+ refs = scene_res.collect_references()
+
+ if self.collection:
+ existing = None
+ if self.skip_existing:
+ existing = lambda r: not os.path.exists(os.path.join(path, r.name))
+ scene_res.write_collection(out_fn, filter=existing)
else:
- object_export.separate_tech = True
- res_dir = os.path.join(path, base+"_resources")
- if not os.path.exists(res_dir):
- os.makedirs(res_dir)
- for o in objs:
- obj_out = open_output(os.path.join(res_dir, o.name+".object"))
- object_export.export(context, obj_out, [o])
-
- for o in objs:
- out_file.begin("object", '"{}.object"'.format(o.name))
- # XXX Parent relationships screw up the location and rotation
- out_file.write("position", o.location[0], o.location[1], o.location[2])
- if o.rotation_mode=="AXIS_ANGLE":
- angle = o.rotation_axis_angle[0]
- axis = o.rotation_axis_angle[1:]
+ scene_res.write_to_file(out_fn)
+ for r in refs:
+ r.write_to_file(os.path.join(path, r.name))
+
+ def export_scene(self, scene, resources, progress):
+ from .datafile import Resource, Statement, Token
+ scene_res = Resource(scene.name+".scene", "scene")
+
+ scene_res.statements.append(Statement("type", Token(scene.scene_type.lower())))
+
+ for i in scene.instances:
+ obj_res = resources[i.prototype+".object"]
+ st = scene_res.create_reference_statement("object", obj_res, i.name)
+
+ ss = Statement("transform")
+
+ loc = i.matrix_world.to_translation()
+ ss.sub.append(Statement("position", *tuple(loc)))
+
+ quat = i.matrix_world.to_quaternion()
+ if i.rotation_mode in ('XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX'):
+ angles = [a*180/math.pi for a in quat.to_euler()]
+ ss.sub.append(Statement("euler", *angles));
else:
- if o.rotation_mode=="QUATERNION":
- q = o.rotation_quaternion
- else:
- q = o.rotation_euler.to_quaternion()
- angle = q.angle
- axis = q.axis
- out_file.write("rotation", angle*180/math.pi, axis[0], axis[1], axis[2])
- out_file.end();
+ ss.sub.append(Statement("rotation", quat.angle*180/math.pi, *tuple(quat.axis)))
+
+ scale = i.matrix_world.to_scale()
+ ss.sub.append(Statement("scale", *tuple(scale)))
+
+ st.sub.append(ss)
+ scene_res.statements.append(st)
+
+ progress.set_progress(1.0)
+
+ return scene_res