Skip to content

Instantly share code, notes, and snippets.

@thirstydevil
Last active May 10, 2017 18:48
Show Gist options
  • Save thirstydevil/40d8a10f8bd7387178da1e334272a69f to your computer and use it in GitHub Desktop.
Save thirstydevil/40d8a10f8bd7387178da1e334272a69f to your computer and use it in GitHub Desktop.
Method of traversing polygons and removing duplicate faces that are sat on top of one and another.
def cleanDuplicateMeshFaces(transform=None):
"""
We are getting lots of meshes from a client with duplicate polygons sat ontop of each other and the vertex positions
of each face are the same. Usually most of them are welded and so every face in lamina. The mesh cleanup tools just
separate every face on the mesh. There's no garantee that the mesh has UV's.
:param transform: PyNode or None
:return: None
"""
if transform is None:
transform = pCore.selected()[0]
shapeNode = transform.getShape()
pCore.select(shapeNode.vtx)
pCore.mel.DetachComponent()
pCore.select([])
class faceData(object):
def __repr__(self):
return "fd(id:%s, %s)" % (self.index, self.points)
def __init__(self, index, points, face):
self.index = index
self.points = points
self.face = face
def array(self):
"""
Adding all the vectors up into 1 array, then asking the length. May be floored.
:return:
"""
data = [list(l) for l in self.points]
return pCore.dt.Array(data)
def isEquivalent(self, other):
return self.array().isEquivalent(other.array())
def __eq__(self, other):
"""
Brute force comparison
:param other: faceData
:return: Bool
"""
if other.index == self.index:
return False
else:
found = []
for p in self.points:
for op in other.points:
if p.isEquivalent(op):
found.append(1)
return len(found) == len(self.points)
def __hash__(self):
"""
For set()
:return: hash
"""
"""
Not sure the best way with some kind of tolerance to hash the poly info
:return:
"""
return hash(self.array().sum())
data = []
for i, face in enumerate(shapeNode.faces):
points = face.getPoints(space="world")
data.append(faceData(i, points, face))
# using sets because they are fast.
setA = set(data)
# debug print the matching polys
print len(data), len(setA)
# select results (speed update candidate here!)
for d in list(setA):
pCore.select("%s.f[%d]" % (shapeNode, d.index), tgl=True)
pCore.mel.invertSelection()
# TODO : merge object back together. Probably duplicate original and transfer normals based on closeest data in the
# final verson.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment