Blender V5.0
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"
29
30namespace blender::ed::asset {
31
33{
34 PropertyRNA *prop;
35 prop = RNA_def_enum(&srna,
36 "asset_library_type",
39 "Asset Library Type",
40 "");
42 prop = RNA_def_string(
43 &srna, "asset_library_identifier", nullptr, 0, "Asset Library Identifier", "");
45 prop = RNA_def_string(
46 &srna, "relative_asset_identifier", nullptr, 0, "Relative Asset Identifier", "");
48}
49
52{
53 const AssetWeakReference weak_ref = asset.make_weak_reference();
54 RNA_enum_set(&ptr, "asset_library_type", weak_ref.asset_library_type);
55 RNA_string_set(&ptr, "asset_library_identifier", weak_ref.asset_library_identifier);
56 RNA_string_set(&ptr, "relative_asset_identifier", weak_ref.relative_asset_identifier);
57}
58
60{
61 return RNA_struct_property_is_set(&ptr, "asset_library_type") &&
62 RNA_struct_property_is_set(&ptr, "asset_library_identifier") &&
63 RNA_struct_property_is_set(&ptr, "relative_asset_identifier");
64}
65
71 const bContext &C, const StringRefNull relative_identifier, ReportList *reports)
72{
73 AssetLibraryReference library_ref{};
74 library_ref.type = ASSET_LIBRARY_LOCAL;
75 list::storage_fetch(&library_ref, &C);
76
77 const asset_system::AssetRepresentation *matching_asset = nullptr;
79 if (asset.library_relative_identifier() == relative_identifier) {
80 matching_asset = &asset;
81 return false;
82 }
83 return true;
84 });
85
86 if (reports && !matching_asset) {
87 if (list::is_loaded(&library_ref)) {
89 reports, RPT_ERROR, "No asset found at path \"%s\"", relative_identifier.c_str());
90 }
91 else {
92 BKE_report(reports, RPT_WARNING, "Asset loading is unfinished");
93 }
94 }
95 return matching_asset;
96}
97
99 const bContext &C, const AssetWeakReference &weak_ref, ReportList *reports)
100{
101 if (weak_ref.asset_library_type == ASSET_LIBRARY_LOCAL) {
103 C, weak_ref.relative_asset_identifier, reports);
104 }
105
107 list::storage_fetch(&library_ref, &C);
110 if (!all_library) {
111 BKE_report(reports, RPT_WARNING, "Asset loading is unfinished");
112 return nullptr;
113 }
114
115 const asset_system::AssetRepresentation *matching_asset = nullptr;
117 if (asset.make_weak_reference() == weak_ref) {
118 matching_asset = &asset;
119 return false;
120 }
121 return true;
122 });
123
124 if (reports && !matching_asset) {
125 if (list::is_loaded(&library_ref)) {
126 const std::string full_path = all_library->resolve_asset_weak_reference_to_full_path(
127 weak_ref);
128 BKE_reportf(reports, RPT_ERROR, "No asset found at path \"%s\"", full_path.c_str());
129 }
130 }
131 return matching_asset;
132}
133
135 const bContext &C, PointerRNA &ptr, ReportList *reports)
136{
137 AssetWeakReference weak_ref{};
138 weak_ref.asset_library_type = RNA_enum_get(&ptr, "asset_library_type");
140 &ptr, "asset_library_identifier", nullptr, 0, nullptr);
142 &ptr, "relative_asset_identifier", nullptr, 0, nullptr);
143 return find_asset_from_weak_ref(C, weak_ref, reports);
144}
145
147 const StringRefNull menu_name,
148 uiLayout &layout)
149{
150 uiLayout *col = &layout.column(false);
151 col->context_string_set("asset_catalog_path", item.catalog_path().c_str());
152 col->menu(menu_name, IFACE_(item.get_name()), ICON_NONE);
153}
154
156 const StringRefNull operator_id,
157 const StringRefNull menu_name,
158 uiLayout &layout)
159{
160 uiLayout *col = &layout.column(false);
161 col->context_string_set("asset_catalog_path", item.catalog_path().c_str());
162 col->context_string_set("operator_id", operator_id);
163 col->menu(menu_name, IFACE_(item.get_name()), ICON_NONE);
164}
165
166} // namespace blender::ed::asset
Main runtime representation of an asset.
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
@ RPT_ERROR
Definition BKE_report.hh:39
@ RPT_WARNING
Definition BKE_report.hh:38
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:153
#define IFACE_(msgid)
@ ASSET_LIBRARY_LOCAL
@ PROP_SKIP_SAVE
Definition RNA_types.hh:344
@ PROP_HIDDEN
Definition RNA_types.hh:338
#define C
Definition RandGen.cpp:29
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)
void iterate(const AssetLibraryReference &library_reference, AssetListIterFn fn)
asset_system::AssetLibrary * library_get_once_available(const AssetLibraryReference &library_reference)
bool is_loaded(const AssetLibraryReference *library_reference)
void draw_node_menu_for_catalog(const asset_system::AssetCatalogTreeItem &item, const StringRefNull operator_id, const StringRefNull menu_name, uiLayout &layout)
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)
bool operator_asset_reference_props_is_set(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)
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
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:4238