]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/mesh.py
Make resource handling in the Blender exporter more flexible
[libs/gl.git] / blender / io_mspgl / mesh.py
1 import bpy
2 import math
3 import mathutils
4
5 def make_edge_key(i1, i2):
6         return (min(i1, i2), max(i1, i2))
7
8 class Edge:
9         def __init__(self, edge):
10                 if edge.__class__==Edge:
11                         self._edge = edge._edge
12                         self.smooth = edge.smooth
13                 else:
14                         self._edge = edge
15                         self.smooth = False
16                 if edge:
17                         self.vertices = edge.vertices[:]
18                         self.key = edge.key
19                 else:
20                         self.vertices = []
21                         self.key = None
22                 self.faces = []
23
24         def __getattr__(self, attr):
25                 return getattr(self._edge, attr)
26
27         def check_smooth(self, limit):
28                 if len(self.faces)!=2:
29                         return
30
31                 d = self.faces[0].normal.dot(self.faces[1].normal)
32                 self.smooth = ((d>limit and self.faces[0].use_smooth and self.faces[1].use_smooth) or d>0.99995)
33
34         def other_face(self, f):
35                 if f.index==self.faces[0].index:
36                         if len(self.faces)>=2:
37                                 return self.faces[1]
38                         else:
39                                 return None
40                 else:
41                         return self.faces[0]
42
43         def other_vertex(self, v):
44                 if v.index==self.vertices[0].index:
45                         return self.vertices[1]
46                 else:
47                         return self.vertices[0]
48
49
50 class Vertex:
51         def __init__(self, vertex):
52                 if vertex.__class__==Vertex:
53                         self._vertex = vertex._vertex
54                         self.uvs = vertex.uvs[:]
55                         self.tan = vertex.tan
56                         self.bino = vertex.bino
57                 else:
58                         self._vertex = vertex
59                         self.uvs = []
60                         self.tan = None
61                         self.bino = None
62                 self.index = vertex.index
63                 self.co = vertex.co
64                 self.normal = vertex.normal
65                 self.flag = False
66                 self.edges = []
67                 self.faces = []
68                 self.groups = vertex.groups[:]
69
70         def __getattr__(self, attr):
71                 return getattr(self._vertex, attr)
72
73         def __cmp__(self, other):
74                 if other is None:
75                         return 1
76                 return cmp(self.index, other.index)
77
78
79 class VertexGroup:
80         def __init__(self, group):
81                 self._group = group
82                 self.group = group.group
83                 self.weight = group.weight
84
85         def __getattr__(self, attr):
86                 return getattr(self._group, attr)
87
88
89 class Face:
90         def __init__(self, face):
91                 self._face = face
92                 self.index = face.index
93                 self.edges = []
94                 self.vertices = face.vertices[:]
95                 self.uvs = []
96                 self.flag = False
97
98         def __getattr__(self, attr):
99                 return getattr(self._face, attr)
100
101         def __cmp__(self, other):
102                 if other is None:
103                         return 1
104                 return cmp(self.index, other.index)
105
106         def pivot_vertex(self, v):
107                 n = self.vertices.index(v)
108                 return [(n+i)%len(self.vertices) for i in range(len(self.vertices))]
109
110         def pivot_vertices(self, *vt):
111                 flags = [(v in vt) for v in self.vertices]
112                 l = len(self.vertices)
113                 for i in range(l):
114                         if flags[i] and not flags[(i+l-1)%l]:
115                                 return self.vertices[i:]+self.vertices[:i]
116
117         def get_edge(self, v1, v2):
118                 key = make_edge_key(v1.index, v2.index)
119                 for e in self.edges:
120                         if e.key==key:
121                                 return e
122                 raise KeyError("No edge %s"%(key,))
123
124         def other_edge(self, e, v):
125                 for d in self.edges:
126                         if d!=e and v in d.vertices:
127                                 return d
128
129         def get_neighbors(self):
130                 neighbors = [e.other_face(self) for e in self.edges]
131                 return list(filter(bool, neighbors))
132
133
134 class Line:
135         def __init__(self, e):
136                 self.edge = e
137                 self.vertices = e.vertices[:]
138                 self.flag = False
139
140
141 class UvLayer:
142         def __init__(self, arg):
143                 if type(arg)==str:
144                         self._layer = None
145                         self.name = arg
146                         self.uvs = []
147                 else:
148                         self._layer = arg
149                         self.name = arg.name
150                         self.uvs = [d.uv for d in self.data]
151
152                 self.unit = None
153                 self.hidden = False
154
155                 dot = self.name.find('.')
156                 if dot>=0:
157                         ext = self.name[dot:]
158                         if ext.startswith(".unit") and ext[5:].isdigit():
159                                 self.unit = int(ext[5:])
160                         elif ext==".hidden":
161                                 self.hidden = True
162
163         def __getattr__(self, attr):
164                 return getattr(self._layer, attr)
165
166
167 class Mesh:
168         def __init__(self, mesh):
169                 self._mesh = mesh
170                 self.name = mesh.name
171
172                 self.winding_test = mesh.winding_test
173                 self.tbn_vecs = mesh.tbn_vecs
174                 self.vertex_groups = mesh.vertex_groups
175
176                 # Clone basic data
177                 self.vertices = [Vertex(v) for v in mesh.vertices]
178                 for v in self.vertices:
179                         v.groups = [VertexGroup(g) for g in v.groups]
180
181                 self.faces = [Face(f) for f in mesh.polygons]
182                 self.edges = [Edge(e) for e in mesh.edges]
183                 self.loops = mesh.loops[:]
184                 self.materials = mesh.materials[:]
185
186                 # Clone only the desired UV layers
187                 if self.use_uv=='NONE' or not mesh.uv_layers:
188                         self.uv_layers = []
189                 else:
190                         self.uv_layers = [UvLayer(u) for u in mesh.uv_layers]
191                         self.uv_layers = sorted([u for u in self.uv_layers if not u.hidden], key=(lambda u: (u.unit or 1000, u.name)))
192
193                         if self.use_uv=='UNIT0':
194                                 self.uv_layers = [self.uv_layers[0]]
195
196                         # Assign texture unit numbers to UV layers that lack one
197                         next_unit = max((u.unit+1 for u in self.uv_layers if u.unit is not None), default=0)
198                         for u in self.uv_layers:
199                                 if not u.unit:
200                                         u.unit = next_unit
201                                         next_unit += 1
202
203                 # Rewrite links between elements to point to cloned data, or create links
204                 # where they don't exist
205                 edge_map = {e.key: e for e in self.edges}
206                 for f in self.faces:
207                         if len(f.vertices)>4:
208                                 raise ValueError("Ngons are not supported")
209
210                         f.vertices = [self.vertices[i] for i in f.vertices]
211                         for v in f.vertices:
212                                 v.faces.append(f)
213
214                         for k in f.edge_keys:
215                                 e = edge_map[k]
216                                 e.faces.append(f)
217                                 f.edges.append(e)
218
219                 for e in self.edges:
220                         e.vertices = [self.vertices[i] for i in e.vertices]
221                         for v in e.vertices:
222                                 v.edges.append(e)
223
224                 # Store loose edges as lines
225                 if self.use_lines:
226                         self.lines = [Line(e) for e in self.edges if not e.faces]
227                 else:
228                         self.lines = []
229
230                 self.vertex_sequence = []
231
232         def __getattr__(self, attr):
233                 return getattr(self._mesh, attr)
234
235         def transform(self, matrix):
236                 for v in self.vertices:
237                         v.co = matrix*v.co
238
239         def splice(self, other):
240                 if len(self.uv_layers)!=len(other.uv_layers):
241                         raise ValueError("Meshes have incompatible UV layers")
242                 for i, u in enumerate(self.uv_layers):
243                         if u.name!=other.uv_layers[i].name:
244                                 raise ValueError("Meshes have incompatible UV layers")
245
246                 # Merge materials and form a lookup from source material indices to the
247                 # merged material list
248                 material_map = []
249                 for m in other.materials:
250                         if m in self.materials:
251                                 material_map.append(self.materials.index(m))
252                         else:
253                                 material_map.append(len(self.materials))
254                                 self.materials.append(m)
255
256                 # Append data and adjust indices where necessary.  Since the data is
257                 # spliced from the source mesh, rebuilding references is not necessary.
258                 for i, u in enumerate(self.uv_layers):
259                         u.uvs += other.uv_layers[i].uvs
260
261                 offset = len(self.vertices)
262                 self.vertices += other.vertices
263                 for v in self.vertices[offset:]:
264                         v.index += offset
265
266                 loop_offset = len(self.loops)
267                 self.loops += other.loops
268
269                 offset = len(self.faces)
270                 self.faces += other.faces
271                 for f in self.faces[offset:]:
272                         f.index += offset
273                         f.loop_start += loop_offset
274                         f.loop_indices = range(f.loop_start, f.loop_start+f.loop_total)
275                         if other.materials:
276                                 f.material_index = material_map[f.material_index]
277
278                 offset = len(self.edges)
279                 self.edges += other.edges
280                 for e in self.edges[offset:]:
281                         e.index += offset
282                         e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
283
284                 self.lines += other.lines
285
286         def prepare_triangles(self, progress):
287                 face_count = len(self.faces)
288                 for i in range(face_count):
289                         f = self.faces[i]
290                         nverts = len(f.vertices)
291                         if nverts==3:
292                                 continue
293
294                         # Calculate normals at each vertex of the face
295                         edge_vecs = []
296                         for j in range(nverts):
297                                 edge_vecs.append(f.vertices[(j+1)%nverts].co-f.vertices[j].co)
298
299                         normals = []
300                         for j in range(nverts):
301                                 normals.append(edge_vecs[j-1].cross(edge_vecs[j]).normalized())
302
303                         # Check which diagonal results in a flatter triangulation
304                         flatness1 = normals[0].dot(normals[2])
305                         flatness2 = normals[1].dot(normals[3])
306                         cut_index = 1 if flatness1>flatness2 else 0
307
308                         nf = Face(f)
309                         nf.index = len(self.faces)
310                         self.faces.append(nf)
311
312                         ne = Edge(None)
313                         ne.index = len(self.edges)
314                         self.edges.append(ne)
315
316                         nf.vertices = [f.vertices[cut_index], f.vertices[2], f.vertices[3]]
317                         nf.loop_indices = [f.loop_indices[cut_index], f.loop_indices[2], f.loop_indices[3]]
318                         for v in nf.vertices:
319                                 v.faces.append(nf)
320
321                         ne.vertices = [f.vertices[cut_index], f.vertices[2+cut_index]]
322                         for v in ne.vertices:
323                                 v.edges.append(ne)
324                         ne.key = make_edge_key(ne.vertices[0].index, ne.vertices[1].index)
325                         ne.smooth = True
326
327                         f.vertices[3-cut_index].faces.remove(f)
328                         del f.vertices[3-cut_index]
329                         f.loop_indices = [f.loop_indices[0], f.loop_indices[1], f.loop_indices[2+cut_index]]
330
331                         ne.faces = [f, nf]
332                         if cut_index==0:
333                                 nf.edges = [ne, f.edges[2], f.edges[3]]
334                                 f.edges = [f.edges[0], f.edges[1], ne]
335                         else:
336                                 nf.edges = [f.edges[1], f.edges[2], ne]
337                                 f.edges = [f.edges[0], ne, f.edges[3]]
338                         for e in nf.edges:
339                                 if e!=ne:
340                                         e.faces.remove(f)
341                                         e.faces.append(nf)
342
343                         f.normal = normals[1-cut_index]
344                         nf.normal = normals[3-cut_index]
345
346                         progress.set_progress(i/face_count)
347
348         def prepare_smoothing(self, progress):
349                 smooth_limit = -1
350                 if self.smoothing=='NONE':
351                         for f in self.faces:
352                                 f.use_smooth = False
353
354                         smooth_limit = 1
355                 elif self.use_auto_smooth:
356                         smooth_limit = math.cos(self.auto_smooth_angle)
357
358                 for e in self.edges:
359                         e.check_smooth(smooth_limit)
360
361                 progress.push_task("Sharp edges", 0.0, 0.7)
362                 self.split_vertices(self.find_smooth_group, progress)
363
364                 if self.smoothing!='BLENDER':
365                         progress.set_task("Updating normals", 0.7, 1.0)
366                         self.compute_normals(progress)
367
368                 progress.pop_task()
369
370         def prepare_vertex_groups(self, obj):
371                 for v in self.vertices:
372                         if v.groups:
373                                 weight_sum = sum(g.weight for g in v.groups)
374                                 v.groups = sorted(v.groups, key=(lambda g: g.weight), reverse=True)[:self.max_groups_per_vertex]
375                                 weight_scale = weight_sum/sum(g.weight for g in v.groups)
376                                 for g in v.groups:
377                                         g.weight *= weight_scale
378
379                 if obj.parent and obj.parent.type=="ARMATURE":
380                         armature = obj.parent.data
381                         bone_indices = {b.name: i for i, b in enumerate(armature.bones)}
382                         group_index_map = {i: i for i in range(len(obj.vertex_groups))}
383                         for g in first_obj.vertex_groups:
384                                 if g.name in bone_indices:
385                                         group_index_map[g.index] = bone_indices[g.name]
386
387                         for v in self.vertices:
388                                 for g in v.groups:
389                                         g.group = group_index_map[g.group]
390
391         def prepare_uv(self, progress):
392                 # Form a list of UV layers referenced by materials with the array atlas
393                 # property set
394                 array_uv_layers = [t.uv_layer for m in self.materials if m.array_atlas for t in m.texture_slots if t and t.texture_coords=='UV']
395                 array_uv_layers = [u for u in self.uv_layers if u.name in array_uv_layers]
396
397                 if array_uv_layers:
398                         for f in self.faces:
399                                 layer = 0
400                                 if f.material_index<len(self.materials):
401                                         mat = self.materials[f.material_index]
402                                         if mat and mat.array_atlas:
403                                                 layer = mat.array_layer
404
405                                 for l in array_uv_layers:
406                                         for i in f.loop_indices:
407                                                 l.uvs[i] = mathutils.Vector((*l.uvs[i], layer))
408
409                 # Copy UVs from layers to faces
410                 for f in self.faces:
411                         for u in self.uv_layers:
412                                 f.uvs.append([u.uvs[i] for i in f.loop_indices])
413
414                 prog_count = len(self.uv_layers)
415                 prog_step = 0
416
417                 # Split by the UV layer used for TBN vectors first so connectivity
418                 # remains intact for TBN vector computation
419                 tbn_layer_index = -1
420                 if self.tbn_vecs:
421                         uv_names = [u.name for u in self.uv_layers]
422                         if self.tbn_uvtex in uv_names:
423                                 prog_count += 1
424                                 tbn_layer_index = uv_names.index(self.tbn_uvtex)
425                                 progress.push_task_slice("Computing TBN", 0, prog_count)
426                                 self.split_vertices(self.find_uv_group, progress, tbn_layer_index)
427                                 progress.set_task_slice(self.tbn_uvtex, 1, prog_count)
428                                 self.compute_tbn(tbn_layer_index, progress)
429                                 progress.pop_task()
430                                 prog_step = 2
431
432                 # Split by the remaining UV layers
433                 for i, u in enumerate(self.uv_layers):
434                         if i==tbn_layer_index:
435                                 continue
436
437                         progress.push_task_slice(u.name, prog_step, prog_count)
438                         self.split_vertices(self.find_uv_group, progress, i)
439                         progress.pop_task()
440                         prog_step += 1
441
442                 # Copy UVs from faces to vertices
443                 for v in self.vertices:
444                         if v.faces:
445                                 # All faces still connected to the vertex have the same UV value
446                                 f = v.faces[0]
447                                 i = f.vertices.index(v)
448                                 v.uvs = [u[i] for u in f.uvs]
449                         else:
450                                 v.uvs = [(0.0, 0.0)]*len(self.uv_layers)
451
452         def split_vertices(self, find_group_func, progress, *args):
453                 vertex_count = len(self.vertices)
454                 for i in range(vertex_count):
455                         v = self.vertices[i]
456                         for f in v.faces:
457                                 f.flag = False
458
459                         # Find all groups of faces on this vertex
460                         groups = []
461                         for f in v.faces:
462                                 if not f.flag:
463                                         groups.append(find_group_func(v, f, *args))
464
465                         # Give groups after the first separate copies of the vertex
466                         for g in groups[1:]:
467                                 nv = Vertex(v)
468                                 nv.index = len(self.vertices)
469                                 self.vertices.append(nv)
470
471                                 for e in v.edges:
472                                         e_faces_in_g = [f for f in e.faces if f in g]
473                                         if not e_faces_in_g:
474                                                 continue
475
476                                         if len(e_faces_in_g)<len(e.faces):
477                                                 # Create a copy of an edge at the boundary of the group
478                                                 ne = Edge(e)
479                                                 ne.index = len(self.edges)
480                                                 self.edges.append(ne)
481
482                                                 ne.other_vertex(v).edges.append(ne)
483
484                                                 for f in e_faces_in_g:
485                                                         e.faces.remove(f)
486                                                         f.edges[f.edges.index(e)] = ne
487                                                         ne.faces.append(f)
488
489                                                 e = ne
490
491                                         e.vertices[e.vertices.index(v)] = nv
492                                         nv.edges.append(e)
493
494                                         e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
495
496                                 # Filter out any edges that were removed from the original vertex
497                                 v.edges = [e for e in v.edges if v in e.vertices]
498
499                                 for f in g:
500                                         v.faces.remove(f)
501                                         f.vertices[f.vertices.index(v)] = nv
502                                         nv.faces.append(f)
503
504                         progress.set_progress(i/vertex_count)
505
506         def find_smooth_group(self, vertex, face):
507                 face.flag = True
508
509                 edges = [e for e in face.edges if vertex in e.vertices]
510
511                 group = [face]
512                 for e in edges:
513                         f = face
514                         while e.smooth:
515                                 f = e.other_face(f)
516                                 if not f or f.flag:
517                                         break
518
519                                 f.flag = True
520                                 group.append(f)
521                                 e = f.other_edge(e, vertex)
522
523                 return group
524
525         def find_uv_group(self, vertex, face, index):
526                 uv = face.uvs[index][face.vertices.index(vertex)]
527                 face.flag = True
528
529                 group = [face]
530                 for f in vertex.faces:
531                         if not f.flag and f.uvs[index][f.vertices.index(vertex)]==uv:
532                                 f.flag = True
533                                 group.append(f)
534
535                 return group
536
537         def compute_normals(self, progress):
538                 for i, v in enumerate(self.vertices):
539                         v.normal = mathutils.Vector()
540                         for f in v.faces:
541                                 fv = f.pivot_vertices(v)
542                                 edge1 = fv[1].co-fv[0].co
543                                 edge2 = fv[-1].co-fv[0].co
544                                 if edge1.length and edge2.length:
545                                         # Use the angle between edges as a weighting factor.  This gives
546                                         # more consistent normals on bends with an inequal number of
547                                         # faces on each side.
548                                         v.normal += f.normal*edge1.angle(edge2)
549
550                         if v.normal.length:
551                                 v.normal.normalize()
552                         else:
553                                 v.normal = mathutils.Vector((0, 0, 1))
554
555                         progress.set_progress(i/len(self.vertices))
556
557         def compute_tbn(self, index, progress):
558                 # This function is called at an early stage during UV preparation when
559                 # face UVs are not available yet
560                 layer_uvs = self.uv_layers[index].uvs
561
562                 for i, v in enumerate(self.vertices):
563                         v.tan = mathutils.Vector()
564                         v.bino = mathutils.Vector()
565                         for f in v.faces:
566                                 vi = f.pivot_vertex(v)
567                                 uv0 = layer_uvs[f.loop_indices[vi[0]]]
568                                 uv1 = layer_uvs[f.loop_indices[vi[1]]]
569                                 uv2 = layer_uvs[f.loop_indices[vi[-1]]]
570                                 du1 = uv1[0]-uv0[0]
571                                 du2 = uv2[0]-uv0[0]
572                                 dv1 = uv1[1]-uv0[1]
573                                 dv2 = uv2[1]-uv0[1]
574                                 edge1 = f.vertices[vi[1]].co-f.vertices[vi[0]].co
575                                 edge2 = f.vertices[vi[-1]].co-f.vertices[vi[0]].co
576                                 div = (du1*dv2-du2*dv1)
577                                 if div:
578                                         mul = edge1.angle(edge2)/div
579                                         v.tan += (edge1*dv2-edge2*dv1)*mul
580                                         v.bino += (edge2*du1-edge1*du2)*mul
581
582                         if v.tan.length:
583                                 v.tan.normalize()
584                         if v.bino.length:
585                                 v.bino.normalize()
586
587                         progress.set_progress(i/len(self.vertices))
588
589         def prepare_sequence(self, progress):
590                 progress.push_task("Reordering faces", 0.0, 0.5)
591                 self.reorder_faces(progress)
592
593                 progress.set_task("Building sequence", 0.5, 1.0)
594                 sequence = None
595                 for i, f in enumerate(self.faces):
596                         if sequence:
597                                 if len(sequence)==3:
598                                         # Rotate the first three vertices so that the new face can be added
599                                         if sequence[0] in f.vertices and sequence[1] not in f.vertices:
600                                                 sequence.append(sequence[0])
601                                                 del sequence[0]
602                                         elif sequence[2] not in f.vertices and sequence[1] in f.vertices:
603                                                 sequence.insert(0, sequence[-1])
604                                                 del sequence[-1]
605
606                                 if sequence[-1] not in f.vertices:
607                                         sequence = None
608                                 else:
609                                         to_add = [v for v in f.vertices if v!=sequence[-1] and v!=sequence[-2]]
610                                         if len(to_add)==2:
611                                                 if (f.vertices[1]==sequence[-1]) != (len(sequence)%2==1):
612                                                         to_add.reverse()
613                                                 sequence.append(sequence[-1])
614                                         sequence += to_add
615
616                         if not sequence:
617                                 sequence = f.vertices[:]
618                                 self.vertex_sequence.append(sequence)
619
620                         progress.set_progress(i/len(self.faces))
621
622                 progress.pop_task()
623
624                 self.reorder_vertices()
625
626         def reorder_faces(self, progress):
627                 # Tom Forsyth's vertex cache optimization algorithm
628                 # http://eelpi.gotdns.org/papers/fast_vert_cache_opt.html
629
630                 for f in self.faces:
631                         f.flag = False
632
633                 last_triangle_score = 0.75
634                 cache_decay_power = 1.5
635                 valence_boost_scale = 2.0
636                 valence_boost_power = -0.5
637
638                 max_cache_size = 32
639                 cached_vertices = []
640
641                 # Keep track of the score and number of unused faces for each vertex
642                 vertex_info = [[0, len(v.faces)] for v in self.vertices]
643                 for vi in vertex_info:
644                         vi[0] = valence_boost_scale*(vi[1]**valence_boost_power)
645
646                 face = None
647                 reordered_faces = []
648
649                 n_processed = 0
650                 while 1:
651                         if not face:
652                                 # Previous iteration gave no candidate for best face (or this is
653                                 # the first iteration).  Scan all faces for the highest score.
654                                 best_score = 0
655                                 for f in self.faces:
656                                         if f.flag:
657                                                 continue
658
659                                         score = sum(vertex_info[v.index][0] for v in f.vertices)
660                                         if score>best_score:
661                                                 best_score = score
662                                                 face = f
663
664                         if not face:
665                                 break
666
667                         reordered_faces.append(face)
668                         face.flag = True
669
670                         for v in face.vertices:
671                                 vertex_info[v.index][1] -= 1
672
673                                 # Shuffle the vertex into the front of the cache
674                                 if v in cached_vertices:
675                                         cached_vertices.remove(v)
676                                 cached_vertices.insert(0, v)
677
678                         # Update scores for all vertices in the cache
679                         for i, v in enumerate(cached_vertices):
680                                 score = 0
681                                 if i<3:
682                                         score += last_triangle_score
683                                 elif i<max_cache_size:
684                                         score += (1-(i-3)/(max_cache_size-3))**cache_decay_power
685                                 if vertex_info[v.index][1]:
686                                         score += valence_boost_scale*(vertex_info[v.index][1]**valence_boost_power)
687                                 vertex_info[v.index][0] = score
688
689                         face = None
690                         best_score = 0
691                         for v in cached_vertices:
692                                 for f in v.faces:
693                                         if not f.flag:
694                                                 score = sum(vertex_info[fv.index][0] for fv in f.vertices)
695                                                 if score>best_score:
696                                                         best_score = score
697                                                         face = f
698
699                         del cached_vertices[max_cache_size:]
700
701                         n_processed += 1
702                         progress.set_progress(n_processed/len(self.faces))
703
704                 self.faces = reordered_faces
705                 for i, f in enumerate(self.faces):
706                         f.index = i
707
708         def reorder_vertices(self):
709                 for v in self.vertices:
710                         v.index = -1
711
712                 reordered_vertices = []
713                 for s in self.vertex_sequence:
714                         for v in s:
715                                 if v.index<0:
716                                         v.index = len(reordered_vertices)
717                                         reordered_vertices.append(v)
718
719                 self.vertices = reordered_vertices
720
721                 for e in self.edges:
722                         e.key = make_edge_key(e.vertices[0].index, e.vertices[1].index)
723
724         def drop_references(self):
725                 for v in self.vertices:
726                         v._vertex = None
727                         for g in v.groups:
728                                 g._group = None
729                 for e in self.edges:
730                         e._edge = None
731                 for f in self.faces:
732                         f._face = None
733                 for u in self.uv_layers:
734                         u._layer = None
735                 self._mesh = None
736
737
738 def create_mesh_from_object(context, obj, progress):
739         if obj.type!="MESH":
740                 raise Exception("Object is not a mesh")
741
742         progress.push_task("Preparing mesh", 0.0, 0.2)
743
744         objs = [(obj, mathutils.Matrix())]
745         i = 0
746         while i<len(objs):
747                 o, m = objs[i]
748                 i += 1
749                 for c in o.children:
750                         if c.type=="MESH" and c.compound:
751                                 objs.append((c, m*c.matrix_local))
752
753         mesh = None
754         bmeshes = []
755         for o, m in objs:
756                 bmesh = o.to_mesh(context.scene, True, "PREVIEW")
757                 bmeshes.append(bmesh)
758
759                 # Object.to_mesh does not copy custom properties
760                 bmesh.winding_test = o.data.winding_test
761                 bmesh.smoothing = o.data.smoothing
762                 bmesh.use_lines = o.data.use_lines
763                 bmesh.vertex_groups = o.data.vertex_groups
764                 bmesh.max_groups_per_vertex = o.data.max_groups_per_vertex
765                 bmesh.use_uv = o.data.use_uv
766                 bmesh.tbn_vecs = o.data.tbn_vecs
767                 bmesh.tbn_uvtex = o.data.tbn_uvtex
768
769                 me = Mesh(bmesh)
770                 me.transform(m)
771
772                 if mesh:
773                         mesh.splice(me)
774                 else:
775                         mesh = me
776
777         mesh.name = obj.data.name
778
779         progress.set_task("Triangulating", 0.2, 0.3)
780         mesh.prepare_triangles(progress)
781         progress.set_task("Smoothing", 0.3, 0.5)
782         mesh.prepare_smoothing(progress)
783         progress.set_task("Vertex groups", 0.5, 0.6)
784         mesh.prepare_vertex_groups(obj)
785         progress.set_task("Preparing UVs", 0.6, 0.8)
786         mesh.prepare_uv(progress)
787         progress.set_task("Render sequence", 0.8, 1.0)
788         mesh.prepare_sequence(progress)
789
790         # Discard the temporary Blender meshes after making sure there's no
791         # references to the data
792         mesh.drop_references()
793         for m in bmeshes:
794                 bpy.data.meshes.remove(m)
795
796         progress.pop_task()
797
798         return mesh