Skip to content

Instantly share code, notes, and snippets.

@Hoikas
Last active May 26, 2020 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hoikas/e742393dbb672ae31268652a44c2e2f3 to your computer and use it in GitHub Desktop.
Save Hoikas/e742393dbb672ae31268652a44c2e2f3 to your computer and use it in GitHub Desktop.
import bmesh
import bpy
from contextlib import contextmanager
@contextmanager
def bmesh_object(name):
mesh = bpy.data.meshes.new(name)
obj = bpy.data.objects.new(name, mesh)
obj.draw_type = "WIRE"
bpy.context.scene.objects.link(obj)
bm = bmesh.new()
try:
yield obj, bm
except:
bpy.context.scene.objects.unlink(obj)
bpy.data.meshes.remove(mesh)
raise
else:
bm.to_mesh(mesh)
finally:
bm.free()
if __name__ == "__main__":
# Test 1
with bmesh_object("AssObject") as (obj, bm):
print("(p)AssObject")
pass
with bmesh_object("ErrorObject") as (obj, bm):
raise RuntimeError("purposeful error!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment