5 def collect_object_lods(self, obj):
7 lods += sorted([c for c in obj.children if c.lod_for_parent], key=(lambda l: l.lod_index))
8 for i, l in enumerate(lods):
9 if i>0 and l.lod_index!=i:
10 raise Exception("Invalid configuration on object {}: Inconsistent LOD indices".format(obj.name))
14 def export_object_resources(self, context, obj, resources, material_atlases, progress):
15 if material_atlases is None:
18 lods = self.collect_object_lods(obj)
20 from .export_mesh import MeshExporter
21 from .export_material import MaterialAtlasExporter, MaterialExporter
22 from .mesh import create_mesh_from_object
23 from .material import Material, create_material_atlas
24 mesh_export = MeshExporter()
25 material_export = MaterialExporter()
26 material_atlas_export = MaterialAtlasExporter()
28 for i, l in enumerate(lods):
29 lod_index = l.lod_index if l.lod_for_parent else 0
30 progress.push_task_slice("LOD {}".format(lod_index), i, len(lods))
33 atlas_flags = [m.render_mode!='EXTERNAL' and m.material_atlas for m in l.data.materials if m]
35 mmk = lambda m: m.shader if m.render_mode=='CUSTOM' else ""
36 material_atlas_key = mmk(l.data.materials[0])
37 key_mismatch = any(mmk(m)!=material_atlas_key for m in l.data.materials)
38 if not all(atlas_flags) or key_mismatch:
39 raise Exception("Invalid configuration on object {}: Mixed material atlas state")
41 if material_atlas_key in material_atlases:
42 material_atlas = material_atlases[material_atlas_key]
44 material_atlas = create_material_atlas(context, l.data.materials[0])
45 material_atlases[material_atlas_key] = material_atlas
47 tech_name = "{}.tech".format(material_atlas.name)
48 if tech_name not in resources:
49 material_atlas_export.export_technique_resources(material_atlas, resources)
50 resources[tech_name] = material_atlas_export.export_technique(material_atlas, resources)
51 elif l.material_slots and l.material_slots[0].material:
52 material = l.material_slots[0].material
53 if material.render_mode!='EXTERNAL':
54 tech_name = material.name+".tech"
55 if tech_name not in resources:
56 material = Material(material)
57 material_export.export_technique_resources(material, resources)
58 resources[tech_name] = material_export.export_technique(material, resources)
59 elif "stub.tech" not in resources:
60 resources["stub.tech"] = self.export_stub_technique()
62 mesh_name = l.data.name+".mesh"
63 if mesh_name not in resources:
64 mesh = create_mesh_from_object(context, l, material_atlas, progress)
65 mesh_res = mesh_export.export_mesh(context, mesh, progress)
66 resources[mesh_name] = mesh_res
70 def export_object(self, obj, resources, progress):
72 raise ValueError("Object {} is not a mesh".format(obj.name))
74 lods = self.collect_object_lods(obj)
76 from .datafile import Resource, Statement
77 obj_res = Resource(obj.name+".object", "object")
78 statements = obj_res.statements
80 from .util import compute_bounding_sphere
81 center, radius = compute_bounding_sphere([v.co for v in obj.data.vertices])
82 statements.append(Statement("bounding_sphere_hint", *center, radius))
86 for i, l in enumerate(lods):
89 if l.data.name!=prev_mesh:
90 mesh_res = resources[l.data.name+".mesh"]
91 lod_st.append(obj_res.create_reference_statement("mesh", mesh_res))
93 prev_mesh = l.data.name
97 material = l.material_slots[0].material
99 if material.render_mode=='EXTERNAL':
100 tech_name = material.technique
101 elif material.material_atlas:
102 tech_name = "material_atlas_{}.tech".format(os.path.splitext(material.technique)[0])
104 tech_name = material.name+".tech"
106 tech_name = "stub.tech"
108 if tech_name!=prev_tech:
109 if material and material.render_mode=='EXTERNAL':
110 lod_st.append(Statement("technique", material.technique))
112 lod_st.append(obj_res.create_reference_statement("technique", resources[tech_name]))
113 prev_tech = tech_name
116 st = Statement("level_of_detail", i)
118 statements.append(st)
122 progress.set_progress(1.0)
126 def export_stub_technique(self):
127 from .datafile import Resource, Statement, Token
128 tech_res = Resource("stub.tech", "technique")
129 pass_st = Statement("method", "")
130 tech_res.statements.append(pass_st)
131 mat_st = Statement("material")
132 pass_st.sub.append(mat_st)
133 mat_st.sub.append(Statement("type", Token("basic")))