simplex-solver 3.1.0__39-abi3-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. simplex_solver-3.1.0.dist-info/METADATA +90 -0
  2. simplex_solver-3.1.0.dist-info/RECORD +79 -0
  3. simplex_solver-3.1.0.dist-info/WHEEL +5 -0
  4. simplexui/__init__.py +53 -0
  5. simplexui/_version.py +1 -0
  6. simplexui/channelBox.py +858 -0
  7. simplexui/comboCheckDialog.py +343 -0
  8. simplexui/commands/__init__.py +16 -0
  9. simplexui/commands/alembicCommon.py +991 -0
  10. simplexui/commands/applyCorrectives.py +451 -0
  11. simplexui/commands/correctiveInterface.py +261 -0
  12. simplexui/commands/expandedExport.py +436 -0
  13. simplexui/commands/hdf5Convert.py +60 -0
  14. simplexui/commands/mayaCorrectiveInterface.py +161 -0
  15. simplexui/commands/mayatonumpy.py +375 -0
  16. simplexui/commands/mesh.py +1081 -0
  17. simplexui/commands/numpytoimath.py +319 -0
  18. simplexui/commands/poseblendlib.py +166 -0
  19. simplexui/commands/reorderSimplexPoints.py +102 -0
  20. simplexui/commands/rigidAlign.py +99 -0
  21. simplexui/commands/smpxBlend.py +629 -0
  22. simplexui/commands/unsubdivide.py +1227 -0
  23. simplexui/commands/uvTransfer.py +977 -0
  24. simplexui/dragFilter.py +299 -0
  25. simplexui/falloffDialog.py +471 -0
  26. simplexui/img/ChefHead.png +0 -0
  27. simplexui/img/frozen.png +0 -0
  28. simplexui/interface/__init__.py +28 -0
  29. simplexui/interface/dummyInterface.py +1585 -0
  30. simplexui/interface/mayaInterface.py +3271 -0
  31. simplexui/interfaceModel.py +1053 -0
  32. simplexui/interfaceModelTrees.py +441 -0
  33. simplexui/items/__init__.py +42 -0
  34. simplexui/items/accessor.py +213 -0
  35. simplexui/items/combo.py +852 -0
  36. simplexui/items/falloff.py +778 -0
  37. simplexui/items/group.py +297 -0
  38. simplexui/items/progression.py +743 -0
  39. simplexui/items/shape.py +443 -0
  40. simplexui/items/simplex.py +1764 -0
  41. simplexui/items/slider.py +738 -0
  42. simplexui/items/stack.py +154 -0
  43. simplexui/items/traversal.py +966 -0
  44. simplexui/main.pyw +22 -0
  45. simplexui/menu/__init__.py +69 -0
  46. simplexui/menu/dummyPlugins/__init__.py +16 -0
  47. simplexui/menu/genericPlugins/__init__.py +16 -0
  48. simplexui/menu/genericPlugins/_builtins.py +268 -0
  49. simplexui/menu/genericPlugins/checkPossibleCombos.py +45 -0
  50. simplexui/menu/genericPlugins/exportSplit.py +59 -0
  51. simplexui/menu/genericPlugins/showFalloffs.py +24 -0
  52. simplexui/menu/genericPlugins/showTraversals.py +24 -0
  53. simplexui/menu/genericPlugins/simplexUvTransfer.py +107 -0
  54. simplexui/menu/genericPlugins/unsubdivide.py +70 -0
  55. simplexui/menu/mayaPlugins/__init__.py +16 -0
  56. simplexui/menu/mayaPlugins/exportOther.py +43 -0
  57. simplexui/menu/mayaPlugins/extractProgressives.py +65 -0
  58. simplexui/menu/mayaPlugins/freezeCombo.py +245 -0
  59. simplexui/menu/mayaPlugins/generateShapeIncrementals.py +97 -0
  60. simplexui/menu/mayaPlugins/importObjs.py +193 -0
  61. simplexui/menu/mayaPlugins/linearizeTraversal.py +48 -0
  62. simplexui/menu/mayaPlugins/makeShelfBtn.py +65 -0
  63. simplexui/menu/mayaPlugins/relaxToSelection.py +75 -0
  64. simplexui/menu/mayaPlugins/reloadDefinition.py +33 -0
  65. simplexui/menu/mayaPlugins/snapToNeutral.py +72 -0
  66. simplexui/menu/mayaPlugins/softSelectToCluster.py +200 -0
  67. simplexui/menu/mayaPlugins/tweakMix.py +134 -0
  68. simplexui/menu/mayaPlugins/updateRestShape.py +105 -0
  69. simplexui/pysimplex.dll.a +0 -0
  70. simplexui/pysimplex.pyd +0 -0
  71. simplexui/simplexDialog.py +1454 -0
  72. simplexui/travCheckDialog.py +397 -0
  73. simplexui/traversalDialog.py +271 -0
  74. simplexui/ui/comboCheckDialog.ui +475 -0
  75. simplexui/ui/falloffDialog.ui +243 -0
  76. simplexui/ui/simplexDialog.ui +1458 -0
  77. simplexui/ui/travCheckDialog.ui +488 -0
  78. simplexui/ui/traversalDialog.ui +532 -0
  79. simplexui/utils.py +320 -0
@@ -0,0 +1,1227 @@
1
+ # Copyright 2016, Blur Studio
2
+ #
3
+ # This file is part of Simplex.
4
+ #
5
+ # Simplex is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU Lesser General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # Simplex is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public License
16
+ # along with Simplex. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ # pylint: disable=unused-argument, too-many-locals
19
+ # pylint:disable=E0611,E0401
20
+
21
+ import json
22
+ from itertools import chain, zip_longest
23
+
24
+ from Qt.QtWidgets import QApplication
25
+ from .alembicCommon import buildSmpx, pbPrint, readSmpx
26
+
27
+ try:
28
+ import numpy as np
29
+ except ImportError:
30
+ np = None
31
+
32
+
33
+ def mergeCycles(groups):
34
+ """Take a list of ordered items, and sort them so the last item of each list matches the first of the next list
35
+
36
+ Then return the groups of lists mashed together
37
+ For instance, with two cycles:
38
+ +---------+------------------------------------------------------------+
39
+ | input | [(1, 2), (11, 12), (3, 1), (10, 11), (2, 3), (12, 10)] |
40
+ +---------+------------------------------------------------------------+
41
+ | reorder | [[(1, 2), (2, 3), (3, 1)], [(10, 11), (11, 12), (12, 13)]] |
42
+ +---------+------------------------------------------------------------+
43
+ | output | [[1, 2, 3], [10, 11, 12, 13]] |
44
+ +---------+------------------------------------------------------------+
45
+
46
+ Also, return whether the cycles merged form a single closed group
47
+
48
+ Parameters
49
+ ----------
50
+ groups : [(int, int), ...]
51
+ A list of pairs of integers
52
+
53
+ Returns
54
+ -------
55
+ : [[int, ...], ...]
56
+ The ordered cycles
57
+ """
58
+ groups = [list(g) for g in groups]
59
+ heads = {g[0]: g for g in groups}
60
+ tails = {g[-1]: g for g in groups}
61
+
62
+ headGetter = lambda x: heads.get(x[-1]) # noqa: E731
63
+ headSetter = lambda x, y: x + y[1:] # noqa: E731
64
+
65
+ tailGetter = lambda x: tails.get(x[0]) # noqa: E731
66
+ tailSetter = lambda x, y: y + x[1:] # noqa: E731
67
+
68
+ searches = ((headGetter, headSetter), (tailGetter, tailSetter))
69
+
70
+ out = []
71
+ cycles = []
72
+ while groups:
73
+ g = groups.pop()
74
+ del heads[g[0]]
75
+ del tails[g[-1]]
76
+
77
+ for getter, setter in searches:
78
+ while True:
79
+ adder = getter(g)
80
+ if adder is None:
81
+ break
82
+ g = setter(g, adder)
83
+ del heads[adder[0]]
84
+ del tails[adder[-1]]
85
+ adder[:] = []
86
+ groups = [x for x in groups if x]
87
+
88
+ cycle = False
89
+ if g[0] == g[-1]:
90
+ g.pop()
91
+ cycle = True
92
+ cycles.append(cycle)
93
+ out.append(g)
94
+ return out, cycles
95
+
96
+
97
+ def grow(neigh, verts, exclude):
98
+ """Grow the vertex set, also keeping track of which vertices to ignore for the next iteration
99
+
100
+ Parameters
101
+ ----------
102
+ neigh : {int: [int, ...]}
103
+ A dict mapping a vert index to a list of neighbor vert indices
104
+ verts : set(int)
105
+ A set of vert indices to grow
106
+ exclude : set(int)
107
+ A set of vert indices to ignore
108
+
109
+ Returns
110
+ -------
111
+ : set(int)
112
+ The newly grown vertices
113
+ : set(int)
114
+ ``exclude`` combined with ``verts``
115
+
116
+ """
117
+ grown = set()
118
+ growSet = verts - exclude
119
+ for v in growSet:
120
+ grown.update(neigh[v])
121
+ newGrown = grown - exclude
122
+ newExclude = exclude | growSet
123
+ return newGrown, newExclude
124
+
125
+
126
+ def buildHint(island, neigh, borders):
127
+ """Find star points that are an even number of grows from an edge
128
+
129
+ Parameters
130
+ ----------
131
+ island : [int, ...]
132
+ A list of vertices as part of a mesh island
133
+ neigh : {int: [int, ...]}
134
+ The dictionary of vertex neighbors
135
+ borders : set(int)
136
+ A set of border vertices
137
+
138
+ Returns
139
+ -------
140
+ : int
141
+ The first star point encountered at an even grow from the given borders
142
+ """
143
+ borders = borders & island
144
+ if not borders:
145
+ # Well ... we don't have any good way of dealing with this
146
+ # Best thing I can do is search for a point with the least
147
+ # number of similar valences, and return that
148
+ d = {}
149
+ for v in island:
150
+ d.setdefault(len(neigh[v]), []).append(v)
151
+
152
+ dd = {}
153
+ for k, v in d.items():
154
+ dd.setdefault(len(v), []).append(k)
155
+
156
+ mkey = min(dd.keys())
157
+ return d[dd[mkey][0]][0]
158
+
159
+ exclude = set()
160
+ while borders:
161
+ borders, exclude = grow(neigh, borders, exclude)
162
+ borders, exclude = grow(neigh, borders, exclude)
163
+ for b in borders:
164
+ if len(neigh[b]) != 4:
165
+ return b
166
+ return None
167
+
168
+
169
+ def partitionIslands(faces, neigh, pBar=None):
170
+ """Find all groups of connected verts
171
+
172
+ Parameters
173
+ ----------
174
+ faces : [[int, ...], ...]
175
+ The list of lists of vertex indices making up faces
176
+ neigh : {int: [int, ...]}
177
+ The dict of vertex neighbors
178
+ pBar : QProgressDialog or None
179
+ An optional progress bar
180
+
181
+ Returns
182
+ -------
183
+ : [set(int), ...]
184
+ A list of sets of non-connected vertex islands
185
+ """
186
+ allVerts = set(chain.from_iterable(faces))
187
+ islands = []
188
+ count = float(len(allVerts))
189
+
190
+ if pBar is not None:
191
+ pBar.setValue(0)
192
+ pBar.setMaximum(count)
193
+ QApplication.processEvents()
194
+
195
+ while allVerts:
196
+ verts = {allVerts.pop()}
197
+ exclude = set()
198
+ while verts:
199
+ verts, exclude = grow(neigh, verts, exclude)
200
+ islands.append(exclude)
201
+ allVerts.difference_update(exclude)
202
+
203
+ if pBar is not None:
204
+ pBar.setValue(count - len(allVerts))
205
+ QApplication.processEvents()
206
+
207
+ return islands
208
+
209
+
210
+ def buildUnsubdivideHints(faces, neigh, borders, pBar=None):
211
+ """Get one vertex per island that was part of the original mesh
212
+
213
+ Parameters
214
+ ----------
215
+ faces : [[int, ...], ...]
216
+ The list of lists of vertex indices making up faces
217
+ neigh : {int: [int, ...]}
218
+ The dict of vertex neighbors
219
+ borders : set(int)
220
+ A set of border vertices
221
+ pBar : QProgressDialog or None
222
+ An optional progress bar
223
+
224
+ Returns
225
+ -------
226
+ : [int, ...]
227
+ A list of star-points (one per island) to un-subdivide from
228
+ """
229
+ islands = partitionIslands(faces, neigh, pBar=pBar)
230
+ hints = []
231
+
232
+ if pBar is not None:
233
+ pBar.setValue(0)
234
+ pBar.setMaximum(len(islands))
235
+ QApplication.processEvents()
236
+
237
+ for i, isle in enumerate(islands):
238
+ if pBar is not None:
239
+ pBar.setValue(i)
240
+ QApplication.processEvents()
241
+ hints.append(buildHint(isle, neigh, borders))
242
+
243
+ hints = [h for h in hints if h is not None]
244
+ return hints
245
+
246
+
247
+ def getFaceCenterDel(faces, eNeigh, hints, pBar=None):
248
+ """Given a list of hint "keeper" points
249
+
250
+ Parameters
251
+ ----------
252
+ faces : [[int, ...], ...]
253
+ The list of lists of vertex indices making up faces
254
+ eNeigh : {int: [int, ...]}
255
+ The dict of vertex neighbors
256
+ hints : [int, ...]
257
+ A list of star-points (one per island) to un-subdivide from
258
+ pBar : QProgressDialog or None
259
+ An optional progress bar
260
+
261
+ Returns
262
+ -------
263
+ : set(int)
264
+ Centers of the original faces during a subdivision
265
+ : bool
266
+ Whether the operation failed(True) or not(False)
267
+ """
268
+ vertToFaces = {}
269
+ vc = set()
270
+ for i, face in enumerate(faces):
271
+ for f in face:
272
+ vertToFaces.setdefault(f, []).append(i)
273
+ vc.add(f)
274
+
275
+ count = len(vc)
276
+ centers = set()
277
+ midpoints = set()
278
+ originals = set(hints)
279
+ queue = set(hints)
280
+
281
+ if pBar is not None:
282
+ pBar.setValue(0)
283
+ pBar.setMaximum(count)
284
+ QApplication.processEvents()
285
+
286
+ i = 0
287
+ fail = False
288
+ while queue:
289
+ cur = queue.pop()
290
+ if cur in midpoints:
291
+ continue
292
+
293
+ if pBar is not None:
294
+ pBar.setValue(i)
295
+ QApplication.processEvents()
296
+ i += 2 # Add 2 because I *shouldn't* get any midpoints
297
+
298
+ midpoints.update(eNeigh[cur])
299
+ t = centers if cur in originals else originals
300
+
301
+ for f in vertToFaces[cur]:
302
+ nVerts = faces[f]
303
+
304
+ if len(nVerts) != 4:
305
+ fail = True
306
+ continue
307
+
308
+ curFaceIndex = nVerts.index(cur)
309
+
310
+ half = int(len(nVerts) / 2)
311
+ diag = nVerts[curFaceIndex - half]
312
+
313
+ isOrig = diag in originals
314
+ isCtr = diag in centers
315
+ if not isOrig and not isCtr:
316
+ t.add(diag)
317
+ queue.add(diag)
318
+ elif (
319
+ (isCtr and t is originals)
320
+ or (isOrig and t is centers)
321
+ or (diag in midpoints)
322
+ ):
323
+ fail = True
324
+
325
+ return centers, fail
326
+
327
+
328
+ def getBorders(faces):
329
+ """Get the indices of verts along the borders of a mesh
330
+
331
+ Parameters
332
+ ----------
333
+ faces : [[int, ...], ...]
334
+ The list of lists of vertex indices making up faces
335
+
336
+ Returns
337
+ -------
338
+ : set(int)
339
+ A set of border vertices
340
+ """
341
+ edgePairs = set()
342
+ for face in faces:
343
+ for f in range(len(face)):
344
+ edgePairs.add((face[f], face[f - 1]))
345
+ borders = set()
346
+ for ep in edgePairs:
347
+ if (ep[1], ep[0]) not in edgePairs:
348
+ borders.update(ep)
349
+ return borders
350
+
351
+
352
+ def buildEdgeDict(faces):
353
+ """Build a dictionary of un-ordered neighboring vertices along edges
354
+
355
+ Parameters
356
+ ----------
357
+ faces : [[int, ...], ...]
358
+ The list of lists of vertex indices making up faces
359
+
360
+ Returns
361
+ -------
362
+ : {int: set(int)}
363
+ A dictionary of neighboring vertices along edges
364
+ """
365
+ edgeDict = {}
366
+ for face in faces:
367
+ for f in range(len(face)):
368
+ ff = edgeDict.setdefault(face[f - 1], set())
369
+ ff.add(face[f])
370
+ ff.add(face[f - 2])
371
+ return edgeDict
372
+
373
+
374
+ def buildNeighborDict(faces):
375
+ """Build a structure to ask for edge and face neighboring vertices
376
+ The returned neighbor list starts with an edge neighbor, and
377
+ proceeds counter clockwise, alternating between edge and face neighbors
378
+ Also, while I'm here, grab the border verts
379
+
380
+ Parameters
381
+ ----------
382
+ faces : [[int, ...], ...]
383
+ The list of lists of vertex indices making up faces
384
+
385
+ Returns
386
+ -------
387
+ : {int: [[int, ...], ...]}
388
+ A dictionary keyed from a vert index whose
389
+ values are ordered cycles or fans
390
+ : set(int)
391
+ A set of vertices that are on the border
392
+ """
393
+ fanDict = {}
394
+ edgeDict = {}
395
+ for face in faces:
396
+ for i in range(len(face)):
397
+ fanDict.setdefault(face[i], []).append(face[i + 1 :] + face[:i])
398
+ ff = edgeDict.setdefault(face[i - 1], set())
399
+ ff.add(face[i])
400
+ ff.add(face[i - 2])
401
+
402
+ borders = set()
403
+ out = {}
404
+ for k, v in fanDict.items():
405
+ fans, cycles = mergeCycles(v)
406
+ for f, c in zip(fans, cycles):
407
+ if not c:
408
+ borders.update((f[0], f[-1], k))
409
+ out[k] = fans
410
+ return out, edgeDict, borders
411
+
412
+
413
+ def _fanMatch(fan, uFan, dWings):
414
+ """Twist a single fan so it matches the uFan if it can"""
415
+ uIdx = uFan[0]
416
+ for f, fIdx in enumerate(fan):
417
+ dw = dWings.get(fIdx, [])
418
+ if uIdx in dw:
419
+ return fan[f:] + fan[:f]
420
+ return None
421
+
422
+
423
+ def _align(neigh, uNeigh, dWings):
424
+ """Twist all the neighs so they match the uNeigh"""
425
+ out = []
426
+ for uFan in uNeigh:
427
+ for fan in neigh:
428
+ fm = _fanMatch(fan, uFan, dWings)
429
+ if fm is not None:
430
+ out.append(fm)
431
+ break
432
+ return out
433
+
434
+
435
+ def buildLayeredNeighborDicts(faces, uFaces, dWings):
436
+ """Build and align two neighbor dicts for both faces and uFaces
437
+ This guarantees that the neighbors at the same index are analogous
438
+ (ie. they go in the same direction)
439
+
440
+ Parameters
441
+ ----------
442
+ faces : [[int, ...], ...]
443
+ The list of lists of vertex indices making up faces
444
+ uFaces : [[int, ...], ...]
445
+ The list of lists of vertex indices making up un-subdivided faces
446
+ dWings : ???
447
+ ???
448
+
449
+ Returns
450
+ -------
451
+
452
+ """
453
+ neighDict, edgeDict, borders = buildNeighborDict(faces)
454
+ uNeighDict, uEdgeDict, uBorders = buildNeighborDict(uFaces)
455
+
456
+ assert (
457
+ borders >= uBorders
458
+ ), "Somehow the unsubdivided borders contain different vIdxs"
459
+
460
+ for k, uNeigh in uNeighDict.items():
461
+ neighDict[k] = _align(neighDict[k], uNeigh, dWings)
462
+
463
+ return neighDict, uNeighDict, edgeDict, uEdgeDict, borders
464
+
465
+
466
+ def _findOldPositionBorder(
467
+ faces,
468
+ uFaces,
469
+ verts,
470
+ uVerts,
471
+ neighDict,
472
+ uNeighDict,
473
+ edgeDict,
474
+ uEdgeDict,
475
+ borders,
476
+ vIdx,
477
+ computed,
478
+ ):
479
+ """Find the position of the un-subdivided mesh if the vertex was on the border"""
480
+ nei = neighDict[vIdx][0]
481
+ nei = [i for i in nei if i in borders]
482
+ assert len(nei) == 2, "Found multi border, {}".format(nei)
483
+ uVerts[vIdx] = 2 * verts[vIdx] - ((verts[nei[0]] + verts[nei[1]]) / 2)
484
+ computed.add(vIdx)
485
+
486
+
487
+ def _findOldPositionSimple(
488
+ faces,
489
+ uFaces,
490
+ verts,
491
+ uVerts,
492
+ neighDict,
493
+ uNeighDict,
494
+ edgeDict,
495
+ uEdgeDict,
496
+ vIdx,
497
+ computed,
498
+ ):
499
+ """Find the position of the un-subdivided mesh if the vertex has at least 4 neighbors.
500
+ Updates uVerts in-place
501
+ """
502
+ neigh = neighDict[vIdx][0]
503
+
504
+ eTest = edgeDict[vIdx]
505
+ e = [p for p in neigh if p in eTest]
506
+ f = [p for p in neigh if p not in eTest]
507
+
508
+ es = verts[e].sum(axis=0)
509
+ fs = verts[f].sum(axis=0)
510
+
511
+ n = len(e)
512
+ term1 = verts[vIdx] * (n / (n - 3.0))
513
+ term2 = es * (4 / (n * (n - 3.0)))
514
+ term3 = fs * (1 / (n * (n - 3.0)))
515
+ vk = term1 - term2 + term3
516
+
517
+ uVerts[vIdx] = vk
518
+ computed.add(vIdx)
519
+
520
+
521
+ def _findOldPosition3Valence(
522
+ faces,
523
+ uFaces,
524
+ verts,
525
+ uVerts,
526
+ neighDict,
527
+ uNeighDict,
528
+ edgeDict,
529
+ uEdgeDict,
530
+ vIdx,
531
+ computed,
532
+ ):
533
+ """Find the position of the un-subdivided mesh if the vertex has exactly 3 neighbors
534
+ Updates uVerts in-place. It is possible for this to fail.
535
+
536
+ Returns
537
+ -------
538
+ : bool
539
+ Whether an update happened
540
+ """
541
+ neigh = neighDict[vIdx][0]
542
+ uNeigh = uNeighDict[vIdx][0]
543
+
544
+ eTest = edgeDict[vIdx]
545
+ eNeigh = [n for n in neigh if n in eTest]
546
+ fNeigh = [n for n in neigh if n not in eTest]
547
+
548
+ ueTest = uEdgeDict[vIdx]
549
+ ueNeigh = [n for n in uNeigh if n in ueTest]
550
+ # ufNeigh = [n for n in uNeigh if n not in ueTest]
551
+
552
+ intr = computed.intersection(ueNeigh)
553
+ if intr:
554
+ # Easy valence 3 case. I only need
555
+ # The computed new neighbor
556
+ # The midpoint on the edge to that neighbor
557
+ # The "face" verts neighboring the midpoint
558
+
559
+ # Get the matching subbed an unsubbed neighbor indexes
560
+ uNIdx = intr.pop()
561
+ nIdx = eNeigh[ueNeigh.index(uNIdx)]
562
+
563
+ # Get the "face" verts next to the subbed neighbor
564
+ xx = neigh.index(nIdx)
565
+ fnIdxs = (neigh[xx - 1], neigh[(xx + 1) % len(neigh)])
566
+
567
+ # Then compute
568
+ # vk = 4*k1e - ke - k1fNs[0] - k1fNs[1]
569
+ vka = uVerts[uNIdx] + verts[fnIdxs[0]] + verts[fnIdxs[1]]
570
+ vkb = verts[nIdx] * 4
571
+ uVerts[vIdx] = vkb - vka
572
+ computed.add(vIdx)
573
+ return True
574
+
575
+ else:
576
+ # The Hard valence 3 case. Made even harder
577
+ # because the paper has a mistake in it
578
+
579
+ # vk = 4*ejk1 + 4*ejpk1 - fjnk1 - fjpk1 - 6*fjk1 + sum(fik)
580
+ # where k1 means subdivided mesh
581
+ # where j means an index, jn and jp are next/prev adjacents
582
+ # sum(fik) is the sum of all the points of the face that
583
+ # *aren't* the original, or edge-adjacent
584
+ # There could be more than 1 if an n-gon was subdivided
585
+ #
586
+ # I wonder: If it was a triangle that was subdivided, what
587
+ # would sum(fik) because there are no verts that fit that
588
+ # description. I think this is a degenerate case
589
+
590
+ # First, find an adjacent face on the unsub mesh that
591
+ # is only missing the neighbors of the vIdx
592
+
593
+ fnIdx = None
594
+ fik = None
595
+ fCtrIdx = None
596
+ for x, v in enumerate(fNeigh):
597
+ # working with neigh, but should only ever contain uNeigh indexes
598
+ eTest = edgeDict[vIdx]
599
+ origFace = {n for n in neighDict[v][0] if n not in eTest}
600
+
601
+ check = (origFace - set(ueNeigh)) - {vIdx}
602
+ if computed >= check:
603
+ fCtrIdx = v
604
+ fnIdx = x
605
+ fik = sorted(check)
606
+ break
607
+
608
+ if fnIdx is None:
609
+ # No possiblity found
610
+ return False
611
+
612
+ # Then apply the equation from above
613
+ neighIdx = neigh.index(fCtrIdx)
614
+ ejnk1 = neigh[(neighIdx + 1) % len(neigh)]
615
+ ejpk1 = neigh[neighIdx - 1]
616
+ fjnk1 = fNeigh[(fnIdx + 1) % len(fNeigh)]
617
+ fjpk1 = fNeigh[fnIdx - 1]
618
+ fjk1 = verts[fCtrIdx]
619
+ sumFik = uVerts[fik].sum(axis=0)
620
+ vk = 4 * ejnk1 + 4 * ejpk1 - fjnk1 - fjpk1 - 6 * fjk1 + sumFik
621
+ uVerts[vIdx] = vk
622
+ computed.add(vIdx)
623
+ return True
624
+ return False
625
+
626
+
627
+ def deleteCenters(meshFaces, uvFaces, centerDel, pBar=None):
628
+ """Delete the given vertices and connected edges from a face representation
629
+ to give a new representation.
630
+
631
+ Parameters
632
+ ----------
633
+ meshFaces : [[int, ...], ...]
634
+ The list of lists of vertex indices making up faces
635
+ centerDel : set(int)
636
+ A set of vertices to delete. These were the vertices added
637
+ to the centers of the faces when subdividing.
638
+ uvFaces : [[int, ...], ...]
639
+ The list of lists of uv indices making up uvFaces
640
+ pBar : QProgressDialog or None
641
+ An optional progress bar
642
+
643
+ Returns
644
+ -------
645
+ : [[int, ...], ...]
646
+ The new list of faces of the mesh
647
+ : [[int, ...], ...]
648
+ The new list of uvfaces of the mesh
649
+ : {int: (int, int)}
650
+ A dict of a deleted edge-midpoint to its two existing neighbor verts
651
+ : {int: (int, int)}
652
+ A dict of a deleted uv edge-midpoint to its two existing neighbor uvs
653
+ """
654
+ # For each deleted index, grab the neighboring faces,
655
+ # and twist the faces so the deleted index is first
656
+ cds = set(centerDel)
657
+ faceDelDict = {}
658
+ uvDelDict = {}
659
+ uvFaces = uvFaces or []
660
+ for face, uvFace in zip_longest(meshFaces, uvFaces):
661
+ fi = cds.intersection(face)
662
+ # If we are a subdivided mesh, Then each face will have exactly one
663
+ # vertex that is part of the deletion set
664
+ if len(fi) != 1:
665
+ raise ValueError("Found a face with an unrecognized connectivity")
666
+ # Get that one vert
667
+ idx = fi.pop()
668
+ # Each face is a cycle. Rotate the cycle
669
+ # so that idx is first in the list
670
+ rv = face.index(idx)
671
+ rFace = face[rv:] + face[:rv]
672
+ faceDelDict.setdefault(idx, []).append(rFace)
673
+
674
+ if uvFace is not None:
675
+ rUVFace = uvFace[rv:] + uvFace[:rv]
676
+ uvDelDict.setdefault(idx, []).append(rUVFace)
677
+
678
+ newFaces = []
679
+ nUVFaces = []
680
+ wings = {}
681
+ uvWings = {}
682
+
683
+ if pBar is not None:
684
+ pBar.setValue(0)
685
+ pBar.setMaximum(len(faceDelDict))
686
+
687
+ chk = -1
688
+ for idx, rFaces in faceDelDict.items():
689
+ chk += 1
690
+ if pBar is not None:
691
+ pBar.setValue(chk)
692
+ QApplication.processEvents()
693
+
694
+ ruvFaces = uvDelDict.get(idx, [])
695
+ # The faces are guaranteed to be in a single loop cycle
696
+ # so I don't have to handle any annoying edge cases! Yay!
697
+ faceEnds = {
698
+ f[1]: (f[2], f[3], uvf) for f, uvf in zip_longest(rFaces, ruvFaces)
699
+ } # face ends
700
+
701
+ end = rFaces[-1][-1] # get an arbitrary face to start with
702
+ newFace = []
703
+ nUVFace = []
704
+ while faceEnds:
705
+ try:
706
+ diag, nxt, uvf = faceEnds.pop(end)
707
+ except KeyError:
708
+ print("rFaces", rFaces)
709
+ print("fe", faceEnds)
710
+ raise
711
+ if uvf is not None:
712
+ try:
713
+ nUVFace.append(uvf[2])
714
+ uvWings.setdefault(uvf[1], []).append(uvf[2])
715
+ uvWings.setdefault(uvf[3], []).append(uvf[2])
716
+ except IndexError:
717
+ print("UVF", uvf, chk)
718
+ raise
719
+
720
+ newFace.append(diag)
721
+ wings.setdefault(end, []).append(diag)
722
+ wings.setdefault(nxt, []).append(diag)
723
+
724
+ end = nxt
725
+ newFaces.append(newFace)
726
+ if nUVFace:
727
+ nUVFaces.append(nUVFace)
728
+ nUVFaces = nUVFaces or None
729
+
730
+ return newFaces, nUVFaces, wings, uvWings
731
+
732
+
733
+ def fixVerts(
734
+ faces,
735
+ uFaces,
736
+ verts,
737
+ neighDict,
738
+ uNeighDict,
739
+ edgeDict,
740
+ uEdgeDict,
741
+ borders,
742
+ pinned,
743
+ pBar=None,
744
+ ):
745
+ """Given the faces, vertex positions, and the point indices that
746
+ were created at the face centers for a subdivision step
747
+ Return the faces and verts of the mesh from before the
748
+ subdivision step. This algorithm doesn't handle UV's yet
749
+
750
+ Parameters
751
+ ----------
752
+ faces : [[int, ...], ...]
753
+ The list of lists of vertex indices making up faces
754
+ verts : np.array
755
+ The Nx3 array of vertices
756
+ centerDel : set(int)
757
+ A set of vertices to delete
758
+ uFaces : [[int, ...], ...]
759
+ The list of lists of vertex indices making up the
760
+ un-subdivided faces. Not all numbers in the vert range are in this structure
761
+ neighDict : {int: [int, ...]}
762
+ The dictionary of vertex neighbors based on ``faces``
763
+ uNeighDict : {int: [int, ...]}
764
+ The dictionary of vertex neighbors based on ``uFaces``
765
+ edgeDict : TODO
766
+ Some kind of dictionary representing individual edges
767
+ uEdgeDict : TODO
768
+ Some kind of dictionary representing individual edges in the unsub mesh
769
+ borders : set(int)
770
+ The set of vertices along the borders
771
+ pinned : set(int)
772
+ The set of vertices that will not be moved by this process
773
+ pBar : QProgressDialog or None
774
+ An optional progress bar
775
+
776
+ Returns
777
+ -------
778
+ : np.array
779
+ An array of vertex positions
780
+ """
781
+ uVerts = verts.copy()
782
+ uIdxs = sorted(set(chain.from_iterable(uFaces)))
783
+
784
+ v3Idxs = []
785
+ # bowtie verts are pinned
786
+ bowTieIdxs = []
787
+ computed = set()
788
+ i = 0
789
+ if pBar is not None:
790
+ pBar.setValue(0)
791
+ pBar.setMaximum(len(uIdxs))
792
+
793
+ for idx in uIdxs:
794
+ if pBar is not None:
795
+ pBar.setValue(i)
796
+ QApplication.processEvents()
797
+ if len(uNeighDict[idx]) > 1:
798
+ bowTieIdxs.append(idx)
799
+ i += 1
800
+ elif idx in pinned:
801
+ pass
802
+ elif idx in borders:
803
+ _findOldPositionBorder(
804
+ faces,
805
+ uFaces,
806
+ verts,
807
+ uVerts,
808
+ neighDict,
809
+ uNeighDict,
810
+ edgeDict,
811
+ uEdgeDict,
812
+ borders,
813
+ idx,
814
+ computed,
815
+ )
816
+ i += 1
817
+ elif sum(map(len, neighDict[idx])) > 6: # if valence > 3
818
+ _findOldPositionSimple(
819
+ faces,
820
+ uFaces,
821
+ verts,
822
+ uVerts,
823
+ neighDict,
824
+ uNeighDict,
825
+ edgeDict,
826
+ uEdgeDict,
827
+ idx,
828
+ computed,
829
+ )
830
+ i += 1
831
+ else:
832
+ v3Idxs.append(idx)
833
+
834
+ updated = True
835
+ while updated:
836
+ updated = False
837
+ rem = set()
838
+ for idx in v3Idxs:
839
+ up = _findOldPosition3Valence(
840
+ faces,
841
+ uFaces,
842
+ verts,
843
+ uVerts,
844
+ neighDict,
845
+ uNeighDict,
846
+ edgeDict,
847
+ uEdgeDict,
848
+ idx,
849
+ computed,
850
+ )
851
+ if not up:
852
+ continue
853
+ if pBar is not None:
854
+ pBar.setValue(i)
855
+ QApplication.processEvents()
856
+ i += 1
857
+ updated = True
858
+ rem.add(idx)
859
+ v3Idxs = list(set(v3Idxs) - rem)
860
+
861
+ return uVerts
862
+
863
+
864
+ def getUVPins(faces, borders, uvFaces, uvBorders, pinBorders):
865
+ """Find which uvBorders are also mesh borders
866
+
867
+ Parameters
868
+ ----------
869
+ faces : [[int, ...], ...]
870
+ The list of lists of vertex indices making up faces
871
+ borders : set(int)
872
+ The set of vertices along the borders
873
+ uvFaces : [[int, ...], ...]
874
+ The list of lists of uv indices making up uvFaces
875
+ uvBorders : set(int)
876
+ The set of vertices along the borders of uvs
877
+ pinBorders : bool
878
+ Whether to just pin all uv borders
879
+
880
+ Returns
881
+ -------
882
+ : set(int)
883
+ The vertex indices to pin
884
+ """
885
+ if uvFaces is None:
886
+ return set()
887
+ if pinBorders:
888
+ return set(uvBorders)
889
+
890
+ pinnit = set()
891
+ for face, uvFace in zip(faces, uvFaces):
892
+ for i in range(len(face)):
893
+ f = face[i]
894
+ pf = face[i - 1]
895
+
896
+ uv = uvFace[i]
897
+ puv = uvFace[i - 1]
898
+
899
+ if not (f in borders and pf in borders):
900
+ if uv in uvBorders and puv in uvBorders:
901
+ pinnit.add(puv)
902
+ pinnit.add(uv)
903
+
904
+ return uvBorders & pinnit
905
+
906
+
907
+ def collapse(faces, verts, uvFaces, uvs):
908
+ """Take a mesh representation with unused vertex indices, and remove those vertices
909
+
910
+ Parameters
911
+ ----------
912
+ faces : [[int, ...], ...]
913
+ The list of lists of vertex indices making up faces
914
+ The set of integers used here will be non-contiguous
915
+ verts : np.array
916
+ The N*3 array of vertices
917
+ uvFaces : [[int, ...], ...]
918
+ The list of lists of uv indices making up uvFaces
919
+ The set of integers used here will be non-contiguous
920
+ uvs : np.array
921
+ The N*2 array of uvs
922
+
923
+ Returns
924
+ -------
925
+ : [[int, ...], ...]
926
+ The list of lists of vertex indices making up faces
927
+ The set of integers used here will be contiguous
928
+ : np.array
929
+ The new N*3 array of vertices
930
+ : [[int, ...], ...]
931
+ The list of lists of uv indices making up uvFaces
932
+ The set of integers used here will be non-contiguous
933
+ : np.array
934
+ The new N*2 array of uvs
935
+ """
936
+ vset = sorted(set(chain.from_iterable(faces)))
937
+ nVerts = verts[vset]
938
+ vDict = {v: i for i, v in enumerate(vset)}
939
+ nFaces = [[vDict[f] for f in face] for face in faces]
940
+
941
+ if uvFaces is not None:
942
+ uvset = sorted(set(chain.from_iterable(uvFaces)))
943
+ nUVs = uvs[uvset]
944
+ uvDict = {v: i for i, v in enumerate(uvset)}
945
+ nUVFaces = [[uvDict[f] for f in face] for face in uvFaces]
946
+ else:
947
+ nUVs = None
948
+ nUVFaces = None
949
+
950
+ return nFaces, nVerts, nUVFaces, nUVs
951
+
952
+
953
+ def getCenters(faces, hints=None, pBar=None):
954
+ """Given a set of faces, find the face-center vertices from the subdivision
955
+
956
+ Parameters
957
+ ----------
958
+ faces : [[int, ...], ...]
959
+ The list of lists of vertex indices making up faces
960
+ hints : set(int) or None
961
+ An optional list of star points that were part of the original un-subdivided mesh
962
+ pBar : QProgressDialog or None
963
+ An optional progress bar
964
+
965
+ Returns
966
+ -------
967
+ : set(int)
968
+ The vertices that were added to the centers of the faces as part of the subdivision process
969
+ """
970
+ if pBar is not None:
971
+ pBar.setLabelText("Crawling Edges")
972
+ pBar.show()
973
+ QApplication.processEvents()
974
+
975
+ eNeigh = buildEdgeDict(faces)
976
+ if hints is None:
977
+ borders = getBorders(faces)
978
+ hints = buildUnsubdivideHints(
979
+ faces, eNeigh, borders, pBar=None
980
+ ) # purposely no PBar
981
+ centerDel, fail = getFaceCenterDel(faces, eNeigh, hints, pBar=pBar)
982
+ assert not fail, "Could not detect subdivided topology with the provided hints"
983
+
984
+ if pBar is not None:
985
+ pBar.close()
986
+
987
+ return centerDel
988
+
989
+
990
+ def unSubdivide(
991
+ faces,
992
+ verts,
993
+ uvFaces,
994
+ uvs,
995
+ hints=None,
996
+ repositionVerts=True,
997
+ pinBorders=False,
998
+ pBar=None,
999
+ ):
1000
+ """Given a mesh representation (faces and vertices) remove the edges added
1001
+ by a subdivision, and optionally reposition the verts
1002
+
1003
+ Parameters
1004
+ ----------
1005
+ faces : [[int, ...], ...]
1006
+ The list of lists of vertex indices making up faces
1007
+ verts : np.array
1008
+ The N*3 array of vertices
1009
+ uvFaces : [[int, ...], ...]
1010
+ The list of lists of uv indices making up uvFaces
1011
+ uvs : np.array
1012
+ The N*2 array of uvs
1013
+ hints : set(int) or None
1014
+ An optional list of star points that were part of the original un-subdivided mesh
1015
+ repositionVerts : bool
1016
+ Whether to also estimate the positions of the un-subdivided verts. Defaults to True
1017
+ pinBorders : bool
1018
+ Whether or not to pin the border vertices. Defaults to False
1019
+ pBar : QProgressDialog or None
1020
+ An optional progress bar
1021
+
1022
+ Returns
1023
+ -------
1024
+ : [[vIdx ...], ...]
1025
+ The un-subdivided face structure
1026
+ : np.array
1027
+ The un-subdivided vertex positions
1028
+ : [[vIdx ...], ...] or None
1029
+ The un-subdivided uv-face structure if it exists
1030
+ : np.array or None
1031
+ The un-subdivided uvs if they exist
1032
+ """
1033
+ if pBar is not None:
1034
+ pBar.show()
1035
+ pBar.setLabelText("Finding Neighbors")
1036
+ QApplication.processEvents()
1037
+
1038
+ eNeigh = buildEdgeDict(faces)
1039
+
1040
+ if hints is None:
1041
+ if pBar is not None:
1042
+ pBar.show()
1043
+ pBar.setLabelText("Getting Hints")
1044
+ QApplication.processEvents()
1045
+ borders = getBorders(faces)
1046
+ hints = buildUnsubdivideHints(
1047
+ faces, eNeigh, borders, pBar=None
1048
+ ) # Purposely no PBar
1049
+
1050
+ if pBar is not None:
1051
+ pBar.setLabelText("Crawling Edges")
1052
+ QApplication.processEvents()
1053
+ centerDel, fail = getFaceCenterDel(faces, eNeigh, hints, pBar=pBar)
1054
+ assert not fail, "Could not detect subdivided topology with the provided hints"
1055
+
1056
+ if pBar is not None:
1057
+ pBar.setLabelText("Deleting Edges")
1058
+ QApplication.processEvents()
1059
+ uFaces, uUVFaces, dWings, uvDWings = deleteCenters(
1060
+ faces, uvFaces, centerDel, pBar=pBar
1061
+ )
1062
+
1063
+ uVerts = verts
1064
+ uUVs = uvs
1065
+ if repositionVerts:
1066
+ # Handle the verts
1067
+ if pBar is not None:
1068
+ pBar.setLabelText("Building Correspondences")
1069
+ QApplication.processEvents()
1070
+ neighDict, uNeighDict, edgeDict, uEdgeDict, borders = buildLayeredNeighborDicts(
1071
+ faces, uFaces, dWings
1072
+ )
1073
+ pinned = set(borders) if pinBorders else []
1074
+ if pBar is not None:
1075
+ pBar.setLabelText("Fixing Vert Positions")
1076
+ QApplication.processEvents()
1077
+ uVerts = fixVerts(
1078
+ faces,
1079
+ uFaces,
1080
+ verts,
1081
+ neighDict,
1082
+ uNeighDict,
1083
+ edgeDict,
1084
+ uEdgeDict,
1085
+ borders,
1086
+ pinned,
1087
+ pBar=pBar,
1088
+ )
1089
+
1090
+ # Handle the UVs
1091
+ if uvFaces is not None:
1092
+ (
1093
+ uvNeighDict,
1094
+ uUVNeighDict,
1095
+ uvEdgeDict,
1096
+ uvUEdgeDict,
1097
+ uvBorders,
1098
+ ) = buildLayeredNeighborDicts(uvFaces, uUVFaces, uvDWings)
1099
+ uvPinned = getUVPins(faces, borders, uvFaces, uvBorders, pinBorders)
1100
+ if pBar is not None:
1101
+ pBar.setLabelText("Fixing UV Positions")
1102
+ QApplication.processEvents()
1103
+ uUVs = fixVerts(
1104
+ uvFaces,
1105
+ uUVFaces,
1106
+ uvs,
1107
+ uvNeighDict,
1108
+ uUVNeighDict,
1109
+ uvEdgeDict,
1110
+ uvUEdgeDict,
1111
+ uvBorders,
1112
+ uvPinned,
1113
+ pBar=pBar,
1114
+ )
1115
+
1116
+ rFaces, rVerts, rUVFaces, rUVs = collapse(uFaces, uVerts, uUVFaces, uUVs)
1117
+
1118
+ if pBar is not None:
1119
+ pBar.close()
1120
+
1121
+ return rFaces, rVerts, rUVFaces, rUVs
1122
+
1123
+
1124
+ ####################################################################
1125
+ # Handle .smpx files here #
1126
+ ####################################################################
1127
+
1128
+
1129
+ def _ussmpx(faces, verts, uvFaces, uvs, pBar=None):
1130
+ """Unsubdivide a simplex"""
1131
+ pbPrint(pBar, message="Finding Neighbors")
1132
+ eNeigh = buildEdgeDict(faces)
1133
+
1134
+ pbPrint(pBar, message="Getting Hints")
1135
+ borders = getBorders(faces)
1136
+ hints = buildUnsubdivideHints(
1137
+ faces, eNeigh, borders, pBar=None
1138
+ ) # Purposely no PBar
1139
+
1140
+ pbPrint(pBar, message="Crawling Edges")
1141
+ centerDel, fail = getFaceCenterDel(faces, eNeigh, hints, pBar=pBar)
1142
+ assert not fail, "Could not detect subdivided topology with the provided hints"
1143
+
1144
+ pbPrint(pBar, message="Deleting Edges")
1145
+ uFaces, uUVFaces, _, _ = deleteCenters(faces, uvFaces, centerDel, pBar=pBar)
1146
+
1147
+ pbPrint(pBar, message="Collapsing Indexes")
1148
+ uVerts = verts
1149
+ uUVs = uvs
1150
+
1151
+ rFaces, rVerts, rUVFaces, rUVs = collapse(uFaces, uVerts, uUVFaces, uUVs)
1152
+
1153
+ if pBar is not None:
1154
+ pBar.close()
1155
+ else:
1156
+ print("Done")
1157
+
1158
+ return rFaces, rVerts, rUVFaces, rUVs
1159
+
1160
+
1161
+ def _applyShapePrefix(shapePrefix, jsString):
1162
+ """Apply a prefix to the shape names.
1163
+ For XSI where shape names must be unique
1164
+ """
1165
+ if shapePrefix is not None:
1166
+ d = json.loads(jsString)
1167
+ if d["encodingVersion"] > 1:
1168
+ for shape in d["shapes"]:
1169
+ shape["name"] = shapePrefix + shape["name"]
1170
+ else:
1171
+ d["shapes"] = [shapePrefix + i for i in d["shapes"]]
1172
+ jsString = json.dumps(d)
1173
+ return jsString
1174
+
1175
+
1176
+ def _unflattenFaces(flatFaces, counts):
1177
+ faces = []
1178
+ ptr = 0
1179
+ for c in counts:
1180
+ faces.append(flatFaces[ptr : ptr + c].tolist())
1181
+ ptr += c
1182
+ return faces
1183
+
1184
+
1185
+ def unsubdivideSimplex(inPath, outPath, shapePrefix=None, pBar=None):
1186
+ """Unsubdivde a .smpx file on disk
1187
+
1188
+ Parameters
1189
+ ----------
1190
+ inPath : str
1191
+ The input .smpx file path
1192
+ outPath : str
1193
+ The output .smpx file path
1194
+ shapePrefix : str or None
1195
+ An optional string to prefix the shape names with
1196
+ pBar : QProgressDialog or None
1197
+ An optional progress bar
1198
+ """
1199
+ if np is None:
1200
+ raise RuntimeError("Un-Subdivide requires numpy, and it is not available here")
1201
+
1202
+ pbPrint(pBar, message="Loading smpx")
1203
+ jsString, counts, verts, flatFaces, uvs, flatUVFaces = readSmpx(inPath)
1204
+ jsString = _applyShapePrefix(shapePrefix, jsString)
1205
+
1206
+ js = json.loads(jsString)
1207
+ name = js["systemName"]
1208
+
1209
+ faces = _unflattenFaces(flatFaces, counts)
1210
+ uvFaces = None
1211
+ if flatUVFaces is not None:
1212
+ uvFaces = _unflattenFaces(flatUVFaces, counts)
1213
+
1214
+ pbPrint(pBar, message="Unsubdividing")
1215
+ verts = verts.swapaxes(0, 1)
1216
+ uFaces, uVerts, uUVFaces, uUVs = _ussmpx(faces, verts, uvFaces, uvs, pBar=pBar)
1217
+
1218
+ pbPrint(pBar, message="Exporting")
1219
+ buildSmpx(
1220
+ outPath,
1221
+ uVerts.swapaxes(0, 1),
1222
+ uFaces,
1223
+ jsString,
1224
+ name,
1225
+ uvs=uUVs,
1226
+ uvFaces=uUVFaces,
1227
+ )