Last active
May 26, 2020 19:00
-
-
Save Hoikas/e742393dbb672ae31268652a44c2e2f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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