]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_object.py
Rename RenderPass to RenderMethod
[libs/gl.git] / blender / io_mspgl / export_object.py
1 import os
2 import mathutils
3
4 class ObjectExporter:
5         def compute_bounding_sphere(self, obj):
6                 p1 = max(((v.co, v.co.length) for v in obj.data.vertices), key=lambda x:x[1])[0]
7                 p2 = max(((v.co, (v.co-p1).length) for v in obj.data.vertices), key=lambda x:x[1])[0]
8                 center = (p1+p2)/2
9                 radius = (p1-p2).length/2
10                 for v in obj.data.vertices:
11                         d = v.co-center
12                         if d.length>radius:
13                                 center += d*(1-radius/d.length)/2
14                                 radius = (radius+d.length)/2
15
16                 return center, radius
17
18         def collect_object_lods(self, obj):
19                 lods = [obj]
20                 lods += sorted([c for c in obj.children if c.lod_for_parent], key=(lambda l: l.lod_index))
21                 for i, l in enumerate(lods):
22                         if i>0 and l.lod_index!=i:
23                                 raise Exception("Invalid configuration on object {}: Inconsistent LOD indices".format(obj.name))
24
25                 return lods
26
27         def create_mesh_exporter(self):
28                 from .export_mesh import MeshExporter
29                 mesh_export = MeshExporter()
30                 return mesh_export
31
32         def create_material_exporter(self):
33                 from .export_material import MaterialExporter
34                 material_export = MaterialExporter()
35                 return material_export
36
37         def create_material_atlas_exporter(self):
38                 from .export_material import MaterialAtlasExporter
39                 material_atlas_export = MaterialAtlasExporter()
40                 return material_atlas_export
41
42         def export_object_resources(self, context, obj, resources, material_atlases, progress):
43                 if material_atlases is None:
44                         material_atlases = {}
45
46                 lods = self.collect_object_lods(obj)
47
48                 from .mesh import create_mesh_from_object
49                 from .material import create_material_atlas
50                 mesh_export = self.create_mesh_exporter()
51                 material_export = self.create_material_exporter()
52                 material_atlas_export = self.create_material_atlas_exporter()
53
54                 for i, l in enumerate(lods):
55                         lod_index = l.lod_index if l.lod_for_parent else 0
56                         progress.push_task_slice("LOD {}".format(lod_index), i, len(lods))
57
58                         material_atlas = None
59                         atlas_flags = [m.render_mode!='EXTERNAL' and m.material_atlas for m in l.data.materials if m]
60                         if any(atlas_flags):
61                                 mmk = lambda m: m.shader if m.render_mode=='CUSTOM' else ""
62                                 material_atlas_key = mmk(l.data.materials[0])
63                                 key_mismatch = any(mmk(m)!=material_atlas_key for m in l.data.materials)
64                                 if not all(atlas_flags) or key_mismatch:
65                                         raise Exception("Invalid configuration on object {}: Mixed material atlas state")
66
67                                 if material_atlas_key in material_atlases:
68                                         material_atlas = material_atlases[material_atlas_key]
69                                 else:
70                                         material_atlas = create_material_atlas(context, l.data.materials[0])
71                                         material_atlases[material_atlas_key] = material_atlas
72
73                                 tech_name = "{}.tech".format(material_atlas.name)
74                                 if tech_name not in resources:
75                                         material_atlas_export.export_technique_resources(material_atlas, resources)
76                                         resources[tech_name] = material_atlas_export.export_technique(material_atlas, resources)
77                         elif l.material_slots and l.material_slots[0].material:
78                                 material = l.material_slots[0].material
79                                 if material.render_mode!='EXTERNAL':
80                                         tech_name = material.name+".tech"
81                                         if tech_name not in resources:
82                                                 material_export.export_technique_resources(material, resources)
83                                                 resources[tech_name] = material_export.export_technique(material, resources)
84                         elif "stub.tech" not in resources:
85                                 resources["stub.tech"] = self.export_stub_technique()
86
87                         mesh_name = l.data.name+".mesh"
88                         if mesh_name not in resources:
89                                 mesh = create_mesh_from_object(context, l, material_atlas, progress)
90                                 mesh_res = mesh_export.export_mesh(context, mesh, progress)
91                                 resources[mesh_name] = mesh_res
92
93                         progress.pop_task()
94
95         def export_object(self, obj, resources, progress):
96                 if obj.type!='MESH':
97                         raise ValueError("Object {} is not a mesh".format(obj.name))
98
99                 lods = self.collect_object_lods(obj)
100
101                 from .datafile import Resource, Statement
102                 obj_res = Resource(obj.name+".object", "object")
103                 statements = obj_res.statements
104
105                 center, radius = self.compute_bounding_sphere(obj)
106                 statements.append(Statement("bounding_sphere_hint", *center, radius))
107
108                 prev_mesh = None
109                 prev_tech = None
110                 for i, l in enumerate(lods):
111                         lod_st = []
112
113                         if l.data.name!=prev_mesh:
114                                 mesh_res = resources[l.data.name+".mesh"]
115                                 lod_st.append(obj_res.create_reference_statement("mesh", mesh_res))
116
117                                 prev_mesh = l.data.name
118
119                         material = None
120                         if l.material_slots:
121                                 material = l.material_slots[0].material
122                         if material:
123                                 if material.render_mode=='EXTERNAL':
124                                         tech_name = material.technique
125                                 elif material.material_atlas:
126                                         tech_name = "material_atlas_{}.tech".format(os.path.splitext(material.technique)[0])
127                                 else:
128                                         tech_name = material.name+".tech"
129                         else:
130                                 tech_name = "stub.tech"
131
132                         if tech_name!=prev_tech:
133                                 if material and material.render_mode=='EXTERNAL':
134                                         lod_st.append(Statement("technique", material.technique))
135                                 else:
136                                         lod_st.append(obj_res.create_reference_statement("technique", resources[tech_name]))
137                                 prev_tech = tech_name
138
139                         if i>0:
140                                 st = Statement("level_of_detail", i)
141                                 st.sub = lod_st
142                                 statements.append(st)
143                         else:
144                                 statements += lod_st
145
146                 progress.set_progress(1.0)
147
148                 return obj_res
149
150         def export_stub_technique(self):
151                 from .datafile import Resource, Statement
152                 tech_res = Resource("stub.tech", "technique")
153                 pass_st = Statement("method", "")
154                 tech_res.statements.append(pass_st)
155                 mat_st = Statement("material")
156                 pass_st.sub.append(mat_st)
157                 mat_st.sub.append(Statement("basic"))
158                 return tech_res