X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=blender%2Fio_mspgl%2Fexport_scene.py;h=7bb32efeeb927ab6c083d0bc387d7ccbf885e358;hp=b2c3a2e671b6f12c8ded80ee8a1dbee098b9d7ed;hb=2b2676392aff2eb6b38c3e463cc67f4d67a4ef8b;hpb=f241ccca7d0d3f3073a948377bd557a70aa8f1d2 diff --git a/blender/io_mspgl/export_scene.py b/blender/io_mspgl/export_scene.py index b2c3a2e6..7bb32efe 100644 --- a/blender/io_mspgl/export_scene.py +++ b/blender/io_mspgl/export_scene.py @@ -4,142 +4,143 @@ import os class SceneExporter: def __init__(self): self.selected_only = False - self.visible_collections = True - self.resource_collection = True + self.visible_only = True + self.collection = True self.skip_existing = True - self.show_progress = True def export_to_file(self, context, out_fn): - if self.selected_only: - objs = context.selected_objects - else: - objs = context.scene.objects - if self.visible_collections: - collections = [c.collection for c in context.view_layer.layer_collection.children if not (c.hide_viewport or c.collection.hide_viewport)] - objs = [o for o in objs if any((o.name in c.all_objects) for c in collections)] - objs = [o for o in objs 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)] - objs.sort(key=lambda x:x.name) - - 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 = [] - export_names = {} - used_names = set() - for o in objs: - if o.name in object_prototypes: - continue - - clones = [o] - for u in objs: - if u is o: - continue - if u.data.name!=o.data.name: - continue - if any(m1.name!=m2.name for m1, m2 in zip(o.material_slots, u.material_slots)): - continue - - clones.append(u) - - prefix = o.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[o.name+".object"] = prefix.strip(" .") - else: - used_names.add(o.name) - - unique_objects.append(o) - for c in clones: - object_prototypes[c.name] = o - - 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) - from .util import Progress - progress = Progress(self.show_progress and context) + progress = Progress(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, unique_objects, resources, progress) - for n, r in resources.items(): - if r.name in export_names: - r.name = export_names[r.name] + self.export_scene_resources(context, scene, resources, progress) + scene_res = self.export_scene(scene, resources) + progress.set_progress(1.0) - scene_res = self.export_scene(context, objs, resources, object_prototypes, progress) - refs = scene_res.collect_references() + path, base = os.path.split(out_fn) + base, ext = os.path.splitext(base) - if self.resource_collection: - filter = None + if self.collection: + existing = None if self.skip_existing: - filter = lambda r: not os.path.exists(os.path.join(path, r.name)) - scene_res.write_collection(os.path.join(path, base+"_resources.mdc"), exclude_self=True, filter=filter) + existing = lambda r: not os.path.exists(os.path.join(path, r.name)) + scene_res.write_collection(out_fn, filter=existing) else: - res_dir = os.path.join(path, base+"_resources") - if not os.path.exists(res_dir): - os.makedirs(res_dir) - for r in refs: - r.write_to_file(os.path.join(res_dir, r.name)) - - scene_res.write_to_file(out_fn) - - def export_scene_resources(self, context, objs, resources, progress): - from .export_object import ObjectExporter - object_export = ObjectExporter() - object_export.single_file = False + scene_res.write_to_file(out_fn) + for r in scene_res.collect_references(): + r.write_to_file(os.path.join(path, r.name)) - material_atlases = {} + def export_scene_resources(self, context, scene, resources, progress): + from .export import DataExporter + data_exporter = DataExporter() - for i, o in enumerate(objs): - progress.push_task_slice(o.name, i, len(objs)) - object_export.export_object_resources(context, o, resources, material_atlases, progress) - obj_name = o.name+".object" - resources[obj_name] = object_export.export_object(context, o, resources, progress) - progress.pop_task() + data_exporter.export_resources(context, scene.prototypes, resources, None, progress) - def export_scene(self, context, objs, resources, prototypes, progress): + def export_scene(self, scene, resources): from .datafile import Resource, Statement, Token - scene_res = Resource("scene.scene", "scene") + scene_res = Resource(scene.name+".scene", "scene") - scene_res.statements.append(Statement("type", Token(context.scene.scene_type.lower()))) + scene_res.statements.append(Statement("type", Token(scene.scene_type.lower()))) - for o in objs: - obj_res = resources[prototypes[o.name].name+".object"] - st = scene_res.create_reference_statement("object", obj_res, o.name) + 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 = o.matrix_world.to_translation() + loc = i.matrix_world.to_translation() ss.sub.append(Statement("position", *tuple(loc))) - quat = o.matrix_world.to_quaternion() - if o.rotation_mode in ('XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX'): + 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: ss.sub.append(Statement("rotation", quat.angle*180/math.pi, *tuple(quat.axis))) - scale = o.matrix_world.to_scale() + 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 + + 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("pass", "", "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