class SceneExporter:
def __init__(self):
- self.resource_collection = True
+ self.selected_only = False
+ self.visible_only = True
+ self.collection = True
+ self.skip_existing = 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) and not o.lod_for_parent]
+ def export_to_file(self, context, out_fn):
+ from .util import Progress
+ progress = Progress(context)
- from .outfile import open_output
- out_file = open_output(out_file)
+ from .scene import create_scene_from_current
+ scene = create_scene_from_current(context, selected_only=self.selected_only, visible_only=self.visible_only)
- path, base = os.path.split(out_file.filename)
- base, ext = os.path.splitext(base)
+ resources = {}
+ self.export_scene_resources(context, scene, resources, progress)
+ scene_res = self.export_scene(scene, resources)
+ progress.set_progress(1.0)
- 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
+ path, base = os.path.split(out_fn)
+ base, ext = os.path.splitext(base)
- from .util import Progress
- progress = Progress(context)
- if self.resource_collection:
- res_out = open_output(os.path.join(path, base+"_resources.mdc"))
-
- # TODO Export techniques as separate items in the collection
- for i, o in enumerate(unique_objects):
- res_out.begin("object", '"{}.object"'.format(o.name))
- progress.push_task(o.name, i/len(objs), (i+1)/len(objs))
- object_export.export(context, res_out, o, progress)
- progress.pop_task()
- res_out.end()
+ 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_mesh = True
- 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 i, o in enumerate(unique_objects):
- obj_out = open_output(os.path.join(res_dir, o.name+".object"))
- progress.push_task(o.name, i/len(objs), (i+1)/len(objs))
- object_export.export(context, obj_out, o, progress)
- progress.pop_task()
-
- for o in objs:
- out_file.begin("object", '"{}.object"'.format(object_prototypes[o.name].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 scene_res.collect_references():
+ r.write_to_file(os.path.join(path, r.name))
+
+ def export_scene_resources(self, context, scene, resources, progress):
+ from .export import DataExporter
+ data_exporter = DataExporter()
+
+ 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
- out_file.write("rotation", angle*180/math.pi, axis[0], axis[1], axis[2])
- out_file.write("scale", o.scale[0], o.scale[1], o.scale[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)
+
+ return scene_res
+
+ def export_sequence_resources(self, scene, resources):
+ from .datafile import Resource, Statement, Token
+
+ if scene.background_set:
+ wrapper_name = scene.name+".wrapper.scene"
+ if wrapper_name not in resources:
+ wrapper_res = Resource(wrapper_name, "scene")
+ wrapper_res.statements.append(Statement("type", Token("ordered")))
+ for s in scene.get_chain():
+ wrapper_res.statements.append(wrapper_res.create_reference_statement("scene", resources[s.name+".scene"]))
+
+ resources[wrapper_name] = wrapper_res
+
+ lights = []
+ s = scene
+ while s:
+ lights += s.lights
+ s = s.background_set
+
+ from .util import make_unique
+ lights = make_unique(lights)
+
+ from .export_light import LightExporter
+ light_exporter = LightExporter()
+ for l in lights:
+ light_name = l.name+".light"
+ if light_name not in resources:
+ resources[light_name] = light_exporter.export_light(l)
+
+ lighting_name = scene.name+".lightn"
+ if lighting_name not in resources:
+ lighting_res = Resource(lighting_name, "lighting")
+ lighting_res.statements.append(Statement("ambient", *tuple(scene.ambient_light)))
+ for l in lights:
+ lighting_res.statements.append(lighting_res.create_reference_statement("light", resources[l.name+".light"]))
+
+ resources[lighting_name] = lighting_res
+
+ def export_sequence(self, scene, resources):
+ from .datafile import Resource, Statement, Token
+ seq_res = Resource(scene.name+".seq", "sequence")
+
+ if scene.use_hdr:
+ seq_res.statements.append(Statement("hdr", True))
+
+ content = scene
+ if scene.background_set:
+ content = resources[scene.name+".wrapper.scene"]
+
+ ss = Statement("step", "", "content")
+ ss.sub.append(Statement("depth_test", Token("LEQUAL")))
+ ss.sub.append(seq_res.create_reference_statement("lighting", resources[scene.name+".lightn"]))
+ ss.sub.append(seq_res.create_reference_statement("scene", content))
+ seq_res.statements.append(ss)
+
+ if scene.use_ao:
+ ss = Statement("ambient_occlusion")
+ ss.sub.append(Statement("occlusion_radius", scene.ao_distance))
+ ss.sub.append(Statement("samples", scene.ao_samples))
+ seq_res.statements.append(ss)
+
+ if scene.use_hdr:
+ seq_res.statements.append(Statement("bloom"))
+ ss = Statement("colorcurve")
+ ss.sub.append(Statement("exposure_adjust", scene.exposure))
+ ss.sub.append(Statement("srgb"))
+ seq_res.statements.append(ss)
+ else:
+ # Add a colorcurve with linear response to convert into sRGB color space
+ ss = Statement("colorcurve")
+ ss.sub.append(Statement("brightness_response", 1.0))
+ ss.sub.append(Statement("srgb"))
+ seq_res.statements.append(ss)
+
+ return seq_res