]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_object.py
040f5302f060536c708c0bb9a0300a3c12444317
[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_export.export_technique_resources(material, resources)
70                                                 resources[tech_name] = material_export.export_technique(material, resources)
71                         elif "stub.tech" not in resources:
72                                 resources["stub.tech"] = self.export_stub_technique()
73
74                         mesh_name = l.data.name+".mesh"
75                         if mesh_name not in resources:
76                                 mesh = create_mesh_from_object(context, l, material_atlas, progress)
77                                 mesh_res = mesh_export.export_mesh(context, mesh, progress)
78                                 resources[mesh_name] = mesh_res
79
80                         progress.pop_task()
81
82         def export_object(self, obj, resources, progress):
83                 if obj.type!='MESH':
84                         raise ValueError("Object {} is not a mesh".format(obj.name))
85
86                 lods = self.collect_object_lods(obj)
87
88                 from .datafile import Resource, Statement
89                 obj_res = Resource(obj.name+".object", "object")
90                 statements = obj_res.statements
91
92                 center, radius = self.compute_bounding_sphere(obj)
93                 statements.append(Statement("bounding_sphere_hint", *center, radius))
94
95                 prev_mesh = None
96                 prev_tech = None
97                 for i, l in enumerate(lods):
98                         lod_st = []
99
100                         if l.data.name!=prev_mesh:
101                                 mesh_res = resources[l.data.name+".mesh"]
102                                 lod_st.append(obj_res.create_reference_statement("mesh", mesh_res))
103
104                                 prev_mesh = l.data.name
105
106                         material = None
107                         if l.material_slots:
108                                 material = l.material_slots[0].material
109                         if material:
110                                 if material.render_mode=='EXTERNAL':
111                                         tech_name = material.technique
112                                 elif material.material_atlas:
113                                         tech_name = "material_atlas_{}.tech".format(os.path.splitext(material.technique)[0])
114                                 else:
115                                         tech_name = material.name+".tech"
116                         else:
117                                 tech_name = "stub.tech"
118
119                         if tech_name!=prev_tech:
120                                 if material and material.render_mode=='EXTERNAL':
121                                         lod_st.append(Statement("technique", material.technique))
122                                 else:
123                                         lod_st.append(obj_res.create_reference_statement("technique", resources[tech_name]))
124                                 prev_tech = tech_name
125
126                         if i>0:
127                                 st = Statement("level_of_detail", i)
128                                 st.sub = lod_st
129                                 statements.append(st)
130                         else:
131                                 statements += lod_st
132
133                 progress.set_progress(1.0)
134
135                 return obj_res
136
137         def export_stub_technique(self):
138                 from .datafile import Resource, Statement, Token
139                 tech_res = Resource("stub.tech", "technique")
140                 pass_st = Statement("method", "")
141                 tech_res.statements.append(pass_st)
142                 mat_st = Statement("material")
143                 pass_st.sub.append(mat_st)
144                 mat_st.sub.append(Statement("type", Token("basic")))
145                 return tech_res