Merge branch 'blender-v4.0-release'
[blender-addons.git] / pose_library / functions.py
blob1e5971d952ad2dba36dfeab37ebc2bd0d06ac9ec
1 # SPDX-FileCopyrightText: 2021-2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 """
6 Pose Library - functions.
7 """
9 from pathlib import Path
10 from typing import Any, List, Iterable
12 Datablock = Any
14 import bpy
17 def load_assets_from(filepath: Path) -> List[Datablock]:
18 if not has_assets(filepath):
19 # Avoid loading any datablocks when there are none marked as asset.
20 return []
22 # Append everything from the file.
23 with bpy.data.libraries.load(str(filepath)) as (
24 data_from,
25 data_to,
27 for attr in dir(data_to):
28 setattr(data_to, attr, getattr(data_from, attr))
30 # Iterate over the appended datablocks to find assets.
31 def loaded_datablocks() -> Iterable[Datablock]:
32 for attr in dir(data_to):
33 datablocks = getattr(data_to, attr)
34 for datablock in datablocks:
35 yield datablock
37 loaded_assets = []
38 for datablock in loaded_datablocks():
39 if not getattr(datablock, "asset_data", None):
40 continue
42 # Fake User is lost when appending from another file.
43 datablock.use_fake_user = True
44 loaded_assets.append(datablock)
45 return loaded_assets
48 def has_assets(filepath: Path) -> bool:
49 with bpy.data.libraries.load(str(filepath), assets_only=True) as (
50 data_from,
53 for attr in dir(data_from):
54 data_names = getattr(data_from, attr)
55 if data_names:
56 return True
57 return False