4 def collect_object_lods(self, obj):
6 lods += sorted([c for c in obj.children if c.lod_for_parent], key=(lambda l: l.lod_index))
7 for i, l in enumerate(lods):
8 if i>0 and l.lod_index!=i:
9 raise Exception("Invalid configuration on object {}: Inconsistent LOD indices".format(obj.name))
13 def export_object_resources(self, ctx, obj, resources, material_atlases):
14 if material_atlases is None:
17 lods = self.collect_object_lods(obj)
19 from .export_mesh import MeshExporter
20 from .export_material import MaterialAtlasExporter, MaterialExporter
21 from .mesh import create_mesh_from_object
22 from .material import Material, create_material_atlas
23 mesh_export = MeshExporter()
24 material_export = MaterialExporter()
25 material_atlas_export = MaterialAtlasExporter()
27 ctx.set_slices(len(lods))
29 lod_index = l.lod_index if l.lod_for_parent else 0
30 task = ctx.next_slice("LOD {}".format(lod_index))
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(task.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 subtask = task.task(material, 0.1)
54 if material.render_mode!='EXTERNAL':
55 tech_name = material.name+".tech"
56 if tech_name not in resources:
57 material = Material(material)
58 material_export.export_technique_resources(subtask, material, resources)
59 resources[tech_name] = material_export.export_technique(material, resources)
60 elif "stub.tech" not in resources:
61 resources["stub.tech"] = self.export_stub_technique()
63 mesh_name = l.data.name+".mesh"
64 if mesh_name not in resources:
65 subtask = task.task(l, 1.0)
66 mesh = create_mesh_from_object(subtask, l, material_atlas)
67 mesh_res = mesh_export.export_mesh(subtask, mesh)
68 resources[mesh_name] = mesh_res
70 def export_object(self, obj, resources):
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)
124 def export_stub_technique(self):
125 from .datafile import Resource, Statement, Token
126 tech_res = Resource("stub.tech", "technique")
127 pass_st = Statement("method", "")
128 tech_res.statements.append(pass_st)
129 mat_st = Statement("material")
130 pass_st.sub.append(mat_st)
131 mat_st.sub.append(Statement("type", Token("basic")))