class SceneExporter:
def __init__(self):
- 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_to_file(self, context, out_fn):
- objs = [o for o in context.selected_objects if o.type=="MESH" and not o.lod_for_parent]
- objs = [o for o in objs if (not o.compound or o.parent not in objs)]
+ from .util import Progress
+ progress = Progress(self.show_progress and context)
+
+ from .scene import create_scene_from_current
+ scene = create_scene_from_current(context, selected_only=self.selected_only, visible_only=self.visible_only)
+
+ resources = {}
+ self.export_scene_resources(context, scene, resources, progress)
+ scene_res = self.export_scene(scene, resources)
+ progress.set_progress(1.0)
path, base = os.path.split(out_fn)
base, ext = os.path.splitext(base)
- from .export_object import ObjectExporter
- object_export = ObjectExporter()
-
- object_prototypes = {}
- unique_objects = []
- for o in objs:
- if o.name in object_prototypes:
- continue
-
- clones = [o]
- if not any(s.link=="OBJECT" for s in o.material_slots):
- for u in objs:
- if u is o:
- continue
- if u.data.name!=o.data.name:
- continue
- if u.technique!=o.technique:
- continue
- if any(s.link=="OBJECT" for s in u.material_slots):
- continue
-
- clones.append(u)
-
- unique_objects.append(o)
- for c in clones:
- object_prototypes[c.name] = o
+ 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:
+ scene_res.write_to_file(out_fn)
+ for r in scene_res.collect_references():
+ r.write_to_file(os.path.join(path, r.name))
- from .util import Progress
- progress = Progress(self.show_progress and context)
+ def export_scene_resources(self, context, scene, resources, progress):
+ from .export import DataExporter
+ data_exporter = DataExporter()
- from .export_object import ObjectExporter
- object_export = ObjectExporter()
-
- from .datafile import Statement
- if self.resource_collection:
- with open(os.path.join(path, base+"_resources.mdc"), "w") as res_out:
- for i, o in enumerate(unique_objects):
- progress.push_task_slice(o.name, i, len(unique_objects))
- st = Statement("object", "{}.object".format(o.name))
- st.sub = object_export.export_object(context, o, progress)
- st.write_to_file(res_out)
- progress.pop_task()
- else:
- res_dir = os.path.join(path, base+"_resources")
- if not os.path.exists(res_dir):
- os.makedirs(res_dir)
- for i, o in enumerate(unique_objects):
- progress.push_task_slice(o.name, i, len(unique_objects))
- st = object_export.export_object(context, o, progress)
- with open(os.path.join(res_dir, o.name+".object"), "w") as obj_out:
- for s in st:
- s.write_to_file(obj_out)
- progress.pop_task()
-
- statements = self.export_scene(context, objs, progress, prototypes=object_prototypes)
-
- with open(out_fn, "w") as out_file:
- for s in statements:
- s.write_to_file(out_file)
-
- def export_scene(self, context, objs, progress, *, prototypes=None):
- from .datafile import Statement
- statements = []
-
- for o in objs:
- st = Statement("object", "{}.object".format(prototypes[o.name].name))
- # XXX Parent relationships screw up the location and rotation
- st.sub.append(Statement("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:]
+ data_exporter.export_resources(context, scene.prototypes, resources, None, progress)
+
+ def export_scene(self, scene, resources):
+ 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
- st.sub.append(Statement("rotation", angle*180/math.pi, axis[0], axis[1], axis[2]))
- st.sub.append(Statement("scale", o.scale[0], o.scale[1], o.scale[2]))
- statements.append(st)
+ ss.sub.append(Statement("rotation", quat.angle*180/math.pi, *tuple(quat.axis)))
- progress.set_progress(1.0)
+ scale = i.matrix_world.to_scale()
+ ss.sub.append(Statement("scale", *tuple(scale)))
+
+ st.sub.append(ss)
+ scene_res.statements.append(st)
- return statements
+ return scene_res