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
10 from bpy_extras
import asset_utils
13 if "functions" not in locals():
14 from . import functions
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.
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
))
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
):
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.
56 space_data
= context
.space_data
57 if asset_utils
.SpaceAssetInfo
.is_asset_browser(space_data
):
60 # Try the current screen first.
61 browser_area
= biggest_asset_browser_area(context
.screen
)
65 for win
in context
.window_manager
.windows
:
66 if win
.screen
== context
.screen
:
68 browser_area
= biggest_asset_browser_area(win
.screen
)
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
):