]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_object.py
Redesign progress and error reporting in the Blender exporter
[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, ctx, obj, resources, material_atlases):
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                 ctx.set_slices(len(lods))
29                 for l in lods:
30                         lod_index = l.lod_index if l.lod_for_parent else 0
31                         task = ctx.next_slice("LOD {}".format(lod_index))
32
33                         material_atlas = None
34                         atlas_flags = [m.render_mode!='EXTERNAL' and m.material_atlas for m in l.data.materials if m]
35                         if any(atlas_flags):
36                                 mmk = lambda m: m.shader if m.render_mode=='CUSTOM' else ""
37                                 material_atlas_key = mmk(l.data.materials[0])
38                                 key_mismatch = any(mmk(m)!=material_atlas_key for m in l.data.materials)
39                                 if not all(atlas_flags) or key_mismatch:
40                                         raise Exception("Invalid configuration on object {}: Mixed material atlas state")
41
42                                 if material_atlas_key in material_atlases:
43                                         material_atlas = material_atlases[material_atlas_key]
44                                 else:
45                                         material_atlas = create_material_atlas(task.context, l.data.materials[0])
46                                         material_atlases[material_atlas_key] = material_atlas
47
48                                 tech_name = "{}.tech".format(material_atlas.name)
49                                 if tech_name not in resources:
50                                         material_atlas_export.export_technique_resources(material_atlas, resources)
51                                         resources[tech_name] = material_atlas_export.export_technique(material_atlas, resources)
52                         elif l.material_slots and l.material_slots[0].material:
53                                 material = l.material_slots[0].material
54                                 subtask = task.task(material, 0.1)
55                                 if material.render_mode!='EXTERNAL':
56                                         tech_name = material.name+".tech"
57                                         if tech_name not in resources:
58                                                 material = Material(material)
59                                                 material_export.export_technique_resources(subtask, material, resources)
60                                                 resources[tech_name] = material_export.export_technique(material, resources)
61                         elif "stub.tech" not in resources:
62                                 resources["stub.tech"] = self.export_stub_technique()
63
64                         mesh_name = l.data.name+".mesh"
65                         if mesh_name not in resources:
66                                 subtask = task.task(l, 1.0)
67                                 mesh = create_mesh_from_object(subtask, l, material_atlas)
68                                 mesh_res = mesh_export.export_mesh(subtask, mesh)
69                                 resources[mesh_name] = mesh_res
70
71         def export_object(self, obj, resources):
72                 if obj.type!='MESH':
73                         raise ValueError("Object {} is not a mesh".format(obj.name))
74
75                 lods = self.collect_object_lods(obj)
76
77                 from .datafile import Resource, Statement
78                 obj_res = Resource(obj.name+".object", "object")
79                 statements = obj_res.statements
80
81                 from .util import compute_bounding_sphere
82                 center, radius = compute_bounding_sphere([v.co for v in obj.data.vertices])
83                 statements.append(Statement("bounding_sphere_hint", *center, radius))
84
85                 prev_mesh = None
86                 prev_tech = None
87                 for i, l in enumerate(lods):
88                         lod_st = []
89
90                         if l.data.name!=prev_mesh:
91                                 mesh_res = resources[l.data.name+".mesh"]
92                                 lod_st.append(obj_res.create_reference_statement("mesh", mesh_res))
93
94                                 prev_mesh = l.data.name
95
96                         material = None
97                         if l.material_slots:
98                                 material = l.material_slots[0].material
99                         if material:
100                                 if material.render_mode=='EXTERNAL':
101                                         tech_name = material.technique
102                                 elif material.material_atlas:
103                                         tech_name = "material_atlas_{}.tech".format(os.path.splitext(material.technique)[0])
104                                 else:
105                                         tech_name = material.name+".tech"
106                         else:
107                                 tech_name = "stub.tech"
108
109                         if tech_name!=prev_tech:
110                                 if material and material.render_mode=='EXTERNAL':
111                                         lod_st.append(Statement("technique", material.technique))
112                                 else:
113                                         lod_st.append(obj_res.create_reference_statement("technique", resources[tech_name]))
114                                 prev_tech = tech_name
115
116                         if i>0:
117                                 st = Statement("level_of_detail", i)
118                                 st.sub = lod_st
119                                 statements.append(st)
120                         else:
121                                 statements += lod_st
122
123                 return obj_res
124
125         def export_stub_technique(self):
126                 from .datafile import Resource, Statement, Token
127                 tech_res = Resource("stub.tech", "technique")
128                 pass_st = Statement("method", "")
129                 tech_res.statements.append(pass_st)
130                 mat_st = Statement("material")
131                 pass_st.sub.append(mat_st)
132                 mat_st.sub.append(Statement("type", Token("basic")))
133                 return tech_res