FacesManager
In order to find faster what faces should be connected, I created a class called FacesManager to manage the existing faces. It can be used when you want to get the faces which have a specified vertex in it. If you give a vertex, then FacesManager will return the faces which are connected to that vertex.
|
a sample configuration of vertices and faces to explain how FacesManager manages the data of the faces. |
Now let me show you briefly how FacesManager manages the data of the faces. I assume that you have a configuration of vertices and faces like the left image.
The vertices are named by the numbers 0 to 4 as seen in the image, which has two faces in it. And the data structure which the class has in this case is as follows ([] means an array and it is the same symbol as Python language):
Data = [ [[0,1,4],[0,3,4]], [[0,1,4]], [], [[0,3,4]], [[0,1,4],[0,3,4]] ]
When the structure has such a style, you can easily get the faces which a given vertex makes, for example, if you give vertex 0 to the class, then it can easily get the set [[0,1,4],[0,3,4]] by calling Data[0]. The set represents two faces which are formed with the vertices 0,1,4 and 0,3,4, and you can see the both faces have the vertex 0, which you just gave.
Codes of FacesManager
#******************************
#---Faces manager
#******************************
class FacesManager:
def __init__(self, inMesh):
self.Mesh = inMesh
self.Verts = [[] for i in xrange(len(inMesh.verts))] # This will waste huge memory if many verts are given
def Import(self):
lcFaces = []
for enFace in self.Mesh.faces:
lcFaces.append([enVert.index for enVert in enFace.verts])
self.Extend(lcFaces)
def Extend(self, inFaces):
for enFace in inFaces:
enFace = enFace[:] # copy of array to sort
enFace.sort()
for i in enFace:
lcFaces = self.Verts[i]
if enFace in lcFaces: continue
lcFaces.append(enFace)
def Delete(self, inFaces):
for enFace in inFaces:
enFace = enFace[:] # copy of array to sort
enFace.sort()
for i in enFace:
lcFaces = self.Verts[i]
lcFaces.remove(enFace) # Error will occer if specified face isn't found
def __getitem__(self, inIndex):
return self.Verts[inIndex]
def Bake(self):
# Transform Verts array into Blender format
tmpFaces = []
for i in xrange(len(self.Verts)):
enFaces = self.Verts[i]
for enFace in enFaces:
# Add face to make
tmpFaces.append(enFace)
# Remove other same faces in Verts
for j in enFace:
if i == j: continue
enOtherFaces = self.Verts[j]
enOtherFaces.remove(enFace)
self.Verts[i] = None
del self.Verts
# Make faces
self.Mesh.faces.extend(tmpFaces)
Codes to Test FacesManager
# Code to test faces manager functionarity
lcFM = FacesManager(B.Mesh.Get("Mesh1"))
lcFM.Import()
print lcFM.Verts
lcFM.Extend([[0,1,2],[3,4,5]])
print lcFM.Verts
lcFM.Delete([[3,4,5]])
print lcFM.Verts
print lcFM[0]
lcFM.Bake()
|