Destructive bmesh operation in a modal operator

are there any best practices for this? doing a non-destructive bmesh op in modal is trivial… you just update the existing geometry, but what do you do when the geometry is constantly changing?

For example, say I want to make a custom modal bridge operator. The operator starts with two edges selected by the user, a bmesh is created in invoke(), and then bmesh.ops.bridge_loops gets called in modal(), but after that all of the geometry is changed. The original selection we started with is no longer valid, so if we wanted to have modal controls to add edge loops or control the profile (or whatever, this is all hypothetical), we’ve lost our original selection. worse- we have all of this new geometry that wasn’t part of the original mesh now.

So that thought exercise got me thinking- what exactly is the best practice for something like this? The best I could come up with is creating a temporary copy of the original mesh with the original selection and essentially re-build the mesh at the beginning of every modal update, but with the updated parameters. That seems… wrong. That can’t be it, can it?

surely I’m not the first one to think of this problem… anybody?

I don’t know ehat is Best practice, but You can always use undo push and redo

The best practice would be to not draw geometry using bmesh. While in modal, draw the proposed changes with bgl/gpu. On completion of the modal, write the changes to the mesh.

I do this for additive modal operators, but IMO it’s a poor choice for something that is subtractive. Bridge is additive, so it would work in this situation- but what about Bevel (or something similar). In order to see the effective change you’d need to disable depth testing and draw your GL polygons on top of everything while the old geometry sits behind it unchanged.

It’s an effective workaround in some cases but I wouldn’t call it a best practice. What I’m looking for here is a solution that can look at feel (to the end user) like a built in modal geometry operator like Bevel.

With Undo effectively being a reload of the entire scene graph, I think I’d prefer rebuilding my bmesh from an off-screen copy of the object each frame, it’s surely faster than reloading the whole scene

If You are in Editmode and do undo of editmode operation scene in not reloaded (only entire mesh).
You can manualy store initial bmesh and on each modal loop restore back that initial state.

There are not many ppl that done addon with modal mesh operation. You can check how they did that. I’m aware of: MeshMachine, OffsetEdges(2.7x and 2.8 version had modal). (on 2.81+ it crash) - undo-redo-operator - you can chceck performance