Import_3ds: Improved distance cue node setup
[blender-addons.git] / pose_library / asset_browser.py
blobe714f5e05d0ff80a2c76e2c9d6c836e6b449fa49
1 # SPDX-FileCopyrightText: 2021-2023 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 """Functions for finding and working with Asset Browsers."""
7 from typing import Iterable, Optional, Tuple
9 import bpy
10 from bpy_extras import asset_utils
13 if "functions" not in locals():
14 from . import functions
15 else:
16 import importlib
18 functions = importlib.reload(functions)
21 def biggest_asset_browser_area(screen: bpy.types.Screen) -> Optional[bpy.types.Area]:
22 """Return the asset browser Area that's largest on screen.
24 :param screen: context.window.screen
26 :return: the Area, or None if no Asset Browser area exists.
27 """
29 def area_sorting_key(area: bpy.types.Area) -> Tuple[bool, int]:
30 """Return area size in pixels."""
31 return area.width * area.height
33 areas = list(suitable_areas(screen))
34 if not areas:
35 return None
37 return max(areas, key=area_sorting_key)
40 def suitable_areas(screen: bpy.types.Screen) -> Iterable[bpy.types.Area]:
41 """Generator, yield Asset Browser areas."""
43 for area in screen.areas:
44 space_data = area.spaces[0]
45 if not asset_utils.SpaceAssetInfo.is_asset_browser(space_data):
46 continue
47 yield area
50 def area_from_context(context: bpy.types.Context) -> Optional[bpy.types.Area]:
51 """Return an Asset Browser suitable for the given category.
53 Prefers the current Asset Browser if available, otherwise the biggest.
54 """
56 space_data = context.space_data
57 if asset_utils.SpaceAssetInfo.is_asset_browser(space_data):
58 return context.area
60 # Try the current screen first.
61 browser_area = biggest_asset_browser_area(context.screen)
62 if browser_area:
63 return browser_area
65 for win in context.window_manager.windows:
66 if win.screen == context.screen:
67 continue
68 browser_area = biggest_asset_browser_area(win.screen)
69 if browser_area:
70 return browser_area
72 return None
75 def activate_asset(asset: bpy.types.Action, asset_browser: bpy.types.Area, *, deferred: bool) -> None:
76 """Select & focus the asset in the browser."""
78 space_data = asset_browser.spaces[0]
79 assert asset_utils.SpaceAssetInfo.is_asset_browser(space_data)
80 space_data.activate_asset_by_id(asset, deferred=deferred)
83 def active_catalog_id(asset_browser: bpy.types.Area) -> str:
84 """Return the ID of the catalog shown in the asset browser."""
85 return params(asset_browser).catalog_id
88 def params(asset_browser: bpy.types.Area) -> bpy.types.FileAssetSelectParams:
89 """Return the asset browser parameters given its Area."""
90 space_data = asset_browser.spaces[0]
91 assert asset_utils.SpaceAssetInfo.is_asset_browser(space_data)
92 return space_data.params
95 def tag_redraw(screen: bpy.types.Screen) -> None:
96 """Tag all asset browsers for redrawing."""
98 for area in suitable_areas(screen):
99 area.tag_redraw()