T Junction remover

Hi, just whipped up this script because my colleague needed to remove T junctions in a model and couldent find a mel script.

Its slow and not nice code but does the job ok- main problem is no support for UV’s/VCols - it wont remove vcols


from Blender import *

MidpointVecs= Mathutils.MidpointVecs

def findvert(me, loc):
    for v in me.verts:
        if (v.co - loc).length < 0.00001:
            return v



scn = Scene.GetCurrent()
me= scn.objects.active.getData(mesh=1)

Window.EditMode(0)
Mesh.Mode(Mesh.SelectModes.EDGE)
count = 0 
ok= True
while ok:
    ok= False
    edges= list(me.edges)
    edges.sort(lambda b,a: cmp(a.length, b.length))
    
    for ed in edges:
        l= ed.length
        if l < 0.001:
            continue
        
        v1 = ed.v1
        v2 = ed.v2
        
        co1 = v1.co.copy()
        co2 = v2.co.copy()
        
        
        subdiv= False
        for v in me.verts:
            if v != v1 and v != v2 and v.co != co1 and v.co != co2:
                co= v.co.copy()
                w1=(co-co1).length 
                w2=(co-co2).length
                
                if abs(l - (w1+w2)) < 0.00001:
                    # SPLIT
                    me.sel= False
                    ed.sel = True
                    me.subdivide(0)
                    me= scn.objects.active.getData(mesh=1)
                    
                    print "subdiv", count
                    count+=1
                    
                    v_new = findvert(me, (co1+co2)/2 )
                    if not v_new:
                        raise 'error'
                    
                    v_new.co= co
                    me.sel= True
                    me.remDoubles(0.0001)
                    me.sel= False
                    me= scn.objects.active.getData(mesh=1)
                    Window.RedrawAll()
                    subdiv= True
                    ok= True
                    break
        if subdiv == True:
            me= scn.objects.active.getData(mesh=1)
            break    

me.sel= True
me.remDoubles(0.0001)