I am updating as well as trying to optimize an add-on that I haven't touched in a couple of years. I learned python as I was making that add-on back in the day, actually, and now am reading that the code that I came up with is what's commonly referred to as "Spaghetti Code".
Anyway, my add-on, amongst other things, is doing a certain number of bmesh extrudes each time the modal runs. So in order to be able to control the amount of extrudes it does, I found that the old add-on is deleting all the previously extruded vertices, in the beginning of def modal(). And so it doesn't end up with an ungodly amount of extrudes due to the mouse moving hundreds of pixels, but rather a certain number that the user can control.
So in the old add-on, which I'm trying to optimize and update, the deletion apparently only works if bm is made into a global variable. I've read that global variables are at least something to be careful with, so I'm not sure if this is 'A-OK'? From what I understand, my past self figured out that without having access to the bmesh data from the previous 'modal run' he would not be able to delete the vertices and avoid a ton of extrudes, and that exposing bmesh.from_edit_mesh() globally would solve that problem. However, can anyone suggest a better way? Or perhaps confirm if making bm a global variable is a decent solution for a blender modal operator?
def modal(self, context, event):
global bm
obj = bpy.context.edit_object
me = obj.data
bm = bmesh.from_edit_mesh(me)