]> git.tdb.fi Git - libs/gl.git/blobdiff - blender/io_mspgl/export_scene.py
Further refactoring of instance handling in the Blender exporter
[libs/gl.git] / blender / io_mspgl / export_scene.py
index d51fb4718a0fdf1afda48a9587bf670dcbe3732e..a42d8180c1d3bc0255285935617787c6a4b74a78 100644 (file)
 import math
 import os
+import itertools
+import mathutils
 
 class SceneExporter:
-       def __init__(self):
-               self.resource_collection = True
-               self.show_progress = True
+       def export_to_file(self, ctx, out_fn, *, selected_only=False, visible_only=True, collection=True, skip_existing=True):
+               from .scene import create_scene_from_current
+               task = ctx.task("Preparing scene", 0.1)
+               scene = create_scene_from_current(task, selected_only=selected_only, visible_only=visible_only)
 
-       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]
+               resources = {}
+               task = ctx.task("Exporting resources", 0.9)
+               self.export_scene_resources(task, scene, resources)
+               task = ctx.task(scene, 1.0)
+               scene_res = self.export_scene(scene, resources)
 
-               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)
 
-               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
-
-               from .util import Progress
-               progress = Progress(self.show_progress and 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_slice(o.name, i, len(objs))
-                               object_export.export(context, res_out, o, progress)
-                               progress.pop_task()
-                               res_out.end()
+               task = ctx.task("Writing files", 1.0)
+               if collection:
+                       existing = None
+                       if 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))
+
+       def export_scene_resources(self, ctx, scene, resources):
+               from .export import DataExporter
+               data_exporter = DataExporter()
+
+               data_exporter.export_resources(ctx, [p.object for p in scene.prototypes], resources)
+
+       def export_scene(self, scene, resources):
+               from .datafile import Resource, Statement, Token
+               scene_res = Resource(scene.name+".scene", "scene")
+
+               if scene.background_set or (scene.instances and scene.blended_instances):
+                       scene_res.statements.append(Statement("type", Token("ordered")))
+                       if scene.background_set:
+                               scene_res.statements.append(scene_res.create_reference_statement("scene", resources[scene.background_set.name+".scene"]))
+
+                       if scene.instances:
+                               st = Statement("scene")
+                               st.sub.append(Statement("type", Token("simple")))
+                               self.add_instances(scene_res, st.sub, scene.instances, resources)
+                               scene_res.statements.append(st)
+
+                       if scene.blended_instances:
+                               st = Statement("scene")
+                               st.sub.append(Statement("type", Token("zsorted")))
+                               self.add_instances(scene_res, st.sub, scene.blended_instances, resources)
+                               scene_res.statements.append(st)
+               else:
+                       scene_type = "zsorted" if scene.blended_instances else "simple"
+                       scene_res.statements.append(Statement("type", Token(scene_type)))
+
+                       self.add_instances(scene_res, scene_res.statements, scene.instances, resources)
+                       self.add_instances(scene_res, scene_res.statements, scene.blended_instances, resources)
+
+               return scene_res
+
+       def add_instances(self, scene_res, statements, instances, resources):
+               from .datafile import Statement
+
+               for i in instances:
+                       obj_res = resources[i.prototype.name+".object"]
+                       st = scene_res.create_reference_statement("object", obj_res)
+                       if i.name:
+                               st.append(i.name)
+
+                       st.sub.append(self.create_transform_statement(i))
+                       statements.append(st)
+
+       def create_transform_statement(self, instance):
+               from .datafile import Statement
+
+               st = Statement("transform")
+
+               loc = instance.matrix_world.to_translation()
+               st.sub.append(Statement("position", *tuple(loc)))
+
+               quat = instance.matrix_world.to_quaternion()
+               if instance.rotation_mode in ('XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX'):
+                       angles = [a*180/math.pi for a in quat.to_euler()]
+                       st.sub.append(Statement("euler", *angles));
+               else:
+                       st.sub.append(Statement("rotation", quat.angle*180/math.pi, *tuple(quat.axis)))
+
+               scale = instance.matrix_world.to_scale()
+               st.sub.append(Statement("scale", *tuple(scale)))
+
+               return st
+
+       def export_sequence_resources(self, scene, resources):
+               from .datafile import Resource, Statement, Token
+
+               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))
+
+               self.add_clear(seq_res.statements, (0.0, 0.0, 0.0, 0.0), 1.0)
+
+               scene_res = resources[scene.name+".scene"]
+               seq_res.statements.append(seq_res.create_reference_statement("renderable", "content", scene_res))
+
+               lighting_res = resources[scene.name+".lightn"]
+
+               any_opaque = False
+               any_blended = False
+               use_ibl = False
+               use_shadow = False
+               shadowed_lights = []
+               shadow_casters = []
+               s = scene
+               while s:
+                       if s.instances:
+                               any_opaque = True
+                       if s.blended_instances:
+                               any_blended = True
+                       if s.use_ibl:
+                               use_ibl = True
+                       if s.use_shadow:
+                               use_shadow = True
+                       shadowed_lights += [l.data for l in s.lights if l.data.use_shadow]
+                       for i in itertools.chain(s.instances, s.blended_instances):
+                               o = i.prototype.object
+                               if o.material_slots and o.material_slots[0].material and o.material_slots[0].material.shadow_method!='NONE':
+                                       shadow_casters.append(i)
+                       s = s.background_set
+
+               shadowed_lights.sort(key=lambda l:l.shadow_map_size, reverse=True)
+
+               main_tags = []
+               if any_opaque:
+                       main_tags.append("")
+               if any_blended:
+                       main_tags.append("blended")
+
+               content = "content"
+               if use_ibl and scene.use_sky:
+                       self.add_auxiliary_sequence(seq_res, "environment", "sky", ((0.0, 0.0, 0.0, 0.0), 1.0), main_tags, lighting_res)
+
+                       st = Statement("effect", "environment")
+                       st.sub.append(Statement("type", Token("environment_map")))
+                       st.sub.append(Statement("size", 32))
+                       st.sub.append(Statement("roughness_levels", 2))
+                       st.sub.append(Statement("fixed_position", 0.0, 0.0, 0.0))
+                       st.sub.append(Statement("content", content))
+                       st.sub.append(Statement("environment", "environment_sequence"))
+
+                       seq_res.statements.append(st)
+                       content = "environment"
+
+               if scene.use_sky:
+                       st = Statement("effect", "sky")
+                       st.sub.append(Statement("type", Token("sky")))
+                       st.sub.append(seq_res.create_reference_statement("sun", resources[scene.sun_light.name+".light"]))
+                       st.sub.append(Statement("content", content))
+
+                       seq_res.statements.append(st)
+                       content = "sky"
+
+               if use_shadow:
+                       self.add_auxiliary_sequence(seq_res, "shadow", "content", (None, 1.0), ["shadow"], None)
+                       self.add_auxiliary_sequence(seq_res, "thsm", "content", (None, 1.0), ["shadow_thsm"], None)
+
+                       st = Statement("effect", "shadow_map")
+                       st.sub.append(Statement("type", Token("shadow_map")))
+                       st.sub.append(Statement("enable_for_method", "blended"))
+                       st.sub.append(Statement("size", *self.compute_shadowmap_size(shadowed_lights)))
+                       target, radius = self.compute_bounding_sphere(shadow_casters)
+                       st.sub.append(Statement("target", *target))
+                       st.sub.append(Statement("radius", radius))
+                       st.sub.append(Statement("content", content))
+                       st.sub.append(seq_res.create_reference_statement("lighting", lighting_res))
+                       for l in shadowed_lights:
+                               ss = seq_res.create_reference_statement("light", resources[l.name+".light"])
+                               ss.sub.append(Statement("size", int(l.shadow_map_size)))
+                               shadow_caster = "thsm_sequence" if l.type=='POINT' else "shadow_sequence"
+                               ss.sub.append(Statement("shadow_caster", shadow_caster))
+                               st.sub.append(ss)
+
+                       seq_res.statements.append(st)
+                       content = "shadow_map"
+
+               self.add_content_steps(seq_res, content, lighting_res, main_tags)
+
+               if scene.use_ao:
+                       ss = Statement("postprocessor")
+                       ss.sub.append(Statement("type", Token("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:
+                       ss = Statement("postprocessor")
+                       ss.sub.append(Statement("type", Token("bloom")))
+                       seq_res.statements.append(ss)
+
+                       ss = Statement("postprocessor")
+                       ss.sub.append(Statement("type", Token("colorcurve")))
+                       ss.sub.append(Statement("exposure_adjust", scene.exposure))
+                       ss.sub.append(Statement("srgb"))
+                       seq_res.statements.append(ss)
                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_slice(o.name, i, 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:]
-                       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()
+                       # Add a colorcurve with linear response to convert into sRGB color space
+                       ss = Statement("postprocessor")
+                       ss.sub.append(Statement("type", Token("colorcurve")))
+                       ss.sub.append(Statement("brightness_response", 1.0))
+                       ss.sub.append(Statement("srgb"))
+                       seq_res.statements.append(ss)
+
+               return seq_res
+
+       def add_clear(self, statements, color, depth):
+               from .datafile import Statement
+
+               st = Statement("clear")
+               if color is not None:
+                       st.sub.append(Statement("color", *color))
+               if depth is not None:
+                       st.sub.append(Statement("depth", depth))
+               statements.append(st)
+
+       def add_content_steps(self, seq_res, renderable, lighting, tags):
+               from .datafile import Statement, Token
+
+               for t in tags:
+                       st = Statement("step", t, renderable)
+                       st.sub.append(Statement("depth_test", Token("LEQUAL")))
+                       if lighting:
+                               st.sub.append(seq_res.create_reference_statement("lighting", lighting))
+                       seq_res.statements.append(st)
+
+       def add_auxiliary_sequence(self, seq_res, aux_name, content, clear_values, step_tags, lighting):
+               seq_name = os.path.splitext(seq_res.name)[0]
+
+               from .datafile import Resource, Statement
+               aux_seq_res = Resource("{}_{}.seq".format(seq_name, aux_name), "sequence")
+               self.add_clear(aux_seq_res.statements, *clear_values)
+               aux_seq_res.statements.append(Statement("renderable", "content"))
+               self.add_content_steps(aux_seq_res, "content", lighting, step_tags)
+
+               st = seq_res.create_reference_statement("sequence", aux_name+"_sequence", aux_seq_res)
+               st.sub.append(Statement("renderable", "content", content))
+               seq_res.statements.append(st)
+
+       def compute_shadowmap_size(self, lights):
+               total_area = 0
+               for l in lights:
+                       s = int(l.shadow_map_size)
+                       total_area += s*s
+
+               size = 1
+               while size*size<total_area:
+                       size *= 2
+               if size*size>total_area*2:
+                       return (size, size//2)
+               else:
+                       return (size, size)
+
+       def compute_bounding_sphere(self, instances):
+               points = []
+               for i in instances:
+                       points += [i.matrix_world@mathutils.Vector(c) for c in i.prototype.object.bound_box]
+
+               from .util import compute_bounding_sphere
+               return compute_bounding_sphere(points)