Blender V4.5
asset_menu_utils.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10#include "AS_asset_library.hh"
12
13#include "DNA_screen_types.h"
14
15#include "BKE_report.hh"
16
17#include "BLT_translation.hh"
18
19#include "RNA_access.hh"
20#include "RNA_define.hh"
21#include "RNA_enum_types.hh"
22#include "RNA_prototypes.hh"
23
24#include "ED_asset_list.hh"
26
27#include "UI_interface.hh"
28
29namespace blender::ed::asset {
30
32{
33 PropertyRNA *prop;
34 prop = RNA_def_enum(&srna,
35 "asset_library_type",
38 "Asset Library Type",
39 "");
41 prop = RNA_def_string(
42 &srna, "asset_library_identifier", nullptr, 0, "Asset Library Identifier", "");
44 prop = RNA_def_string(
45 &srna, "relative_asset_identifier", nullptr, 0, "Relative Asset Identifier", "");
47}
48
51{
52 const AssetWeakReference weak_ref = asset.make_weak_reference();
53 RNA_enum_set(&ptr, "asset_library_type", weak_ref.asset_library_type);
54 RNA_string_set(&ptr, "asset_library_identifier", weak_ref.asset_library_identifier);
55 RNA_string_set(&ptr, "relative_asset_identifier", weak_ref.relative_asset_identifier);
56}
57
63 const bContext &C, const StringRefNull relative_identifier, ReportList *reports)
64{
65 AssetLibraryReference library_ref{};
66 library_ref.type = ASSET_LIBRARY_LOCAL;
67 list::storage_fetch(&library_ref, &C);
68
69 const asset_system::AssetRepresentation *matching_asset = nullptr;
71 if (asset.library_relative_identifier() == relative_identifier) {
72 matching_asset = &asset;
73 return false;
74 }
75 return true;
76 });
77
78 if (reports && !matching_asset) {
79 if (list::is_loaded(&library_ref)) {
81 reports, RPT_ERROR, "No asset found at path \"%s\"", relative_identifier.c_str());
82 }
83 else {
84 BKE_report(reports, RPT_WARNING, "Asset loading is unfinished");
85 }
86 }
87 return matching_asset;
88}
89
91 const bContext &C, const AssetWeakReference &weak_ref, ReportList *reports)
92{
96 }
97
99 list::storage_fetch(&library_ref, &C);
102 if (!all_library) {
103 BKE_report(reports, RPT_WARNING, "Asset loading is unfinished");
104 return nullptr;
105 }
106
107 const asset_system::AssetRepresentation *matching_asset = nullptr;
109 if (asset.make_weak_reference() == weak_ref) {
110 matching_asset = &asset;
111 return false;
112 }
113 return true;
114 });
115
116 if (reports && !matching_asset) {
117 if (list::is_loaded(&library_ref)) {
118 const std::string full_path = all_library->resolve_asset_weak_reference_to_full_path(
119 weak_ref);
120 BKE_reportf(reports, RPT_ERROR, "No asset found at path \"%s\"", full_path.c_str());
121 }
122 }
123 return matching_asset;
124}
125
128{
129 AssetWeakReference weak_ref{};
130 weak_ref.asset_library_type = RNA_enum_get(&ptr, "asset_library_type");
132 &ptr, "asset_library_identifier", nullptr, 0, nullptr);
134 &ptr, "relative_asset_identifier", nullptr, 0, nullptr);
135 return find_asset_from_weak_ref(C, weak_ref, reports);
136}
137
139 const StringRefNull menu_name,
140 uiLayout &layout)
141{
142 uiLayout *col = &layout.column(false);
143 uiLayoutSetContextString(col, "asset_catalog_path", item.catalog_path().c_str());
144 col->menu(menu_name, IFACE_(item.get_name()), ICON_NONE);
145}
146
147} // namespace blender::ed::asset
Main runtime representation of an asset.
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:126
#define IFACE_(msgid)
@ ASSET_LIBRARY_LOCAL
@ PROP_SKIP_SAVE
Definition RNA_types.hh:330
@ PROP_HIDDEN
Definition RNA_types.hh:324
#define C
Definition RandGen.cpp:29
void uiLayoutSetContextString(uiLayout *layout, blender::StringRef name, blender::StringRef value)
ReportList * reports
Definition WM_types.hh:1025
constexpr const char * c_str() const
std::string resolve_asset_weak_reference_to_full_path(const AssetWeakReference &asset_reference)
uint col
AssetLibraryReference all_library_reference()
void storage_fetch(const AssetLibraryReference *library_reference, const bContext *C)
asset_system::AssetLibrary * library_get_once_available(const AssetLibraryReference &library_reference)
bool is_loaded(const AssetLibraryReference *library_reference)
void iterate(const AssetLibraryReference &library_reference, AssetListIndexIterFn fn)
const asset_system::AssetRepresentation * find_asset_from_weak_ref(const bContext &C, const AssetWeakReference &weak_ref, ReportList *reports)
static const asset_system::AssetRepresentation * get_local_asset_from_relative_identifier(const bContext &C, const StringRefNull relative_identifier, ReportList *reports)
void operator_asset_reference_props_register(StructRNA &srna)
void draw_menu_for_catalog(const asset_system::AssetCatalogTreeItem &item, const StringRefNull menu_name, uiLayout &layout)
const asset_system::AssetRepresentation * operator_asset_reference_props_get_asset_from_all_library(const bContext &C, PointerRNA &ptr, ReportList *reports)
void operator_asset_reference_props_set(const asset_system::AssetRepresentation &asset, PointerRNA &ptr)
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
char * RNA_string_get_alloc(PointerRNA *ptr, const char *name, char *fixedbuf, int fixedlen, int *r_len)
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
int RNA_enum_get(PointerRNA *ptr, const char *name)
const EnumPropertyItem rna_enum_asset_library_type_items[]
Definition rna_asset.cc:21
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
const char * relative_asset_identifier
const char * asset_library_identifier
uiLayout & column(bool align)
PointerRNA * ptr
Definition wm_files.cc:4226