Blender detects wrong number of faces after triangulating. Am I missing a step?

I’m writing a script which removed overhangs from molds. As my algorithm only deals with trigons, one of the first things I do is triangulate faces. This doubles the amoung of faces in the mesh, however, when my script iterates through the list of faces in the mesh, it only detects half as many of them as it should. I think it’s failing to recognise that I just split every face in two and thinks that there’s the old number of faces.

Is there a line of code I’m missing to update it?

Main function, contains the code which triangulates faces (among other things):

def main (draftAngle, direction, applyBooleans, preservePrisms):
    #Opening Variables
    zBoundary = findZBoundary(direction)
    faceList = []
    prismList = []
    moldObject = bpy.context.scene.objects.active
    
    #Select all
    bpy.ops.mesh.select_all(action = "SELECT")
    
    #Convert to tris
    bpy.ops.mesh.quads_convert_to_tris(quad_method='BEAUTY', ngon_method='BEAUTY')
    
    #Generate list of upward/downward oriented faces
    faceList = findFaces (direction)
    
    #Create prisms
    prismList = createPrisms(faceList, moldObject, draftAngle, direction, zBoundary)
    
    #Remove overhangs
    removeOverhangs (prismList, moldObject, applyBooleans, preservePrisms)
    
    #Clean up mold
    cleanUpMold ()
    
    print ("Mold cleaned up")
    
    #Return to execute
    return

And the code that runs through the faces:

def findFaces(direction):
    #Opening Variables
    faceList = []
    faceIndex = 0
    moldObject = bpy.context.scene.objects.active
    
    #Deleteme
    x = 0
    print ("############################################")
    print ("####              Find Faces            ####")
    print ("############################################")
    
    #Loop through faces, determining which ones face up
    for poly in moldObject.data.polygons:
        #Deleteme
        v1 = moldObject.data.vertices[poly.vertices[0]]
        v2 = moldObject.data.vertices[poly.vertices[1]]
        v3 = moldObject.data.vertices[poly.vertices[2]]
        
        #Deleteme
        print ("Polygon no." + str(x))
        print ("V1 = " + str(v1.co))
        print ("V2 = " + str(v2.co))
        print ("V3 = " + str(v3.co))
        x += 1
        
        #Convert local normal to global
        normal = moldObject.matrix_world * poly.normal
        
        #Deleteme
        print("Normal = ", str(normal))
        
        #Determines global orientation of polygon
        if normal.z > 0 and direction == "Up":
            #Add index to facelist
            faceList.append(faceIndex)
            
            #Deleteme
            print("This polygon faces up!")
            
        elif normal.z < 0 and direction == "Down":
            #Add index to facelist
            faceList.append(faceIndex)
            
            #Deleteme
            print("This polygon faces down!")
        
        #Iterate index
        faceIndex += 1
    
    #Return result
    return faceList

Pretty sure the findFaces script is working ok? i added in to the if if normal.z > 0 and direction == “Up”: part poly.select = True and poly.select = False and it selected the upwards faces fine



def findFaces(direction):
    #Opening Variables
    faceList = []
    faceIndex = 0
    moldObject = bpy.context.scene.objects.active
    #Deleteme
    x = 0
    print ("############################################")
    print ("####              Find Faces            ####")
    print ("############################################")
    for poly in moldObject.data.polygons:
        #Deleteme
        v1 = moldObject.data.vertices[poly.vertices[0]]
        v2 = moldObject.data.vertices[poly.vertices[1]]
        v3 = moldObject.data.vertices[poly.vertices[2]]
        #Deleteme
        print ("Polygon no." + str(x))
        print ("V1 = " + str(v1.co))
        print ("V2 = " + str(v2.co))
        print ("V3 = " + str(v3.co))
        x += 1
        #Convert local normal to global
        normal = moldObject.matrix_world * poly.normal
        #Deleteme
        print("Normal = ", str(normal))
        #Determines global orientation of polygon
        if normal.z > 0 and direction == "Up":
            #Add index to facelist
            faceList.append(faceIndex)
            #Deleteme        
            print("This polygon faces up!")        
            poly.select = True
        elif normal.z < 0 and direction == "Down":
            #Add index to facelist
            faceList.append(faceIndex)
            #Deleteme
            print("This polygon faces down!")
            poly.select = False
        #Iterate index
        faceIndex += 1
    #Return result
    return faceList


I did have to run this script whilst in Object Mode, not Edit Mode… as blenders edit mode would need to be synced to be represented correctly.

Ah! That could be the problem, it is indeed running in edit mode. If I triangulate faces, exit to object mode, then reenter edit mode will that perform the update? Or is there a function I can call which will update it?

The function of that script is to generate a list containing the index of every poly facing up (if direction is set to “Up”) or down (if it’s set to “Down”). It’s not supposed to select anything if that’s what you were thinking.

Later on in my code I use that list to generate geometry, but whatever’s selected/unselected doesn’t really matter. I only select all at the beginning of the code to ensure everything gets triangulated.

Alternatively… I guess I could run most of that in object mode. I think it’d only need to be in edit mode to triangulate the mesh and do cleanup operations (remove doubles, recalculate normals etc.).

I just tried it out and that seems to be a solution. If you exit to object mode, then return to edit mode after triangulating, it updates the mesh and detects the correct number of faces.