6 self.show_progress = True
8 self.use_degen_tris = False
9 self.use_textures = True
10 self.export_all = False
11 self.collection = 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.use_textures = self.use_textures
49 return material_export
51 def create_material_atlas_exporter(self):
52 from .export_material import MaterialAtlasExporter
53 material_atlas_export = MaterialAtlasExporter()
54 return material_atlas_export
56 def export_to_file(self, context, out_fn):
58 objs = [o for o in context.selected_objects if o.type=="MESH"]
60 objs = [context.active_object]
62 from .util import Progress
63 progress = Progress(self.show_progress and context)
65 path, base = os.path.split(out_fn)
66 base, ext = os.path.splitext(base)
69 for i, obj in enumerate(objs):
71 out_fn = os.path.join(path, obj.name+ext)
73 progress.push_task_slice(obj.name, i, len(objs))
74 self.export_object_resources(context, obj, resources, progress)
76 obj_res = self.export_object(context, obj, progress, resources=resources)
77 refs = obj_res.collect_references()
78 if not self.shared_resources:
81 res_ext = os.path.splitext(r.name)[1]
82 n = numbers.get(res_ext, 0)
84 r.name = "{}_{}{}".format(base, n, res_ext)
87 numbers[res_ext] = n+1
90 obj_res.write_collection(out_fn)
93 r.write_to_file(os.path.join(path, r.name))
94 obj_res.write_to_file(out_fn)
98 def export_object_resources(self, context, obj, resources, progress, material_atlases=None):
99 if material_atlases is None:
100 material_atlases = {}
102 lods = self.collect_object_lods(obj)
104 from .mesh import create_mesh_from_object
105 from .material import create_material_atlas
106 mesh_export = self.create_mesh_exporter()
107 material_export = self.create_material_exporter()
108 material_atlas_export = self.create_material_atlas_exporter()
110 for i, l in enumerate(lods):
111 lod_index = l.lod_index if l.lod_for_parent else 0
112 progress.push_task_slice("LOD {}".format(lod_index), i, len(lods))
114 material_atlas = None
115 atlas_flags = [m.render_mode!='EXTERNAL' and m.material_atlas for m in l.data.materials if m]
117 mmk = lambda m: m.shader if m.render_mode=='CUSTOM' else ""
118 material_atlas_key = mmk(l.data.materials[0])
119 key_mismatch = any(mmk(m)!=material_atlas_key for m in l.data.materials)
120 if not all(atlas_flags) or key_mismatch:
121 raise Exception("Conflicting settings in object materials")
123 if material_atlas_key in material_atlases:
124 material_atlas = material_atlases[material_atlas_key]
126 material_atlas = create_material_atlas(context, l.data.materials[0])
127 material_atlases[material_atlas_key] = material_atlas
129 tech_name = "{}.tech".format(material_atlas.name)
130 if tech_name not in resources:
131 material_atlas_export.export_technique_resources(material_atlas, resources)
132 resources[tech_name] = material_atlas_export.export_technique(material_atlas, resources=resources)
133 elif l.material_slots and l.material_slots[0].material:
134 material = l.material_slots[0].material
135 if material.render_mode!='EXTERNAL':
136 tech_name = material.name+".tech"
137 if tech_name not in resources:
138 material_export.export_technique_resources(material, resources)
139 resources[tech_name] = material_export.export_technique(material, resources=resources)
140 elif "stub.tech" not in resources:
141 resources["stub.tech"] = self.export_stub_technique()
143 mesh_name = l.data.name+".mesh"
144 if mesh_name not in resources:
145 mesh = create_mesh_from_object(context, l, progress, material_atlas=material_atlas)
146 mesh_res = mesh_export.export_mesh(context, mesh, progress)
147 resources[mesh_name] = mesh_res
151 def export_object(self, context, obj, progress, *, resources=None):
152 if resources is None:
154 self.export_object_resources(context, obj, resources, progress)
156 lods = self.collect_object_lods(obj)
158 from .datafile import Resource, Statement
159 obj_res = Resource(obj.name+".object", "object")
160 statements = obj_res.statements
162 center, radius = self.compute_bounding_sphere(obj)
163 statements.append(Statement("bounding_sphere_hint", *center, radius))
167 for i, l in enumerate(lods):
170 if l.data.name!=prev_mesh:
171 mesh_res = resources[l.data.name+".mesh"]
172 lod_st.append(obj_res.create_reference_statement("mesh", mesh_res))
174 prev_mesh = l.data.name
178 material = l.material_slots[0].material
180 if material.render_mode=='EXTERNAL':
181 tech_name = material.technique
182 elif material.material_atlas:
183 tech_name = "material_atlas_{}.tech".format(os.path.splitext(material.technique)[0])
185 tech_name = material.name+".tech"
187 tech_name = "stub.tech"
189 if tech_name!=prev_tech:
190 if material and material.render_mode=='EXTERNAL':
191 lod_st.append(Statement("technique", material.technique))
193 lod_st.append(obj_res.create_reference_statement("technique", resources[tech_name]))
194 prev_tech = tech_name
197 st = Statement("level_of_detail", i)
199 statements.append(st)
203 progress.set_progress(1.0)
207 def export_stub_technique(self):
208 from .datafile import Resource, Statement
209 tech_res = Resource("stub.tech", "technique")
210 pass_st = Statement("pass", "")
211 tech_res.statements.append(pass_st)
212 mat_st = Statement("material")
213 pass_st.sub.append(mat_st)
214 mat_st.sub.append(Statement("basic"))