Support context manager for opening blend file
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
__version__ = "0.0.4.7"
|
__version__ = "0.0.4.8"
|
||||||
|
|
||||||
def main(argv=sys.argv):
|
def main(argv=sys.argv):
|
||||||
from .cli import main
|
from .cli import main
|
||||||
|
@@ -145,6 +145,12 @@ class BlendFile:
|
|||||||
# cache (could lazy init, incase we never use?)
|
# cache (could lazy init, incase we never use?)
|
||||||
self.block_from_offset = {block.addr_old: block for block in self.blocks if block.code != b'ENDB'}
|
self.block_from_offset = {block.addr_old: block for block in self.blocks if block.code != b'ENDB'}
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, type, value, traceback):
|
||||||
|
self.close()
|
||||||
|
|
||||||
def find_blocks_from_code(self, code):
|
def find_blocks_from_code(self, code):
|
||||||
assert(type(code) == bytes)
|
assert(type(code) == bytes)
|
||||||
if code not in self.code_index:
|
if code not in self.code_index:
|
||||||
|
@@ -396,7 +396,7 @@ class FilePath:
|
|||||||
extra_info = rootdir, os.path.basename(filepath)
|
extra_info = rootdir, os.path.basename(filepath)
|
||||||
|
|
||||||
from bam.blend import blendfile
|
from bam.blend import blendfile
|
||||||
blend = blendfile.open_blend(filepath_tmp, "rb" if readonly else "r+b")
|
with blendfile.open_blend(filepath_tmp, "rb" if readonly else "r+b") as blend:
|
||||||
|
|
||||||
for code in blend.code_index.keys():
|
for code in blend.code_index.keys():
|
||||||
# handle library blocks as special case
|
# handle library blocks as special case
|
||||||
@@ -450,8 +450,8 @@ class FilePath:
|
|||||||
# do this after, incase we mangle names above
|
# do this after, incase we mangle names above
|
||||||
for block in iter_blocks_idlib():
|
for block in iter_blocks_idlib():
|
||||||
yield from FilePath.from_block(block, basedir, extra_info, level)
|
yield from FilePath.from_block(block, basedir, extra_info, level)
|
||||||
|
del blend
|
||||||
|
|
||||||
blend.close()
|
|
||||||
|
|
||||||
# ----------------
|
# ----------------
|
||||||
# Handle Recursive
|
# Handle Recursive
|
||||||
|
Reference in New Issue
Block a user