Blender V5.3
userpref_asset_libraries_list.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10#include "BKE_global.hh"
11#include "BKE_preferences.h"
12
13#include "BLI_listbase.hh"
14#include "BLT_translation.hh"
15
16#include "DNA_screen_types.h"
17
19#include "UI_tree_view.hh"
20
21#include "RNA_access.hh"
22#include "RNA_enum_types.hh"
23#include "RNA_prototypes.hh"
24
26#include "ED_userpref.hh"
27
28#include "userpref_intern.hh"
29
30namespace blender {
31
32constexpr int FIXED_ITEMS_COUNT = 2;
33
35{
37
40
42
43 Vector<bUserAssetLibrary *> extension_libraries;
44 for (bUserAssetLibrary &user_library : U.asset_libraries) {
45 if (!USER_EXPERIMENTAL_TEST(&U, use_remote_asset_libraries) &&
47 {
48 continue;
49 }
51 extension_libraries.append(&user_library);
52 continue;
53 }
55 }
56
57 /* Extension libraries are listed under their repository. */
58 for (bUserExtensionRepo &repo : U.extension_repos) {
59 bool has_repo_row = false;
60 for (bUserAssetLibrary *user_library : extension_libraries) {
61 if (BKE_preferences_extension_asset_library_repo_get(&U, user_library) != &repo) {
62 continue;
63 }
64 if (!has_repo_row) {
66 has_repo_row = true;
67 }
68 result.append(AnyAssetLibraryDefinition{ASSET_LIBRARY_CUSTOM, user_library, &repo});
69 }
70 }
71
72 return result;
73}
74
76{
77 /* Instead of constructing the vector (potentially allocating memory), just count the list items
78 * and use the fixed item count. */
79 if (USER_EXPERIMENTAL_TEST(&U, use_remote_asset_libraries)) {
80 int count = U.asset_libraries.count() + FIXED_ITEMS_COUNT;
81 /* A row for each repository with extension libraries. */
82 for (bUserExtensionRepo &repo : U.extension_repos) {
83 for (bUserAssetLibrary &user_library : U.asset_libraries) {
84 if (BKE_preferences_extension_asset_library_repo_get(&U, &user_library) == &repo) {
85 count++;
86 break;
87 }
88 }
89 }
90 BLI_assert(count == userpref_ui_asset_libraries().size());
91 return count;
92 }
93
94 /* In case remote libraries are disabled, just retrieve the count from the available items. */
95 return userpref_ui_asset_libraries().size();
96}
97
99 const bUserAssetLibrary &user_library)
100{
101 int i = 0;
102
104 for (const AnyAssetLibraryDefinition &library : libraries) {
105 if (library.user_library && library.user_library == &user_library) {
106 return i;
107 }
108 i++;
109 }
110
111 return std::nullopt;
112}
113
115{
117 if (project) {
118 row.label(project->get_name(), ICON_PROJECT);
119 }
120}
121
123{
125 if (!libraries.index_range().contains(U.active_asset_library)) {
126 return nullptr;
127 }
128 return libraries[U.active_asset_library].user_library;
129}
130
132{
134 asset_library);
135 if (index) {
136 U.active_asset_library = *index;
137 }
138}
139
142{
143 return library.extension_repo &&
144 (library.extension_repo->flag & USER_EXTENSION_REPO_FLAG_DISABLED);
145}
146
148
150
151 void build_row(ui::Layout &row) override
152 {
153 const bool is_remote_library = library.user_library &&
155 const bool project_library = library.user_library &&
157
158 /* Draw grayed out, the "enabled" setting stays editable, it just has no effect. */
160 row.active_set(false);
161 }
162
163 if (library.user_library) {
164 int icon = is_remote_library ? ICON_INTERNET : ICON_DISK_DRIVE;
167 {
168 icon = ICON_EXTENSION;
169 }
170 row.label(label_, icon);
171
172 /* Disable row if asset library is project defined. */
173 row.enabled_set(!project_library);
174 }
175 else {
176 row.label(label_, ICON_NONE);
177
178 ui::Layout &sub = row.row(true);
179 /* Draw text grayed out. */
180 sub.active_set(false);
182 sub.label(IFACE_("Built-In"), ICON_NONE);
183 }
184
186 is_remote_library && !library.user_library->remote_url[0])
187 {
188 row.label("", ICON_STATUS_ERROR);
189 }
190
191 if (library.user_library) {
193 nullptr, RNA_UserAssetLibrary, library.user_library);
194 row.prop(&ptr,
195 "enabled",
197 "",
198 (library.user_library->flag & ASSET_LIBRARY_DISABLED) ? ICON_CHECKBOX_DEHLT :
199 ICON_CHECKBOX_HLT);
200 }
201 }
202
203 void on_activate(bContext & /*C*/) override
204 {
205 U.active_asset_library = index_in_list;
206 }
207 std::optional<bool> should_be_active() const override
208 {
209 return U.active_asset_library == index_in_list;
210 }
211};
212
215
216 using AssetLibraryListItem::AssetLibraryListItem;
217
218 void build_row(ui::Layout &row) override
219 {
220 row.label(label_, ICON_NONE);
221
223 row.active_set(false);
224 ui::Layout &sub = row.row(true);
225 /* Draw text grayed out. */
227 sub.label(IFACE_("Disabled"), ICON_NONE);
228 }
229 }
230
231 bool matches_single(const ui::AbstractTreeViewItem &other) const override
232 {
233 /* Match by the repository, the name isn't stable across renaming. */
234 const auto *other_item = dynamic_cast<const AssetLibraryRepoItem *>(&other);
235 return other_item && (library.extension_repo == other_item->library.extension_repo);
236 }
237
238 std::optional<bool> should_be_collapsed() const override
239 {
240 const bUserExtensionRepo &repo = *library.extension_repo;
242 }
243
244 bool set_collapsed(const bool collapsed) override
245 {
246 if (!AbstractTreeViewItem::set_collapsed(collapsed)) {
247 return false;
248 }
249 /* NOTE: also set for changes made by the tree view, user changes set it again through RNA. */
250 SET_FLAG_FROM_TEST(library.extension_repo->flag,
251 collapsed,
253 return true;
254 }
255
256 void on_collapse_change(bContext &C, const bool is_collapsed) override
257 {
258 /* Set through RNA so the preferences are tagged as changed. */
260 nullptr, RNA_UserExtensionRepo, library.extension_repo);
261 PropertyRNA *prop = RNA_struct_find_property(&repo_ptr, "show_expanded");
262 RNA_property_boolean_set(&repo_ptr, prop, !is_collapsed);
263 RNA_property_update(&C, &repo_ptr, prop);
264 }
265};
266
268{
270
271 ui::Layout &layout = *panel->layout;
272
273 ui::Layout &row = layout.row(false);
274
276 *C, row, libraries, "Asset Libraries Preferences");
277
278 ui::Layout &col = row.column(true);
279 if (USER_EXPERIMENTAL_TEST(&U, use_remote_asset_libraries)) {
280 col.op_menu_enum(C, "preferences.asset_library_add", "type", "", ICON_ADD);
281 }
282 else {
283 PointerRNA props = col.op("preferences.asset_library_add", "", ICON_ADD);
284 RNA_enum_set(&props, "type", ASSET_LIBRARY_LOCAL);
285 }
286
287 ui::Layout &sub = col.row(true);
288 const bool active_idx_in_range = U.active_asset_library >= 0 &&
289 U.active_asset_library < libraries.size();
290 const bUserAssetLibrary *active_library = active_idx_in_range ?
291 libraries[U.active_asset_library].user_library :
292 nullptr;
293 const bool is_project_library = active_library &&
296 /* Only user libraries can be removed here, extension libraries are removed by uninstalling
297 * the extension & project libraries from the project settings. */
298 sub.enabled_set(active_library && (BKE_preferences_asset_library_owner_get(active_library) ==
300 PointerRNA props = sub.op("preferences.asset_library_remove", "", ICON_REMOVE);
301 if (active_library) {
302 RNA_int_set(&props, "index", BKE_preferences_asset_library_get_index(&U, active_library));
303 }
304
305 if (!active_idx_in_range) {
306 return;
307 }
308
309 if (is_project_library) {
310 ui::Layout &label_row = layout.row(false);
311 label_row.label(IFACE_("Edit Project asset libraries in Project Settings."), ICON_NONE);
312 ui::Layout &operator_row = label_row.row(false);
314 PointerRNA op_ptr = operator_row.op("SCREEN_OT_project_settings_show",
315 IFACE_("Open Project Settings"),
316 ICON_PROJECT,
319 RNA_string_set(&op_ptr, "section", "Asset Libraries");
320 }
321 else {
322 layout.separator();
323 }
324 if (libraries[U.active_asset_library].is_extension_repo()) {
325 return;
326 }
327 ui::Layout &settings_row = layout.column(false);
328 settings_row.enabled_set(!is_project_library);
329 draw_active_library_settings(C, settings_row, libraries[U.active_asset_library]);
330}
331
332} // namespace blender
#define G_MAIN
#define IFACE_(msgid)
#define USER_EXPERIMENTAL_TEST(userdef, member)
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define SET_FLAG_FROM_TEST(value, test, flag)
int64_t size() const
void append(const T &value)
IndexRange index_range() const
constexpr bool contains(int64_t value) const
Abstract base class for defining a customizable tree-view item.
BMesh Iterators.
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void draw_library_list(const bContext &C, ui::Layout &layout, Vector< AnyAssetLibraryDefinition > &libraries, StringRef view_description)
int BKE_preferences_asset_library_get_index(const struct UserDef *userdef, const struct bUserAssetLibrary *library) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
void userpref_asset_libraries_panel_draw(const bContext *C, Panel *panel)
@ ASSET_LIBRARY_PROJECT_DEFINED
@ ASSET_LIBRARY_DISABLED
@ ASSET_LIBRARY_USE_REMOTE_URL
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
bke::BlenderProject * BKE_blender_project_get(const Main *bmain)
void RNA_int_set(PointerRNA *ptr, const char *name, int value)
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
@ ASSET_LIBRARY_CUSTOM
@ ASSET_LIBRARY_ESSENTIALS
@ ASSET_LIBRARY_LOCAL
struct bUserExtensionRepo * BKE_preferences_extension_asset_library_repo_get(const struct UserDef *userdef, const struct bUserAssetLibrary *library) ATTR_NONNULL()
bUserAssetLibraryOwner BKE_preferences_asset_library_owner_get(const struct bUserAssetLibrary *library) ATTR_NONNULL()
static bool asset_library_repo_is_disabled(const AnyAssetLibraryDefinition &library)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
@ USER_EXTENSION_REPO_FLAG_DISABLED
@ USER_EXTENSION_REPO_FLAG_ASSET_LIBRARIES_COLLAPSED
void draw_active_library_settings(const bContext *C, ui::Layout &layout, const AnyAssetLibraryDefinition &library)
constexpr int FIXED_ITEMS_COUNT
void RNA_property_boolean_set(PointerRNA *ptr, PropertyRNA *prop, bool value)
static Vector< AnyAssetLibraryDefinition > userpref_ui_asset_libraries()
void ED_userpref_asset_library_active_set(const struct bUserAssetLibrary &asset_library)
static std::optional< int > userpref_ui_asset_libraries_index_from_user_library(const bUserAssetLibrary &user_library)
struct bUserAssetLibrary * ED_userpref_asset_library_active_get()
AssetLibraryListItemCommon(const AnyAssetLibraryDefinition &library, const int index_in_list)
std::optional< bool > should_be_active() const override
AssetLibraryListItemCommon(const AnyAssetLibraryDefinition &library, const int index_in_list)
void build_row(ui::Layout &row) override
void on_collapse_change(bContext &C, const bool is_collapsed) override
void build_row(ui::Layout &row) override
std::optional< bool > should_be_collapsed() const override
bool set_collapsed(const bool collapsed) override
bool matches_single(const ui::AbstractTreeViewItem &other) const override
ui::Layout * layout
eUserExtensionRepo_Flag flag
void enabled_set(bool enabled)
void alignment_set(LayoutAlign alignment)
PointerRNA op_menu_enum(const bContext *C, wmOperatorType *ot, StringRefNull propname, std::optional< StringRefNull > name, int icon)
void label(StringRef name, int icon)
PointerRNA op(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
void active_set(bool active)
Layout & column(bool align)
void separator(float factor=1.0f, LayoutSeparatorType type=LayoutSeparatorType::Auto)
Layout & row(bool align)