diff --git a/CHANGELOG.md b/CHANGELOG.md index fa48fbc..0f6b75e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,12 @@ This file logs the changes that are actually interesting to users (new features, changed functionality, fixed bugs). -# Version 1.11 (in development) +# Version 1.12 (in development) + +- Removed "late imports", to help isolate Blender add-ons bundling BAT from each other. + + +# Version 1.11 (2022-02-18) - Support UDIM images. diff --git a/README.md b/README.md index be27aa8..a24ba9f 100644 --- a/README.md +++ b/README.md @@ -115,3 +115,19 @@ To understand the naming of the properties, look at Blender's `DNA_xxxx.h` files definitions. It is those names that are accessed via `blender_asset_tracer.blendfile`. The mapping to the names accessible in Blender's Python interface can be found in the `rna_yyyy.c` files. + + +## Code Guidelines + +This section documents some guidelines for the code in BAT. + +### Avoiding Late Imports + +All imports should be done at the top level of the file, and not inside +functions. The goal is to ensure that, once imported, a (sub)module of BAT can +be used without having to import more parts of BAT. + +This requirement helps to keep Blender add-ons separated, as an add-on can +import the modules of BAT it needs, then remove them from `sys.modules` and +`sys.path` so that other add-ons don't see them. This should reduce problems +with various add-ons shipping different versions of BAT. diff --git a/blender_asset_tracer/pack/__init__.py b/blender_asset_tracer/pack/__init__.py index 6b5bd7c..d9c89be 100644 --- a/blender_asset_tracer/pack/__init__.py +++ b/blender_asset_tracer/pack/__init__.py @@ -28,6 +28,7 @@ import typing from blender_asset_tracer import trace, bpathlib, blendfile from blender_asset_tracer.trace import file_sequence, result + from . import filesystem, transfer, progress log = logging.getLogger(__name__) @@ -121,9 +122,7 @@ class Packer: self._exclude_globs = set() # type: typing.Set[str] - from blender_asset_tracer.cli import common - - self._shorten = functools.partial(common.shorten, self.project) + self._shorten = functools.partial(shorten_path, self.project) if noop: log.warning("Running in no-op mode, only showing what will be done.") @@ -618,3 +617,11 @@ class Packer: ) self._file_transferer.queue_move(infopath, self._target_path / infoname) + + +def shorten_path(cwd: pathlib.Path, somepath: pathlib.Path) -> pathlib.Path: + """Return 'somepath' relative to CWD if possible.""" + try: + return somepath.relative_to(cwd) + except ValueError: + return somepath