Blender chrashs after adding Faces with Python

Hi there,

I’m currently making a tool for fast and accurat prototyping or reconstruction of Bullets and shells.
Now to make that I’d give the outline to a ‘spin’-funktion.

All works fine, except when I add faces to the cartridge.

Then, once I toggle to editmode and delete at least 2 Vertices, blender chrashs.

As I’m unable to find my error, here’s a havily reduced version of my code.

import bpy
from math import sin, cos, radians
from mathutils import Vector, Matrix

def add_Mobj(name, V=[], F=[], E=[], Chain=False, active=True, sel=True):
    me = bpy.data.meshes.new(name)
    if Chain:
        E1 = list(range(len(V)))
        E2 = list([[i+1, 0][i==len(V)] for i in E1])
        E  = list([[E1[i],E2[i]]for i in E1])
        E.pop()
    me.from_pydata(V,E,F)
    ob = bpy.data.objects.new(name, me)
    bpy.context.scene.objects.link(ob)
    if active: bpy.context.scene.objects.active = ob
    ob.select = sel
    return ob

# Aufgabe: spin\screw mit konstanten durchmessern.
# Wenn sie verbunden weden sollen, werden die vertices per edges verbunden,
# und per edges verbundene Vertices werden per faces verbunden.
# Ausgabe:  Verts als plainlist beginnend mit der Eingabe.
#           Edges als plainlist beginnend mit der Eingabe.*
#           Faces als plainlist beginnend mit der Eingabe.*
#           *Jeweils mit den indices passend fuer die Ausgabe.
def spin(VS=[], ES=[], Chain=False, n=32, phi=None, axis=(1,0,0),
         screw=0, base=(0,0,0), connect=True, close=False):
    Vs, Es, Fs = [], [], []
    a = [phi,360][phi==None]/[n, n-1][close and phi!=None]
    axis = Vector(axis).normalized()
    base = Vector(base)
    R_mat = Matrix.Rotation(radians(a), 4, axis)
    VSb = [Vector(V)-base for V in VS]
    L_VS = len(VS)
    if Chain:
        E1 = list(range(len(VS)))
        E2 = list([[i+1, 0][i==len(VS)] for i in E1])
        ES  = list([[E1[i],E2[i]]for i in E1])
        ES.pop()
    for stp in range(n + [1, 0][close]):
        for V in VSb:
            if stp>=1:
                for s in range(stp): V = R_mat * V
            Vs.append(V+base+screw*axis)
        if connect:
            for h, i in ES: # spalt
                k = h + (stp) * L_VS
                l = i + (stp) * L_VS
                Es.append([k,l])
            if stp==0:
                continue
            for i in range(L_VS): # ring
                l = i + (stp) * L_VS
                k = l - L_VS
                Es.append([k,l])
#            for h, i in ES: # flaeche
 #               k = h + (stp) * L_VS
  #              l = i + (stp) * L_VS
   #             K = k - L_VS
    #            L = l - L_VS
     #           Fs.append([K, k, l, L])
    if close:
        for i in range(L_VS):
            k = i + (stp) * L_VS
            l = i
            Es.append([k,l])
#        for h, i in ES: # flaeche
 #           k = h + (stp) * L_VS
  #          l = i + (stp) * L_VS
   #         Fs.append([k, h, i, l])
    else:
        for h, i in ES:
            k = h + stp * L_VS
            l = i + stp * L_VS
            Es.append([k, l])
    return Vs, Es, Fs    

Z  = 0.4    # Radius Zuendhuetchen
R1 = 1.0    # Radius basis
R2 = 0.7    # Radius hals
bR1 = 0.2   # Rand breite
hR1 = 0.2   # Rand laenge
oR1 = 20   # Rand anschnitt
bR2 = 0.1   # Rille tiefe
hR2 = 0.3   # Rille laenge
oR2 = 60    # Rille anschnitt
L1 = 3.0    # laenge basis
L2 = 0.5    # laenge schulter
L3 = 1.0    # laenge hals

Verts = []
#V0 = ()
V1 = (0, Z, 0)
V2 = (0, R1+bR1-hR1*0.01*[0,oR1][10<=oR1<=50], 0)
V3 = (hR1*0.01*[0,oR1][10<=oR1<=50], R1+bR1, 0)
V4 = (hR1, R1+bR1, 0)
V5 = (hR1, R1-bR2, 0)
V6 = (hR1+hR2*0.01*[0,oR2][10<=oR2], R1-bR2, 0)
V7 = (hR1+hR2, R1, 0)
V8 = (L1, R1, 0)
V9 = (L1+L2, R2, 0)
V10= (L1+L2+L3, R2, 0)
#V11 = ()
#V12 = ()
Verts += [V1, V2]
if 10<=oR1<=50: Verts.append(V3)
Verts.append(V4)
Verts.append(V5)
if 10<=oR2: Verts.append(V6)
Verts.append(V7)
Verts.append(V8)
if R2!=R1:
    if L2>0: Verts.append(V9)
    if L3>0: Verts.append(V10)

Verts, Edges, Faces = spin(Verts, Chain=True, close=True)
add_Mobj("Cartridge", Verts, Faces, Edges)#, Chain=True)

Semi-solved:

While i still dont know what triggers this behavior, in my special case its solved by using mesh.update(calc_edges=True).
Important is that mesh.update is used WITH calc_edges=True. mesh.update() on its own wont help at all.

Still if anyone has an idea, …

Perhaps you may need to update bmesh.