# How to work with Bmesh correctly?

I'm writing a window generator script.

Here is a current version of my script: https://gist.github.com/romanvolodin/3bfd349795443118a2298819349a0b3c

Script's algorithm looks like this (in short):

• Build a wall object
• Build window's inner part
• Get vertex normals on the edge of a window
• For every window build window's decoration

In my scene I have a mesh object ('profile') which is define the decor profile.

To build a window decor, I copy given profile several times to window vertex position and orient it in vertex normal direction.

And then I bridge them together. Kind of 'profile along path'.

I have a super weird bug - the most of the time I get distorted geometry. Something corrupt my normal's data. But sometimes the script works correctly (at 0:07, 1:06, 1:28, 1:37, 1:45, 2:12): https://www.youtube.com/watch?v=I6wysGzIQfU

Also, only 4 windows at the beginning are distorted, 5th and next look just nice. (These crumpled objects at the origin are four first windows.)

Here is more.

In my script I print normal's positions. Then I add a new Bmesh object. And then I print normal's positions again.

    # ...
windows_edge_normals = get_windows_edge_normals(windows_vertex_positions)

for i, window_normals in enumerate(windows_edge_normals, start=1):
print("before", i)
for num, normal in enumerate(window_normals):
vert_co, vert_normal = normal
print(
"   co: {}\n"
"normal {}".format(vert_co, vert_normal)
)

for i, window_normals in enumerate(windows_edge_normals, start=1):
print("after", i)
bm = bmesh.new()
last_profile = None
for num, normal in enumerate(window_normals):
vert_co, vert_normal = normal
print(
"   co: {}\n"
"normal {}".format(vert_co, vert_normal)
)
# ...


And the result is completely different! (Note: 'before' is green and on the right, 'after' red on the left, see link) https://www.diffchecker.com/9YEV3moY

Why?! I've just added a new Bmesh!

So the question is - what I'm doing wrong? Should I delete previous bmeshes explicitly (the wall and inner parts)? Or it's a bug in Blender? Any help will be appreciated.

• If you are placing the trim profile simply rotated 45º at the bottom corners of the windows that will inevitably lead to distortions. That is a geometry problem, not a bug in your code. See related blender.stackexchange.com/questions/56079/… – Duarte Farrajota Ramos Oct 8 '18 at 18:39
• Please see the video. The corners are ok, but first 4 windows not. – Roman Volodin Oct 8 '18 at 18:57
• Looking good. Try updating normals example1 Example2 – batFINGER Oct 9 '18 at 6:34
• I've tried to add bm.normal_update() right after bm = bmesh.new() (line 463) and here is what I've got: when I start fresh Blender and run my script the first time, I get no errors. On next runs I get glitches just like described above. It looks like a memory issue. – Roman Volodin Oct 9 '18 at 13:35
• Hi, to look into this issue I'd need run the code inside Blender. Could you make all files available to reproduce the error? – ideasman42 Oct 24 '18 at 6:11