X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=blender%2Fio_mspgl%2Fexport_object.py;h=b911ed362bb242bc84f7f040a7ac25b90ac3e99a;hb=84269f7b598673b06757313d70934a1747045b69;hp=09703e58e902a1847e2b70d3e145117094296122;hpb=ec7e6f12a85a7dd65e57bf88b80c2d94601dd56f;p=libs%2Fgl.git diff --git a/blender/io_mspgl/export_object.py b/blender/io_mspgl/export_object.py index 09703e58..b911ed36 100644 --- a/blender/io_mspgl/export_object.py +++ b/blender/io_mspgl/export_object.py @@ -1,4 +1,5 @@ import os +import mathutils def linear_to_srgb(l): if l<0.0031308: @@ -13,6 +14,13 @@ def image_name(i): else: return i.name +def external_name(out_file, ext, index): + path, base = os.path.split(out_file.filename) + base = os.path.splitext(base)[0] + if index>0: + base += "_lod{}".format(index) + return path, base+ext + class ObjectExporter: def __init__(self): @@ -23,6 +31,7 @@ class ObjectExporter: self.separate_tech = False self.external_tech = True self.shared_tech = True + self.export_lods = True def export(self, context, out_file, objs=None, progress=None): if objs is None: @@ -30,35 +39,92 @@ class ObjectExporter: else: obj = objs[0] + 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 + from .outfile import open_output out_file = open_output(out_file) - path, base = os.path.split(out_file.filename) + p1 = max(((v.co, v.co.length) for v in obj.data.vertices), key=lambda x:x[1])[0] + p2 = max(((v.co, (v.co-p1).length) for v in obj.data.vertices), key=lambda x:x[1])[0] + center = (p1+p2)/2 + radius = (p1-p2).length/2 + for v in obj.data.vertices: + d = v.co-center + if d.length>radius: + center += d*(1-radius/d.length)/2 + radius = (radius+d.length)/2 + + out_file.write("bounding_sphere_hint", center[0], center[1], center[2], radius) + + prev_mesh = None + prev_tech = (None, None) + for i, l in enumerate(lods): + if i>0: + out_file.begin("level_of_detail", i) + objs = [l] + + same_mesh = (l.data.name==prev_mesh) + if i==0 or not same_mesh: + mesh = self.export_object_mesh(context, out_file, i, objs, progress) + prev_mesh = l.data.name + + same_tech = True + mat = None + if l.material_slots and l.material_slots[0].material: + mat = l.material_slots[0].material.name + if mat!=prev_tech[1]: + same_tech = False + if self.external_tech and l.technique!=prev_tech[0]: + same_tech = False + if i==0 or not same_tech: + self.export_object_technique(l, mesh, out_file, i) + prev_tech = (l.technique, mat) + + if i>0: + out_file.end() + + def export_object_mesh(self, context, out_file, lod_index, objs, progress): from .export_mesh import MeshExporter mesh_export = MeshExporter() for k, v in self.__dict__.items(): setattr(mesh_export, k, v) if self.separate_mesh: - base, ext = os.path.splitext(base) - mesh_out = open_output(os.path.join(path, base+".mesh")) + from .outfile import open_output + path, name = external_name(out_file, ".mesh", lod_index) + mesh_out = open_output(os.path.join(path, name)) mesh = mesh_export.export(context, mesh_out, objs, progress) - out_file.write("mesh", '"{}.mesh"'.format(base)) + out_file.write("mesh", '"{}"'.format(name)) else: out_file.begin("mesh") mesh = mesh_export.export(context, out_file, objs, progress) out_file.end() + return mesh + + def export_object_technique(self, obj, mesh, out_file, lod_index): if self.srgb_colors: self.colormap = linear_to_srgb else: self.colormap = lambda x: x + material = None + if obj.material_slots: + material = obj.material_slots[0].material + + from .outfile import open_output + path, name = external_name(out_file, ".tech", lod_index) + if self.external_tech and obj.technique: - if obj.inherit_tech and (obj.override_material or mesh.materials[0].texture_slots): + if obj.inherit_tech and material and (obj.override_material or material.texture_slots): out_file.begin("technique") out_file.begin("inherit", '"{}"'.format(obj.technique)) - for slot in mesh.materials[0].texture_slots: + 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: @@ -66,31 +132,28 @@ class ObjectExporter: elif slot.use_map_normal: out_file.write("texture", '"normal_map"', '"{}"'.format(name)) if obj.override_material: - mat_name = mesh.materials[0].name+".mat" + mat_name = material.name+".mat" mat_out = open_output(os.path.join(path, mat_name)) - self.export_material(mesh.materials[0], mat_out) + self.export_material(material, mat_out) out_file.write("material", '""', '"{}"'.format(mat_name)) out_file.end() out_file.end() else: out_file.write("technique", '"{}"'.format(obj.technique)) elif self.separate_tech: - if self.shared_tech and mesh.materials: - tech_name = mesh.materials[0].name+".tech" - else: - base, ext = os.path.splitext(base) - tech_name = base+".tech" - tech_out = open_output(os.path.join(path, tech_name)) - self.export_technique(mesh, tech_out) - out_file.write("technique", '"{}"'.format(tech_name)) + if self.shared_tech and material: + name = material.name+".tech" + tech_out = open_output(os.path.join(path, name)) + self.export_technique_definition(material, mesh, tech_out) + out_file.write("technique", '"{}"'.format(name)) else: out_file.begin("technique") - self.export_technique(mesh, out_file) + self.export_technique_definition(material, mesh, out_file) out_file.end() - def export_technique(self, mesh, out_file): + def export_technique_definition(self, material, mesh, out_file): out_file.begin("pass", '""') - if mesh.materials: + if material: cm = self.colormap if self.material_tex: @@ -116,11 +179,11 @@ class ObjectExporter: out_file.end() else: out_file.begin("material") - self.export_material(mesh.materials[0], out_file) + self.export_material(material, out_file) out_file.end() if self.textures!="NONE": - for slot in mesh.materials[0].texture_slots: + for slot in material.texture_slots: if not slot: continue