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