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