Skip to content

Instantly share code, notes, and snippets.

@tbttfox
Created December 12, 2021 20:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tbttfox/1b07dc25291c16adb0aecdafb87bb4b6 to your computer and use it in GitHub Desktop.
Save tbttfox/1b07dc25291c16adb0aecdafb87bb4b6 to your computer and use it in GitHub Desktop.
def combineFaces(faces, edgesToDelete):
""" This is where the edge deletion actually happens
Turn a group of connected faces into a new polygon
"""
# TODO
pass
def deleteEdges(counts, connects, edgesToDelete):
""" Counts and Connects are the return values from MFnMesh.getVertices
edgesToDelete is a list of pairs of vertex indices that will be removed
This will return new counts/connects that can be used to directly build
a new mesh that won't have those edges.
"""
# Group the connects into actual faces
idx = 0
faces = []
for count in counts:
faces.append(tuple(connects[idx: idx + count]))
idx += count
# Build the dict that will take a pair of vertices
# in sorted order, and return the faces that are
# connected to it
edgeToFaces = {}
for face in faces:
for x in range(len(face)):
key = tuple(sorted([face[i], face[i - 1]]))
edgeToFaces.setdefault(key, []).append(face)
# build the dictionaries that will keep track of
# which faces will be combined when the edges are removed
faceToIndex = {}
indexToFaces = {}
for i, face in enumerate(faces):
faceToIndex[face] = i
indexToFaces[i] = [face]
# Keep track of which edges to delete per index
indexToDelEdges = {}
for ed in edgesToDelete:
# Get the faces that are next to the edge to delete
adjacentFaces = edgeToFaces[tuple(sorted(ed))]
# Get the indices for those faces
idxsToCombine = [faceToIndex[f] for f in adjacentFaces]
# And get *all* the faces that have those indices
facesToCombine = [f for i in indexesToCombine for f in indexToFaces[i]]
# update the dictionaries so that all the faces we will
# be combining have the same index
newIdx = min(idxsToCombine)
oldIdx = max(idxsToCombine)
for f in facesToCombine:
faceToIndex[f] = newIdx
del indexToFaces[oldIdx]
indexToFaces[newIdx] = facesToCombine
curDelEdges = indexToDelEdges.get(newIdx, [])
oldDelEdges = indexToDelEdges.get(oldIdx, [])
indexToDelEdges[newIdx] = curDelEdges + oldDelEdges + [ed]
if oldDelEdges:
del indexToDelEdges[oldIdx]
newPolygons = []
for idx in sorted(indexToFaces.keys()):
newPolygons.append(combineFaces(indexToFaces[idx], indexToDelEdges[idx]))
newConnects = [vidx for p in newPolygons for vidx in p]
newCounts = [len(p) for p in newPolygons]
return newCounts, newConnects
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment