How to fix invalid mesh (blender crash)

Hi, I’m working on an importer, and it creates the meshes fine, however, they are corrupt, because any attempt to modify a vertex (delete it, merge, remove doubles, etc) crashes blender.

I read this bug http://projects.blender.org/tracker/?func=detail&atid=498&aid=31728&group_id=9
and running .validate on my mesh gives “true”, so it has invalid geometry, the thing is a I don’t know where it is.

Can someone please tell me how to fix the vertex and face array to make a correct mesh?

Thanks a lot

Here’s some code that generates an invalid mesh

import bpy
 
# Define the coordinates of the vertices. Each vertex is defined by 3 consecutive floats.
coords=[(0.10532499849796295, 0.6008009910583496, 0.06658100336790085), (0.08094099909067154, 0.6000720262527466, 0.02625199966132641), (0.09029699862003326, 0.546654999256134, 0.02309899963438511), (0.1172190010547638, 0.5429440140724182, 0.058361999690532684), (0.1519169956445694, 0.607761025428772, 0.08173300325870514), (0.15774700045585632, 0.5481420159339905, 0.07498399913311005), (0.18705500662326813, 0.6142590045928955, 0.0496320016682148), (0.1895419955253601, 0.5570459961891174, 0.044606998562812805), (0.19113899767398834, 0.6183300018310547, -0.0015569999814033508), (0.19600099325180054, 0.564490020275116, 0.00015100000018719584), (0.16698400676250458, 0.6178699731826782, -0.03578300029039383), (0.1736450046300888, 0.5623390078544617, -0.03210499882698059), (0.12651999294757843, 0.6129739880561829, -0.05175299942493439), (0.13587099313735962, 0.5583599805831909, -0.04643400013446808), (0.08691100031137466, 0.6042969822883606, -0.03444800153374672), (0.09717199951410294, 0.5520970225334167, -0.025564000010490417), (0.08094099909067154, 0.6000720262527466, 0.02625199966132641), (0.09029699862003326, 0.546654999256134, 0.02309899963438511), (0.1219870001077652, 0.40807899832725525, -0.0023159999400377274), (0.13589699566364288, 0.44452300667762756, 0.038787998259067535), (0.11418499797582626, 0.4508180022239685, 0.00773000018671155), (0.12279299646615982, 0.459428995847702, -0.027848999947309494), (0.1219870001077652, 0.40807899832725525, -0.0023159999400377274), (0.11418499797582626, 0.4508180022239685, 0.00773000018671155), (-0.1172190010547638, 0.5429440140724182, 0.058361999690532684), (-0.09029699862003326, 0.546654999256134, 0.02309899963438511), (-0.08061300218105316, 0.5997269749641418, 0.024694999679923058), (-0.10639700293540955, 0.6013209819793701, 0.06649500131607056), (-0.15774700045585632, 0.5481420159339905, 0.07498399913311005), (-0.1519169956445694, 0.607761025428772, 0.08173300325870514), (-0.1895419955253601, 0.5570459961891174, 0.044606998562812805), (-0.18705500662326813, 0.6142590045928955, 0.0496320016682148), (-0.19600099325180054, 0.564490020275116, 0.00015100000018719584), (-0.19113899767398834, 0.6183300018310547, -0.0015569999814033508), (-0.1736450046300888, 0.5623390078544617, -0.03210499882698059), (-0.16698400676250458, 0.6178699731826782, -0.03578300029039383), (-0.13587099313735962, 0.5583599805831909, -0.04643400013446808), (-0.12651999294757843, 0.6129739880561829, -0.05175299942493439), (-0.09717199951410294, 0.5520960092544556, -0.025564000010490417), (-0.08400700241327286, 0.6038479804992676, -0.029128000140190125), (-0.09029699862003326, 0.546654999256134, 0.02309899963438511), (-0.08061300218105316, 0.5997269749641418, 0.024694999679923058), (-0.1219870001077652, 0.40807899832725525, -0.0023159999400377274), (-0.11418499797582626, 0.4508180022239685, 0.00773000018671155), (-0.13589699566364288, 0.44452300667762756, 0.038787998259067535), (-0.12279299646615982, 0.459428995847702, -0.027848999947309494), (-0.11418499797582626, 0.4508180022239685, 0.00773000018671155), (-0.1219870001077652, 0.40807899832725525, -0.0023159999400377274), (0.12317200005054474, 0.41634801030158997, -0.043219998478889465), (0.16251200437545776, 0.3845910131931305, -0.07200899720191956), (0.12758900225162506, 0.37647899985313416, -0.05068200081586838), (0.12162899971008301, 0.36807599663734436, -0.010370999574661255), (0.1562660038471222, 0.42409300804138184, -0.0659639984369278), (0.20467999577522278, 0.38839098811149597, -0.06182299926877022), (0.15250900387763977, 0.46880701184272766, -0.048996999859809875), (0.1849460005760193, 0.47387799620628357, -0.042743999511003494), (0.19707700610160828, 0.427962988615036, -0.055792998522520065), (0.22808299958705902, 0.38718798756599426, -0.02747200056910515), (0.2207529991865158, 0.4260050058364868, -0.02013299986720085), (0.21829800307750702, 0.37750500440597534, 0.015149000100791454), (0.2111009955406189, 0.469990998506546, -0.013059999793767929), (0.20432400703430176, 0.45751500129699707, 0.02754100039601326), (0.21116399765014648, 0.412665992975235, 0.020419999957084656), (0.18367500603199005, 0.36844900250434875, 0.037007998675107956), (0.17846600711345673, 0.4040820002555847, 0.04403100162744522), (0.17194199562072754, 0.44300898909568787, 0.0548660010099411), (0.1442669928073883, 0.4043610095977783, 0.027698999270796776), (0.14631600677967072, 0.36534199118614197, 0.02089099958539009), (0.12162899971008301, 0.36807599663734436, -0.010370999574661255), (-0.12162899971008301, 0.36807599663734436, -0.010370999574661255), (-0.12758900225162506, 0.37647899985313416, -0.05068200081586838), (-0.12317200005054474, 0.41634801030158997, -0.043219998478889465), (-0.16251200437545776, 0.3845910131931305, -0.07200899720191956), (-0.1562660038471222, 0.42409300804138184, -0.0659639984369278), (-0.20467999577522278, 0.38839098811149597, -0.06182299926877022), (-0.15250900387763977, 0.46880701184272766, -0.048996999859809875), (-0.1849460005760193, 0.47387799620628357, -0.042743999511003494), (-0.19707700610160828, 0.427962988615036, -0.055792998522520065), (-0.22808299958705902, 0.38718798756599426, -0.02747200056910515), (-0.2207529991865158, 0.4260050058364868, -0.02013299986720085), (-0.21829800307750702, 0.37750500440597534, 0.015149000100791454), (-0.2111009955406189, 0.469990998506546, -0.013059999793767929), (-0.20432400703430176, 0.45751500129699707, 0.02754100039601326), (-0.21116399765014648, 0.412665992975235, 0.020419999957084656), (-0.18367500603199005, 0.36844900250434875, 0.037007998675107956), (-0.17846600711345673, 0.4040820002555847, 0.04403100162744522), (-0.17194199562072754, 0.44300898909568787, 0.0548660010099411), (-0.14631600677967072, 0.36534199118614197, 0.02089099958539009), (-0.1442669928073883, 0.4043610095977783, 0.027698999270796776), (-0.12162899971008301, 0.36807599663734436, -0.010370999574661255)]
 
# Define the faces by index numbers. Each faces is defined by 4 consecutive integers.
# For triangles you need to repeat the first vertex also in the fourth position.
faces=[(2, 1, 0, 2), (3, 2, 0, 3), (4, 3, 0, 4), (5, 3, 4, 5), (6, 5, 4, 6), (7, 5, 6, 7), (8, 7, 6, 8), (9, 7, 8, 9), (10, 9, 8, 10), (11, 9, 10, 11), (12, 11, 10, 12), (13, 11, 12, 13), (14, 13, 12, 14), (15, 13, 14, 15), (16, 15, 14, 16), (17, 15, 16, 17), (20, 19, 18, 20), (23, 22, 21, 23), (26, 25, 24, 26), (27, 26, 24, 27), (28, 27, 24, 28), (29, 27, 28, 29), (30, 29, 28, 30), (31, 29, 30, 31), (32, 31, 30, 32), (33, 31, 32, 33), (34, 33, 32, 34), (35, 33, 34, 35), (36, 35, 34, 36), (37, 35, 36, 37), (38, 37, 36, 38), (39, 37, 38, 39), (40, 39, 38, 40), (41, 39, 40, 41), (44, 43, 42, 44), (47, 46, 45, 47), (50, 49, 48, 50), (51, 50, 48, 51), (22, 51, 48, 22), (21, 22, 48, 21), (49, 52, 48, 49), (52, 21, 48, 52), (53, 52, 49, 53), (54, 21, 52, 54), (55, 54, 52, 55), (53, 56, 52, 53), (53, 57, 56, 53), (55, 52, 56, 55), (58, 55, 56, 58), (57, 58, 56, 57), (57, 59, 58, 57), (60, 55, 58, 60), (61, 60, 58, 61), (62, 61, 58, 62), (59, 62, 58, 59), (59, 63, 62, 59), (64, 61, 62, 64), (63, 64, 62, 63), (64, 65, 61, 64), (19, 65, 64, 19), (66, 19, 64, 66), (63, 66, 64, 63), (67, 66, 63, 67), (67, 68, 66, 67), (18, 66, 68, 18), (19, 66, 18, 19), (71, 70, 69, 71), (72, 70, 71, 72), (47, 71, 69, 47), (45, 71, 47, 45), (73, 71, 45, 73), (73, 72, 71, 73), (74, 72, 73, 74), (45, 75, 73, 45), (76, 73, 75, 76), (77, 74, 73, 77), (78, 74, 77, 78), (76, 77, 73, 76), (79, 77, 76, 79), (79, 78, 77, 79), (80, 78, 79, 80), (76, 81, 79, 76), (82, 79, 81, 82), (82, 83, 79, 82), (80, 79, 83, 80), (84, 80, 83, 84), (82, 85, 83, 82), (85, 84, 83, 85), (86, 85, 82, 86), (86, 44, 85, 86), (88, 87, 84, 88), (85, 88, 84, 85), (44, 88, 85, 44), (89, 87, 88, 89), (42, 89, 88, 42), (44, 42, 88, 44)]
 
me = bpy.data.meshes.new("Test")   # create a new mesh  
 
ob = bpy.data.objects.new("Test", me)          # create an object with that mesh
ob.location = bpy.context.scene.cursor_location   # position object at 3d-cursor
bpy.context.scene.objects.link(ob)                # Link object to scene
 
# Fill the mesh with verts, edges, faces 
me.from_pydata(coords,[],faces)   # edges or faces should be [], or you ask for problems
me.update(calc_edges=True)    # Update mesh with new data

There is a problem with your vertex indices in your face list.



import bpy

coords=[(0.10532499849796295, 0.6008009910583496, 0.06658100336790085), (0.08094099909067154, 0.6000720262527466, 0.02625199966132641), (0.09029699862003326, 0.546654999256134, 0.02309899963438511), (0.1172190010547638, 0.5429440140724182, 0.058361999690532684), (0.1519169956445694, 0.607761025428772, 0.08173300325870514), (0.15774700045585632, 0.5481420159339905, 0.07498399913311005), (0.18705500662326813, 0.6142590045928955, 0.0496320016682148), (0.1895419955253601, 0.5570459961891174, 0.044606998562812805), (0.19113899767398834, 0.6183300018310547, -0.0015569999814033508), (0.19600099325180054, 0.564490020275116, 0.00015100000018719584), (0.16698400676250458, 0.6178699731826782, -0.03578300029039383), (0.1736450046300888, 0.5623390078544617, -0.03210499882698059), (0.12651999294757843, 0.6129739880561829, -0.05175299942493439), (0.13587099313735962, 0.5583599805831909, -0.04643400013446808), (0.08691100031137466, 0.6042969822883606, -0.03444800153374672), (0.09717199951410294, 0.5520970225334167, -0.025564000010490417), (0.08094099909067154, 0.6000720262527466, 0.02625199966132641), (0.09029699862003326, 0.546654999256134, 0.02309899963438511), (0.1219870001077652, 0.40807899832725525, -0.0023159999400377274), (0.13589699566364288, 0.44452300667762756, 0.038787998259067535), (0.11418499797582626, 0.4508180022239685, 0.00773000018671155), (0.12279299646615982, 0.459428995847702, -0.027848999947309494), (0.1219870001077652, 0.40807899832725525, -0.0023159999400377274), (0.11418499797582626, 0.4508180022239685, 0.00773000018671155), (-0.1172190010547638, 0.5429440140724182, 0.058361999690532684), (-0.09029699862003326, 0.546654999256134, 0.02309899963438511), (-0.08061300218105316, 0.5997269749641418, 0.024694999679923058), (-0.10639700293540955, 0.6013209819793701, 0.06649500131607056), (-0.15774700045585632, 0.5481420159339905, 0.07498399913311005), (-0.1519169956445694, 0.607761025428772, 0.08173300325870514), (-0.1895419955253601, 0.5570459961891174, 0.044606998562812805), (-0.18705500662326813, 0.6142590045928955, 0.0496320016682148), (-0.19600099325180054, 0.564490020275116, 0.00015100000018719584), (-0.19113899767398834, 0.6183300018310547, -0.0015569999814033508), (-0.1736450046300888, 0.5623390078544617, -0.03210499882698059), (-0.16698400676250458, 0.6178699731826782, -0.03578300029039383), (-0.13587099313735962, 0.5583599805831909, -0.04643400013446808), (-0.12651999294757843, 0.6129739880561829, -0.05175299942493439), (-0.09717199951410294, 0.5520960092544556, -0.025564000010490417), (-0.08400700241327286, 0.6038479804992676, -0.029128000140190125), (-0.09029699862003326, 0.546654999256134, 0.02309899963438511), (-0.08061300218105316, 0.5997269749641418, 0.024694999679923058), (-0.1219870001077652, 0.40807899832725525, -0.0023159999400377274), (-0.11418499797582626, 0.4508180022239685, 0.00773000018671155), (-0.13589699566364288, 0.44452300667762756, 0.038787998259067535), (-0.12279299646615982, 0.459428995847702, -0.027848999947309494), (-0.11418499797582626, 0.4508180022239685, 0.00773000018671155), (-0.1219870001077652, 0.40807899832725525, -0.0023159999400377274), (0.12317200005054474, 0.41634801030158997, -0.043219998478889465), (0.16251200437545776, 0.3845910131931305, -0.07200899720191956), (0.12758900225162506, 0.37647899985313416, -0.05068200081586838), (0.12162899971008301, 0.36807599663734436, -0.010370999574661255), (0.1562660038471222, 0.42409300804138184, -0.0659639984369278), (0.20467999577522278, 0.38839098811149597, -0.06182299926877022), (0.15250900387763977, 0.46880701184272766, -0.048996999859809875), (0.1849460005760193, 0.47387799620628357, -0.042743999511003494), (0.19707700610160828, 0.427962988615036, -0.055792998522520065), (0.22808299958705902, 0.38718798756599426, -0.02747200056910515), (0.2207529991865158, 0.4260050058364868, -0.02013299986720085), (0.21829800307750702, 0.37750500440597534, 0.015149000100791454), (0.2111009955406189, 0.469990998506546, -0.013059999793767929), (0.20432400703430176, 0.45751500129699707, 0.02754100039601326), (0.21116399765014648, 0.412665992975235, 0.020419999957084656), (0.18367500603199005, 0.36844900250434875, 0.037007998675107956), (0.17846600711345673, 0.4040820002555847, 0.04403100162744522), (0.17194199562072754, 0.44300898909568787, 0.0548660010099411), (0.1442669928073883, 0.4043610095977783, 0.027698999270796776), (0.14631600677967072, 0.36534199118614197, 0.02089099958539009), (0.12162899971008301, 0.36807599663734436, -0.010370999574661255), (-0.12162899971008301, 0.36807599663734436, -0.010370999574661255), (-0.12758900225162506, 0.37647899985313416, -0.05068200081586838), (-0.12317200005054474, 0.41634801030158997, -0.043219998478889465), (-0.16251200437545776, 0.3845910131931305, -0.07200899720191956), (-0.1562660038471222, 0.42409300804138184, -0.0659639984369278), (-0.20467999577522278, 0.38839098811149597, -0.06182299926877022), (-0.15250900387763977, 0.46880701184272766, -0.048996999859809875), (-0.1849460005760193, 0.47387799620628357, -0.042743999511003494), (-0.19707700610160828, 0.427962988615036, -0.055792998522520065), (-0.22808299958705902, 0.38718798756599426, -0.02747200056910515), (-0.2207529991865158, 0.4260050058364868, -0.02013299986720085), (-0.21829800307750702, 0.37750500440597534, 0.015149000100791454), (-0.2111009955406189, 0.469990998506546, -0.013059999793767929), (-0.20432400703430176, 0.45751500129699707, 0.02754100039601326), (-0.21116399765014648, 0.412665992975235, 0.020419999957084656), (-0.18367500603199005, 0.36844900250434875, 0.037007998675107956), (-0.17846600711345673, 0.4040820002555847, 0.04403100162744522), (-0.17194199562072754, 0.44300898909568787, 0.0548660010099411), (-0.14631600677967072, 0.36534199118614197, 0.02089099958539009), (-0.1442669928073883, 0.4043610095977783, 0.027698999270796776), (-0.12162899971008301, 0.36807599663734436, -0.010370999574661255)]
faces=[(2, 1, 0, 2), (3, 2, 0, 3), (4, 3, 0, 4), (5, 3, 4, 5), (6, 5, 4, 6), (7, 5, 6, 7), (8, 7, 6, 8), (9, 7, 8, 9), (10, 9, 8, 10), (11, 9, 10, 11), (12, 11, 10, 12), (13, 11, 12, 13), (14, 13, 12, 14), (15, 13, 14, 15), (16, 15, 14, 16), (17, 15, 16, 17), (20, 19, 18, 20), (23, 22, 21, 23), (26, 25, 24, 26), (27, 26, 24, 27), (28, 27, 24, 28), (29, 27, 28, 29), (30, 29, 28, 30), (31, 29, 30, 31), (32, 31, 30, 32), (33, 31, 32, 33), (34, 33, 32, 34), (35, 33, 34, 35), (36, 35, 34, 36), (37, 35, 36, 37), (38, 37, 36, 38), (39, 37, 38, 39), (40, 39, 38, 40), (41, 39, 40, 41), (44, 43, 42, 44), (47, 46, 45, 47), (50, 49, 48, 50), (51, 50, 48, 51), (22, 51, 48, 22), (21, 22, 48, 21), (49, 52, 48, 49), (52, 21, 48, 52), (53, 52, 49, 53), (54, 21, 52, 54), (55, 54, 52, 55), (53, 56, 52, 53), (53, 57, 56, 53), (55, 52, 56, 55), (58, 55, 56, 58), (57, 58, 56, 57), (57, 59, 58, 57), (60, 55, 58, 60), (61, 60, 58, 61), (62, 61, 58, 62), (59, 62, 58, 59), (59, 63, 62, 59), (64, 61, 62, 64), (63, 64, 62, 63), (64, 65, 61, 64), (19, 65, 64, 19), (66, 19, 64, 66), (63, 66, 64, 63), (67, 66, 63, 67), (67, 68, 66, 67), (18, 66, 68, 18), (19, 66, 18, 19), (71, 70, 69, 71), (72, 70, 71, 72), (47, 71, 69, 47), (45, 71, 47, 45), (73, 71, 45, 73), (73, 72, 71, 73), (74, 72, 73, 74), (45, 75, 73, 45), (76, 73, 75, 76), (77, 74, 73, 77), (78, 74, 77, 78), (76, 77, 73, 76), (79, 77, 76, 79), (79, 78, 77, 79), (80, 78, 79, 80), (76, 81, 79, 76), (82, 79, 81, 82), (82, 83, 79, 82), (80, 79, 83, 80), (84, 80, 83, 84), (82, 85, 83, 82), (85, 84, 83, 85), (86, 85, 82, 86), (86, 44, 85, 86), (88, 87, 84, 88), (85, 88, 84, 85), (44, 88, 85, 44), (89, 87, 88, 89), (42, 89, 88, 42), (44, 42, 88, 44)]

#coords=[(1.0, 1.0, 0.0), (-1.0, 1.0, 0.0), (-1.0, -1.0, 0.0), (1.0, -1.0, 0.0)]
#faces = [(0, 1, 2, 3)]


test = []

for i in faces:
    i = list(i)
    i.pop(-1)
    test.append(i)

print(test)

me = bpy.data.meshes.new('Test')

me.from_pydata(coords, [], test)
me.update()

ob = bpy.data.objects.new('Test', me)
bpy.context.scene.objects.link(ob)

bpy.context.scene.objects.active = ob
bpy.context.scene.update()


to prevent crash:
http://www.blender.org/documentation/blender_python_api_2_65_release/bpy.types.Mesh.html#bpy.types.Mesh.validate

Thanks zmj! it seems that I only needed 3 vertex indices at the end to form triangles, I was confused with this
“For triangles you need to repeat the first vertex also in the fourth position.” in the documentation, that was what I was doing and didn’t realize it was wrong (actually tried that before, but other error not related made me thought it wasn’t the correct way)

Thanks Campbell, validate() worked well, the problematic faces were removed.
I gotta try to use it before executing the mesh creation just in case, I crashed blender so many times while figuring out how to properly read a format and make correct arrays :stuck_out_tongue: (I guess “from_pydata” still have some minor issues catching invalid inputs)