]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_object.py
Use low-level handles when computing descriptor set hashes
[libs/gl.git] / blender / io_mspgl / export_object.py
1 import os
2 import mathutils
3
4 class ObjectExporter:
5         def collect_object_lods(self, obj):
6                 lods = [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))
11
12                 return lods
13
14         def export_object_resources(self, context, obj, resources, material_atlases, progress):
15                 if material_atlases is None:
16                         material_atlases = {}
17
18                 lods = self.collect_object_lods(obj)
19
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()
27
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))
31
32                         material_atlas = None
33                         atlas_flags = [m.render_mode!='EXTERNAL' and m.material_atlas for m in l.data.materials if m]
34                         if any(atlas_flags):
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")
40
41                                 if material_atlas_key in material_atlases:
42                                         material_atlas = material_atlases[material_atlas_key]
43                                 else:
44                                         material_atlas = create_material_atlas(context, l.data.materials[0])
45                                         material_atlases[material_atlas_key] = material_atlas
46
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()
61
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
67
68                         progress.pop_task()
69
70         def export_object(self, obj, resources, progress):
71                 if obj.type!='MESH':
72                         raise ValueError("Object {} is not a mesh".format(obj.name))
73
74                 lods = self.collect_object_lods(obj)
75
76                 from .datafile import Resource, Statement
77                 obj_res = Resource(obj.name+".object", "object")
78                 statements = obj_res.statements
79
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))
83
84                 prev_mesh = None
85                 prev_tech = None
86                 for i, l in enumerate(lods):
87                         lod_st = []
88
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))
92
93                                 prev_mesh = l.data.name
94
95                         material = None
96                         if l.material_slots:
97                                 material = l.material_slots[0].material
98                         if 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])
103                                 else:
104                                         tech_name = material.name+".tech"
105                         else:
106                                 tech_name = "stub.tech"
107
108                         if tech_name!=prev_tech:
109                                 if material and material.render_mode=='EXTERNAL':
110                                         lod_st.append(Statement("technique", material.technique))
111                                 else:
112                                         lod_st.append(obj_res.create_reference_statement("technique", resources[tech_name]))
113                                 prev_tech = tech_name
114
115                         if i>0:
116                                 st = Statement("level_of_detail", i)
117                                 st.sub = lod_st
118                                 statements.append(st)
119                         else:
120                                 statements += lod_st
121
122                 progress.set_progress(1.0)
123
124                 return obj_res
125
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")))
134                 return tech_res