X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=blender%2Fio_mspgl%2Fexport_object.py;h=54bde96a5d29962f2ad9bac4279d0fe77d67a2ce;hb=f241ccca7d0d3f3073a948377bd557a70aa8f1d2;hp=4bc6f450b330a3ca17159ddea6fa9a2ba1ab836e;hpb=6ee541fd39de1e1368134cb8a98a14f2f5ddd767;p=libs%2Fgl.git diff --git a/blender/io_mspgl/export_object.py b/blender/io_mspgl/export_object.py index 4bc6f450..54bde96a 100644 --- a/blender/io_mspgl/export_object.py +++ b/blender/io_mspgl/export_object.py @@ -1,36 +1,12 @@ import os import mathutils -def linear_to_srgb(l): - if l<0.0031308: - return 12.92*l - else: - return 1.055*(l**(1/2.4))-0.055 - -def get_colormap(srgb): - if srgb: - return linear_to_srgb - else: - return lambda x: x - -def image_name(i): - fp = i.filepath - if fp: - return os.path.split(fp)[1] - else: - return i.name - - class ObjectExporter: def __init__(self): self.show_progress = True - self.use_strips = True - self.use_degen_tris = False - self.textures = "REF" - self.separate_mesh = False - self.separate_tech = False + self.export_all = False + self.collection = False self.shared_resources = True - self.export_lods = True def compute_bounding_sphere(self, obj): p1 = max(((v.co, v.co.length) for v in obj.data.vertices), key=lambda x:x[1])[0] @@ -45,151 +21,169 @@ class ObjectExporter: return center, radius - def make_external_name(self, base_name, resource_name, ext, index): - if self.shared_resources: - return resource_name+ext - elif lod_index>0: - return "{}_lod{}{}".format(base_name, lod_index, ext) - else: - return base_name+ext + def collect_object_lods(self, obj): + lods = [obj] + lods += sorted([c for c in obj.children if c.lod_for_parent], key=(lambda l: l.lod_index)) + for i, l in enumerate(lods): + if i>0 and l.lod_index!=i: + raise Exception("Inconsistent LOD indices") + + return lods def create_mesh_exporter(self): from .export_mesh import MeshExporter mesh_export = MeshExporter() - mesh_export.use_strips = self.use_strips - mesh_export.use_degen_tris = self.use_degen_tris return mesh_export + def create_material_exporter(self): + from .export_material import MaterialExporter + material_export = MaterialExporter() + return material_export + + def create_material_atlas_exporter(self): + from .export_material import MaterialAtlasExporter + material_atlas_export = MaterialAtlasExporter() + return material_atlas_export + def export_to_file(self, context, out_fn): - obj = context.active_object + if self.export_all: + objs = [o for o in context.selected_objects if o.type=="MESH"] + else: + objs = [context.active_object] from .util import Progress progress = Progress(self.show_progress and context) path, base = os.path.split(out_fn) - base = os.path.splitext(base)[0] - - meshes = self.export_object_meshes(context, obj, progress, base_name=base) - if self.separate_mesh: - for name, st in meshes.items(): - with open(os.path.join(path, name), "w") as out_file: - for s in st: - s.write_to_file(out_file) - - if self.separate_tech: - lods = [obj] - if self.export_lods: - lods += [c for c in obj.children if c.lod_for_parent] - - for l in lods: - lod_index = l.lod_index if l.lod_for_parent else 0 - - material = None - if obj.material_slots: - material = obj.material_slots[0].material - - if not l.technique: - tech_name = self.make_external_name(base, material.name, ".tech", lod_index) - st = self.export_object_technique(l, base_name=base) - with open(os.path.join(path, tech_name), "w") as out_file: - for s in st: - s.write_to_file(out_file) - elif material and l.override_material: - mat_name = self.make_external_name(base, material.name, ".mat", lod_index) - st = self.export_material(material) - with open(os.path.join(path, mat_name), "w") as out_file: - for s in st: - s.write_to_file(out_file) - - statements = self.export_object(context, obj, progress, meshes=meshes, base_name=base) - - with open(out_fn, "w") as out_file: - for s in statements: - s.write_to_file(out_file) - - def export_object_meshes(self, context, obj, progress, *, base_name=None): - if base_name is None: - base_name = obj.name + base, ext = os.path.splitext(base) + + resources = {} + for i, obj in enumerate(objs): + if self.export_all: + out_fn = os.path.join(path, obj.name+ext) + + progress.push_task_slice(obj.name, i, len(objs)) + self.export_object_resources(context, obj, resources, None, progress) + + obj_res = self.export_object(context, obj, resources, progress) + refs = obj_res.collect_references() + if not self.shared_resources: + numbers = {} + for r in refs: + res_ext = os.path.splitext(r.name)[1] + n = numbers.get(res_ext, 0) + if n>0: + r.name = "{}_{}{}".format(base, n, res_ext) + else: + r.name = base+res_ext + numbers[res_ext] = n+1 - lods = [obj] - if self.export_lods: - lods += [c for c in obj.children if c.lod_for_parent] + if self.collection: + obj_res.write_collection(out_fn) + else: + for r in refs: + r.write_to_file(os.path.join(path, r.name)) + obj_res.write_to_file(out_fn) + + progress.pop_task() + + def export_object_resources(self, context, obj, resources, material_atlases, progress): + if material_atlases is None: + material_atlases = {} + + lods = self.collect_object_lods(obj) from .mesh import create_mesh_from_object + from .material import create_material_atlas mesh_export = self.create_mesh_exporter() - meshes = {} + material_export = self.create_material_exporter() + material_atlas_export = self.create_material_atlas_exporter() for i, l in enumerate(lods): lod_index = l.lod_index if l.lod_for_parent else 0 progress.push_task_slice("LOD {}".format(lod_index), i, len(lods)) - mesh_name = self.make_external_name(base_name, l.data.name, ".mesh", lod_index) - if mesh_name not in meshes: - mesh = create_mesh_from_object(context, l, progress) - meshes[mesh_name] = mesh_export.export_mesh(context, mesh, progress) + material_atlas = None + atlas_flags = [m.render_mode!='EXTERNAL' and m.material_atlas for m in l.data.materials if m] + if any(atlas_flags): + mmk = lambda m: m.shader if m.render_mode=='CUSTOM' else "" + material_atlas_key = mmk(l.data.materials[0]) + key_mismatch = any(mmk(m)!=material_atlas_key for m in l.data.materials) + if not all(atlas_flags) or key_mismatch: + raise Exception("Conflicting settings in object materials") + + if material_atlas_key in material_atlases: + material_atlas = material_atlases[material_atlas_key] + else: + material_atlas = create_material_atlas(context, l.data.materials[0]) + material_atlases[material_atlas_key] = material_atlas + + tech_name = "{}.tech".format(material_atlas.name) + if tech_name not in resources: + material_atlas_export.export_technique_resources(material_atlas, resources) + resources[tech_name] = material_atlas_export.export_technique(material_atlas, resources) + elif l.material_slots and l.material_slots[0].material: + material = l.material_slots[0].material + if material.render_mode!='EXTERNAL': + tech_name = material.name+".tech" + if tech_name not in resources: + material_export.export_technique_resources(material, resources) + resources[tech_name] = material_export.export_technique(material, resources) + elif "stub.tech" not in resources: + resources["stub.tech"] = self.export_stub_technique() + + mesh_name = l.data.name+".mesh" + if mesh_name not in resources: + mesh = create_mesh_from_object(context, l, material_atlas, progress) + mesh_res = mesh_export.export_mesh(context, mesh, progress) + resources[mesh_name] = mesh_res progress.pop_task() - return meshes - - def export_object(self, context, obj, progress, *, meshes=None, base_name=None): - if base_name is None: - base_name = obj.name + def export_object(self, context, obj, resources, progress): + if resources is None: + resources = {} + self.export_object_resources(context, obj, resources, None, progress) - if meshes is None: - meshes = self.export_object_meshes(context, obj, progress, base_name=base_name) - - lods = [obj] - for c in obj.children: - if c.lod_for_parent: - if c.lod_index>=len(lods): - lods += [None]*(c.lod_index+1-len(lods)) - lods[c.lod_index] = c + lods = self.collect_object_lods(obj) - from .datafile import Statement - statements = [] + from .datafile import Resource, Statement + obj_res = Resource(obj.name+".object", "object") + statements = obj_res.statements center, radius = self.compute_bounding_sphere(obj) statements.append(Statement("bounding_sphere_hint", *center, radius)) prev_mesh = None - prev_tech = (None, None) + prev_tech = None for i, l in enumerate(lods): lod_st = [] if l.data.name!=prev_mesh: - mesh_name = self.make_external_name(base_name, l.data.name, ".mesh", i) - if self.separate_mesh: - lod_st.append(Statement("mesh", mesh_name)) - else: - st = Statement("mesh") - st.sub = meshes[mesh_name] - lod_st.append(st) + mesh_res = resources[l.data.name+".mesh"] + lod_st.append(obj_res.create_reference_statement("mesh", mesh_res)) prev_mesh = l.data.name - mat_name = None - if l.material_slots and l.material_slots[0].material: - mat_name = l.material_slots[0].material.name - - tech = (l.technique, mat_name) - if tech!=prev_tech: - tech_name = self.make_external_name(base_name, mat_name or l.name, ".tech", i) - if l.technique: - if l.inherit_tech: - st = Statement("technique") - st.sub = self.export_object_technique(l, base_name=base_name) - lod_st.append(st) - else: - lod_st.append(Statement("technique", l.technique)) - elif self.separate_tech: - lod_st.append(Statement("technique", tech_name)) + material = None + if l.material_slots: + material = l.material_slots[0].material + if material: + if material.render_mode=='EXTERNAL': + tech_name = material.technique + elif material.material_atlas: + tech_name = "material_atlas_{}.tech".format(os.path.splitext(material.technique)[0]) else: - st = Statement("technique") - st.sub = self.export_object_technique(l, base_name=base_name) - lod_st.append(st) + tech_name = material.name+".tech" + else: + tech_name = "stub.tech" - prev_tech = tech + if tech_name!=prev_tech: + if material and material.render_mode=='EXTERNAL': + lod_st.append(Statement("technique", material.technique)) + else: + lod_st.append(obj_res.create_reference_statement("technique", resources[tech_name])) + prev_tech = tech_name if i>0: st = Statement("level_of_detail", i) @@ -200,86 +194,14 @@ class ObjectExporter: progress.set_progress(1.0) - return statements - - def export_object_technique(self, obj, *, base_name=None): - if base_name is None: - base_name = obj.name - - material = None - if obj.material_slots: - material = obj.material_slots[0].material - - from .datafile import Statement, Token - statements = [] - - if obj.technique: - if not obj.inherit_tech: - return [] - - st = Statement("inherit", obj.technique) - if material: - for slot in material.texture_slots: - if slot and slot.texture.type=="IMAGE": - name = image_name(slot.texture.image) - if slot.use_map_color_diffuse: - st.sub.append(Statement("texture", "diffuse_map", name)) - elif slot.use_map_normal: - st.sub.append(Statement("texture", "normal_map", name)) - if obj.override_material: - mat_name = self.make_external_name(base_name, material.name, ".mat", obj.lod_index) - st.sub.append(Statement("material", "surface", mat_name)) - statements.append(st) - - return statements + return obj_res + def export_stub_technique(self): + from .datafile import Resource, Statement + tech_res = Resource("stub.tech", "technique") pass_st = Statement("pass", "") - if material: - st = Statement("material") - st.sub = self.export_material(material) - pass_st.sub.append(st) - - if self.textures!="NONE": - diffuse_tex = None - for slot in material.texture_slots: - if slot and slot.texture.type=="IMAGE" and slot.use_map_color_diffuse: - diffuse_tex = slot.texture - break - - if diffuse_tex: - st = Statement("texunit", 0) - if self.textures=="INLINE": - ss = Statement("texture2d") - ss.sub.append(Statement("min_filter", Token("LINEAR"))) - ss.sub.append(Statement("storage", Token("RGBA"), tex.image.size[0], tex.image.size[1])) - texdata = "" - for p in tex.image.pixels: - texdata += "\\x%02X"%int(p*255) - ss.sub.append(Statement("raw_data", texdata)) - st.sub.append(ss) - elif tex.image: - st.sub.append(Statement("texture", image_name(tex.image))) - pass_st.sub.append(st) - statements.append(pass_st) - - return statements - - def export_material(self, material): - from .datafile import Statement - statements = [] - - cm = get_colormap(material.srgb_colors) - if any(s.use_map_color_diffuse for s in material.texture_slots if s): - statements.append(Statement("diffuse", 1.0, 1.0, 1.0, 1.0)) - amb = cm(material.ambient) - statements.append(Statement("ambient", amb, amb, amb, 1.0)) - else: - diff = material.diffuse_color*material.diffuse_intensity - statements.append(Statement("diffuse", cm(diff.r), cm(diff.g), cm(diff.b), 1.0)) - amb = diff*material.ambient - statements.append(Statement("ambient", cm(amb.r), cm(amb.g), cm(amb.b), 1.0)) - spec = material.specular_color*material.specular_intensity - statements.append(Statement("specular", cm(spec.r), cm(spec.g), cm(spec.g), 1.0)) - statements.append(Statement("shininess", material.specular_hardness)) - - return statements + tech_res.statements.append(pass_st) + mat_st = Statement("material") + pass_st.sub.append(mat_st) + mat_st.sub.append(Statement("basic")) + return tech_res