Terrible issue with vertex number optimization

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
anders211
Posts: 0
Joined: Tue May 07, 2013 10:18 pm

Terrible issue with vertex number optimization

Post by anders211 » Sun Jul 07, 2013 8:41 pm

Hi,
As we know for example a cube consists of 8 vertices. However if we don't do any optimization when we export our model to .x file we will have 36 vertices instead of 8. So I wrote a program (modify some script) which let us to optimize this and has 8 vertex instead of 36. However the problem is that with this solution I can use only one uv layer map. So now my aim is to create program which can let us to have uv map layers as much as possible. And I have a problem here. At the below there is my code. Input is 'Mesh.polygons', output should be:
OriginalVertexIndices - which includes real vertex indices from mesh.polygons retrieved in the order of gathering vertices from polygons
RealVertexIndices - which includes a new numbers of vertex indices
DuplicatedVertexIndices - if DuplicatedVertexIndices=i then vertex should be write to the .x file

Code: Select all

 class _UnrolledFacesMeshEnumerator(_MeshEnumerator):
        def __init__(self, Mesh):
            MeshExportObject._MeshEnumerator.__init__(self, Mesh)
            
            self.vertices = tuple()
            self.OriginalVertexIndices = []
            self.DuplicateVertexIndices = []
            self.RealVertexIndices = []
			
            secondUvMap = []
            
            counter = 0
            Index2 = 0
            for Polygon in Mesh.polygons:
                
                if counter % 1000 == 0:
                    print("counter ", counter, "\n")
                counter += 1
                
                Index = len(self.vertices)  
                           
                self.vertices += tuple(Mesh.vertices[VertexIndex]
                    for VertexIndex in Polygon.vertices)
					
                print("active ul layer name = ", Mesh.uv_layers.active.name)
                          
                prevSize = len(self.OriginalVertexIndices)
                for VertexIndex in Polygon.vertices:
                    value = Index
                    for i in range(prevSize):
                        if VertexIndex == self.OriginalVertexIndices[i]:
                            
                            if secondUvMap[self.RealVertexIndices[i]] == True:
                                print("duplicated vertex ", i)
                                value = i
                                self.RealVertexIndices.append(self.RealVertexIndices[i])
                                secondUvMap.append(True)
                                break;
						
						    #check if it is in another uv map
                            saveVertex = False
                            for uvlayer in Mesh.uv_layers:
                                print("uvlayer name = ", uvlayer.name)
                                if uvlayer.name != Mesh.uv_layers.active.name:
                                    if uvlayer.data[VertexIndex].pin_uv == True: #uvlayer.data[VertexIndex].uv[0] != 0 or uvlayer.data[VertexIndex].uv[1] != 0:
                                        print("save vertex ", i, " == ", Index2)
                                        saveVertex = True
                                        break
							
							#if not then:
                            if saveVertex == False:
                                print("duplicated vertex ", i)
                                value = i
                                self.RealVertexIndices.append(self.RealVertexIndices[i])
                                secondUvMap.append(True)
                            else: #add vertex as it differs from anyone despite it has the same coordinates
                                value = Index
                                self.RealVertexIndices.append(Index2)
                                secondUvMap[self.RealVertexIndices[i]] = True
                                secondUvMap.append(True)
                                Index2 += 1
                            break
                    else:
                        self.RealVertexIndices.append(Index2)
                        print("save vertex ", Index2)
                        Index2 += 1
                        secondUvMap.append(False)
                    self.DuplicateVertexIndices.append(value)
                    Index += 1
                
                for VertexIndex in Polygon.vertices:
                    self.OriginalVertexIndices.append(VertexIndex)
                          
            
            self.PolygonVertexIndexes = []
            Index = 0
            for Polygon in Mesh.polygons:
                self.PolygonVertexIndexes.append(tuple(range(Index, 
                    Index + len(Polygon.vertices))))
                Index += len(Polygon.vertices)
			
            self.vertexNumber = Index2

Post Reply