]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_object.py
Remove support for material atlases from the Blender exporter
[libs/gl.git] / blender / io_mspgl / export_object.py
1 import os
2
3 class ObjectExporter:
4         def collect_object_lods(self, obj):
5                 lods = [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))
10
11                 return lods
12
13         def export_object_resources(self, ctx, obj, resources):
14                 lods = self.collect_object_lods(obj)
15
16                 from .export_mesh import MeshExporter
17                 from .export_material import MaterialExporter
18                 from .mesh import create_mesh_from_object
19                 from .material import Material
20                 mesh_export = MeshExporter()
21                 material_export = MaterialExporter()
22
23                 ctx.set_slices(len(lods))
24                 for l in lods:
25                         lod_index = l.lod_index if l.lod_for_parent else 0
26                         task = ctx.next_slice("LOD {}".format(lod_index))
27
28                         if l.material_slots and l.material_slots[0].material:
29                                 material = l.material_slots[0].material
30                                 subtask = task.task(material, 0.1)
31                                 if material.render_mode!='EXTERNAL':
32                                         tech_name = material.name+".tech"
33                                         if tech_name not in resources:
34                                                 material = Material(material)
35                                                 material_export.export_technique_resources(subtask, material, resources)
36                                                 resources[tech_name] = material_export.export_technique(material, resources)
37                         elif "stub.tech" not in resources:
38                                 resources["stub.tech"] = self.export_stub_technique()
39
40                         mesh_name = l.data.name+".mesh"
41                         if mesh_name not in resources:
42                                 subtask = task.task(l.data, 1.0)
43                                 mesh = create_mesh_from_object(subtask, l)
44                                 mesh_res = mesh_export.export_mesh(subtask, mesh)
45                                 resources[mesh_name] = mesh_res
46
47         def export_object(self, obj, resources):
48                 if obj.type!='MESH':
49                         raise ValueError("Object {} is not a mesh".format(obj.name))
50
51                 lods = self.collect_object_lods(obj)
52
53                 from .datafile import Resource, Statement
54                 obj_res = Resource(obj.name+".object", "object")
55                 statements = obj_res.statements
56
57                 from .util import compute_bounding_sphere
58                 center, radius = compute_bounding_sphere([v.co for v in obj.data.vertices])
59                 statements.append(Statement("bounding_sphere_hint", *center, radius))
60
61                 prev_mesh = None
62                 prev_tech = None
63                 for i, l in enumerate(lods):
64                         lod_st = []
65
66                         if l.data.name!=prev_mesh:
67                                 mesh_res = resources[l.data.name+".mesh"]
68                                 lod_st.append(obj_res.create_reference_statement("mesh", mesh_res))
69
70                                 prev_mesh = l.data.name
71
72                         material = None
73                         if l.material_slots:
74                                 material = l.material_slots[0].material
75                         if material:
76                                 if material.render_mode=='EXTERNAL':
77                                         tech_name = material.technique
78                                 else:
79                                         tech_name = material.name+".tech"
80                         else:
81                                 tech_name = "stub.tech"
82
83                         if tech_name!=prev_tech:
84                                 if material and material.render_mode=='EXTERNAL':
85                                         lod_st.append(Statement("technique", material.technique))
86                                 else:
87                                         lod_st.append(obj_res.create_reference_statement("technique", resources[tech_name]))
88                                 prev_tech = tech_name
89
90                         if i>0:
91                                 st = Statement("level_of_detail", i)
92                                 st.sub = lod_st
93                                 statements.append(st)
94                         else:
95                                 statements += lod_st
96
97                 return obj_res
98
99         def export_stub_technique(self):
100                 from .datafile import Resource, Statement, Token
101                 tech_res = Resource("stub.tech", "technique")
102                 pass_st = Statement("method", "")
103                 tech_res.statements.append(pass_st)
104                 mat_st = Statement("material")
105                 pass_st.sub.append(mat_st)
106                 mat_st.sub.append(Statement("type", Token("basic")))
107                 return tech_res