Added recursion into library blend files.

This commit is contained in:
Sybren A. Stüvel
2018-02-28 12:42:27 +01:00
parent cb5eff2dcb
commit 86af05e823
7 changed files with 108 additions and 24 deletions
+33 -5
View File
@@ -2,25 +2,53 @@ import logging
import pathlib
import typing
from blender_asset_tracer import blendfile
from blender_asset_tracer import blendfile, bpathlib
from . import result, block_walkers
log = logging.getLogger(__name__)
codes_to_skip = {
b'ID', b'WM', b'SN', # These blocks never have external assets.
# These blocks never have external assets:
b'ID', b'WM', b'SN',
# These blocks are skipped for now, until we have proof they point to
# assets otherwise missed:
b'GR', b'WO', b'BR', b'LS',
}
def deps(bfilepath: pathlib.Path) -> typing.Iterator[result.BlockUsage]:
"""Open the blend file and report its dependencies."""
def deps(bfilepath: pathlib.Path, recursive=False) -> typing.Iterator[result.BlockUsage]:
"""Open the blend file and report its dependencies.
:param bfilepath: File to open.
:param recursive: Also report dependencies inside linked blend files.
"""
log.info('Tracing %s', bfilepath)
with blendfile.BlendFile(bfilepath) as bfile:
for block in asset_holding_blocks(bfile):
yield from block_walkers.from_block(block)
# TODO: handle library blocks for recursion.
if recursive and block.code == b'LI':
yield from _recurse_deps(block)
def _recurse_deps(lib_block: blendfile.BlendFileBlock) -> typing.Iterator[result.BlockUsage]:
"""Call deps() on the file linked from the library block."""
if lib_block.code != b'LI':
raise ValueError('Expected LI block, not %r' % lib_block)
relpath = bpathlib.BlendPath(lib_block[b'name'])
abspath = lib_block.bfile.abspath(relpath)
# Convert bytes to pathlib.Path object so we have a nice interface to work with.
# This assumes the path is encoded in UTF-8.
path = pathlib.Path(abspath.decode())
if not path.exists():
log.warning('Linked blend file %s (%s) does not exist; skipping.', relpath, path)
return
yield from deps(path, recursive=True)
def asset_holding_blocks(bfile: blendfile.BlendFile) -> typing.Iterator[blendfile.BlendFileBlock]: