6 self.show_progress = True
8 self.use_degen_tris = False
10 self.separate_mesh = False
11 self.separate_tech = False
12 self.shared_resources = True
13 self.export_lods = True
15 def compute_bounding_sphere(self, obj):
16 p1 = max(((v.co, v.co.length) for v in obj.data.vertices), key=lambda x:x[1])[0]
17 p2 = max(((v.co, (v.co-p1).length) for v in obj.data.vertices), key=lambda x:x[1])[0]
19 radius = (p1-p2).length/2
20 for v in obj.data.vertices:
23 center += d*(1-radius/d.length)/2
24 radius = (radius+d.length)/2
28 def collect_object_lods(self, obj):
31 lods += sorted([c for c in obj.children if c.lod_for_parent], key=(lambda l: l.lod_index))
32 for i, l in enumerate(lods):
33 if i>0 and l.lod_index!=i:
34 raise Exception("Inconsistent LOD indices")
38 def create_mesh_exporter(self):
39 from .export_mesh import MeshExporter
40 mesh_export = MeshExporter()
41 mesh_export.use_strips = self.use_strips
42 mesh_export.use_degen_tris = self.use_degen_tris
45 def create_material_exporter(self):
46 from .export_material import MaterialExporter
47 material_export = MaterialExporter()
48 material_export.textures = self.textures
49 return material_export
51 def export_to_file(self, context, out_fn):
52 obj = context.active_object
54 from .util import Progress
55 progress = Progress(self.show_progress and context)
57 path, base = os.path.split(out_fn)
58 base = os.path.splitext(base)[0]
61 self.export_object_resources(context, obj, resources, progress)
63 obj_res = self.export_object(context, obj, progress, resources=resources)
64 refs = obj_res.collect_references()
65 if not self.shared_resources:
68 ext = os.path.splitext(r.name)[1]
69 n = numbers.get(ext, 0)
71 r.name = "{}_{}{}".format(base, n, ext)
77 with open(os.path.join(path, r.name), "w") as out_file:
78 for s in r.statements:
79 s.write_to_file(out_file)
81 with open(out_fn, "w") as out_file:
82 for s in obj_res.statements:
83 s.write_to_file(out_file)
85 def export_object_resources(self, context, obj, resources, progress):
86 lods = self.collect_object_lods(obj)
88 from .mesh import create_mesh_from_object
89 mesh_export = self.create_mesh_exporter()
90 material_export = self.create_material_exporter()
92 for i, l in enumerate(lods):
93 lod_index = l.lod_index if l.lod_for_parent else 0
94 progress.push_task_slice("LOD {}".format(lod_index), i, len(lods))
96 mesh_name = l.data.name+".mesh"
97 if mesh_name not in resources:
98 mesh = create_mesh_from_object(context, l, progress)
99 mesh_res = mesh_export.export_mesh(context, mesh, progress)
100 resources[mesh_name] = mesh_res
102 if l.material_slots and l.material_slots[0].material:
103 material = l.material_slots[0].material
104 tech_name = material.name+".tech"
105 if tech_name not in resources:
106 material_export.export_technique_resources(material, resources)
107 resources[tech_name] = material_export.export_technique(material, resources=resources)
108 elif "stub.tech" not in resources:
109 resources["stub.tech"] = self.export_stub_technique()
113 def export_object(self, context, obj, progress, *, resources=None):
114 if resources is None:
116 self.export_object_resources(context, obj, resources, progress)
118 lods = self.collect_object_lods(obj)
120 from .datafile import Resource, Statement
121 obj_res = Resource(obj.name+".object")
122 statements = obj_res.statements
124 center, radius = self.compute_bounding_sphere(obj)
125 statements.append(Statement("bounding_sphere_hint", *center, radius))
129 for i, l in enumerate(lods):
132 if l.data.name!=prev_mesh:
133 mesh_res = resources[l.data.name+".mesh"]
134 if self.separate_mesh:
135 lod_st.append(obj_res.create_reference_statement("mesh", mesh_res))
137 lod_st.append(obj_res.create_embed_statement("mesh", mesh_res))
139 prev_mesh = l.data.name
143 material = l.material_slots[0].material
145 tech_res = resources[material.name+".tech"]
147 tech_res = resources["stub.tech"]
149 if tech_res.name!=prev_tech:
150 if material and material.technique and not material.inherit_tech:
151 lod_st.append(Statement("technique", material.technique))
152 elif self.separate_tech:
153 lod_st.append(obj_res.create_reference_statement("technique", tech_res))
155 lod_st.append(obj_res.create_embed_statement("technique", tech_res))
156 prev_tech = tech_res.name
159 st = Statement("level_of_detail", i)
161 statements.append(st)
165 progress.set_progress(1.0)
169 def export_stub_technique(self):
170 from .datafile import Resource, Statement
171 tech_res = Resource("stub.tech")
172 tech_res.statements.append(Statement("pass", ""))