Implement contextmanager for load

pull/3271/head
David 2020-06-09 08:32:59 +02:00
parent ce838dfafb
commit efbabf3b0d
1 changed files with 16 additions and 6 deletions

View File

@ -14,10 +14,13 @@ if sys.version_info >= (3,0):
xrange = range xrange = range
try: import numpy try:
except ImportError: numpy = None import numpy
except ImportError:
numpy = None
import logging import logging
import ctypes import ctypes
from contextlib import contextmanager
logger = logging.getLogger("pyassimp") logger = logging.getLogger("pyassimp")
# attach default null handler to logger so it doesn't complain # attach default null handler to logger so it doesn't complain
# even if you don't attach another handler to logger # even if you don't attach another handler to logger
@ -272,6 +275,13 @@ def recur_pythonize(node, scene):
for c in node.children: for c in node.children:
recur_pythonize(c, scene) recur_pythonize(c, scene)
def release(scene):
'''
Release resources of a loaded scene.
'''
_assimp_lib.release(ctypes.pointer(scene))
@contextmanager
def load(filename, def load(filename,
file_type = None, file_type = None,
processing = postprocess.aiProcess_Triangulate): processing = postprocess.aiProcess_Triangulate):
@ -319,7 +329,10 @@ def load(filename,
raise AssimpError('Could not import file!') raise AssimpError('Could not import file!')
scene = _init(model.contents) scene = _init(model.contents)
recur_pythonize(scene.rootnode, scene) recur_pythonize(scene.rootnode, scene)
return scene try:
yield scene
finally:
release(scene)
def export(scene, def export(scene,
filename, filename,
@ -373,9 +386,6 @@ def export_blob(scene,
raise AssimpError('Could not export scene to blob!') raise AssimpError('Could not export scene to blob!')
return exportBlobPtr return exportBlobPtr
def release(scene):
_assimp_lib.release(ctypes.pointer(scene))
def _finalize_texture(tex, target): def _finalize_texture(tex, target):
setattr(target, "achformathint", tex.achFormatHint) setattr(target, "achformathint", tex.achFormatHint)
if numpy: if numpy: