Blender  V2.93
asset_ops.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include "BKE_context.h"
22 #include "BKE_report.h"
23 
24 #include "BLI_vector.hh"
25 
26 #include "ED_asset.h"
27 
28 #include "RNA_access.h"
29 
30 #include "WM_api.h"
31 #include "WM_types.h"
32 
33 /* -------------------------------------------------------------------- */
34 
36 
37 static bool asset_operation_poll(bContext * /*C*/)
38 {
39  return U.experimental.use_asset_browser;
40 }
41 
47 {
48  PointerRNAVec ids;
49 
51  if (idptr.data) {
52  /* Single ID. */
53  ids.append(idptr);
54  }
55  else {
56  ListBase list;
57  CTX_data_selected_ids(C, &list);
58  LISTBASE_FOREACH (CollectionPointerLink *, link, &list) {
59  ids.append(link->ptr);
60  }
61  BLI_freelistN(&list);
62  }
63 
64  return ids;
65 }
66 
67 /* -------------------------------------------------------------------- */
68 
70  public:
71  void operator()(const bContext &C, PointerRNAVec &ids);
72 
73  void reportResults(ReportList &reports) const;
74  bool wasSuccessful() const;
75 
76  private:
77  struct Stats {
78  int tot_created = 0;
79  int tot_already_asset = 0;
80  ID *last_id = nullptr;
81  };
82 
83  Stats stats;
84 };
85 
87 {
88  for (PointerRNA &ptr : ids) {
90 
91  ID *id = static_cast<ID *>(ptr.data);
92  if (id->asset_data) {
93  stats.tot_already_asset++;
94  continue;
95  }
96 
97  if (ED_asset_mark_id(&C, id)) {
98  stats.last_id = id;
99  stats.tot_created++;
100  }
101  }
102 }
103 
105 {
106  return stats.tot_created > 0;
107 }
108 
110 {
111  /* User feedback on failure. */
112  if (!wasSuccessful()) {
113  if ((stats.tot_already_asset > 0)) {
114  BKE_report(&reports,
115  RPT_ERROR,
116  "Selected data-blocks are already assets (or do not support use as assets)");
117  }
118  else {
119  BKE_report(&reports,
120  RPT_ERROR,
121  "No data-blocks to create assets for found (or do not support use as assets)");
122  }
123  }
124  /* User feedback on success. */
125  else if (stats.tot_created == 1) {
126  /* If only one data-block: Give more useful message by printing asset name. */
127  BKE_reportf(&reports, RPT_INFO, "Data-block '%s' is now an asset", stats.last_id->name + 2);
128  }
129  else {
130  BKE_reportf(&reports, RPT_INFO, "%i data-blocks are now assets", stats.tot_created);
131  }
132 }
133 
135 {
137 
138  AssetMarkHelper mark_helper;
139  mark_helper(*C, ids);
140  mark_helper.reportResults(*op->reports);
141 
142  if (!mark_helper.wasSuccessful()) {
143  return OPERATOR_CANCELLED;
144  }
145 
148 
149  return OPERATOR_FINISHED;
150 }
151 
153 {
154  ot->name = "Mark Asset";
155  ot->description =
156  "Enable easier reuse of selected data-blocks through the Asset Browser, with the help of "
157  "customizable metadata (like previews, descriptions and tags)";
158  ot->idname = "ASSET_OT_mark";
159 
162 
164 }
165 
166 /* -------------------------------------------------------------------- */
167 
169  public:
170  void operator()(PointerRNAVec &ids);
171 
172  void reportResults(ReportList &reports) const;
173  bool wasSuccessful() const;
174 
175  private:
176  struct Stats {
177  int tot_cleared = 0;
178  ID *last_id = nullptr;
179  };
180 
181  Stats stats;
182 };
183 
185 {
186  for (PointerRNA &ptr : ids) {
188 
189  ID *id = static_cast<ID *>(ptr.data);
190  if (!id->asset_data) {
191  continue;
192  }
193 
194  if (ED_asset_clear_id(id)) {
195  stats.tot_cleared++;
196  stats.last_id = id;
197  }
198  }
199 }
200 
202 {
203  if (!wasSuccessful()) {
204  BKE_report(&reports, RPT_ERROR, "No asset data-blocks selected/focused");
205  }
206  else if (stats.tot_cleared == 1) {
207  /* If only one data-block: Give more useful message by printing asset name. */
208  BKE_reportf(
209  &reports, RPT_INFO, "Data-block '%s' is no asset anymore", stats.last_id->name + 2);
210  }
211  else {
212  BKE_reportf(&reports, RPT_INFO, "%i data-blocks are no assets anymore", stats.tot_cleared);
213  }
214 }
215 
217 {
218  return stats.tot_cleared > 0;
219 }
220 
222 {
224 
225  AssetClearHelper clear_helper;
226  clear_helper(ids);
227  clear_helper.reportResults(*op->reports);
228 
229  if (!clear_helper.wasSuccessful()) {
230  return OPERATOR_CANCELLED;
231  }
232 
235 
236  return OPERATOR_FINISHED;
237 }
238 
240 {
241  ot->name = "Clear Asset";
242  ot->description =
243  "Delete all asset metadata and turn the selected asset data-blocks back into normal "
244  "data-blocks";
245  ot->idname = "ASSET_OT_clear";
246 
249 
251 }
252 
253 /* -------------------------------------------------------------------- */
254 
256 {
259 }
PointerRNA CTX_data_pointer_get_type(const bContext *C, const char *member, StructRNA *type)
Definition: context.c:456
int CTX_data_selected_ids(const bContext *C, ListBase *list)
Definition: context.c:1219
void BKE_report(ReportList *reports, ReportType type, const char *message)
Definition: report.c:104
void BKE_reportf(ReportList *reports, ReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:547
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
StructRNA RNA_ID
#define C
Definition: RandGen.cpp:39
#define NC_ID
Definition: WM_types.h:296
@ OPTYPE_UNDO
Definition: WM_types.h:155
@ OPTYPE_REGISTER
Definition: WM_types.h:153
#define NA_ADDED
Definition: WM_types.h:464
#define NA_EDITED
Definition: WM_types.h:462
#define NC_ASSET
Definition: WM_types.h:305
#define NA_REMOVED
Definition: WM_types.h:465
bool ED_asset_clear_id(ID *id)
Definition: asset_edit.cc:51
bool ED_asset_mark_id(const bContext *C, ID *id)
Definition: asset_edit.cc:33
static void ASSET_OT_mark(wmOperatorType *ot)
Definition: asset_ops.cc:152
static int asset_clear_exec(bContext *C, wmOperator *op)
Definition: asset_ops.cc:221
static PointerRNAVec asset_operation_get_ids_from_context(const bContext *C)
Definition: asset_ops.cc:46
static int asset_mark_exec(bContext *C, wmOperator *op)
Definition: asset_ops.cc:134
static void ASSET_OT_clear(wmOperatorType *ot)
Definition: asset_ops.cc:239
static bool asset_operation_poll(bContext *)
Definition: asset_ops.cc:37
void ED_operatortypes_asset(void)
Definition: asset_ops.cc:255
unsigned int U
Definition: btGjkEpa3.h:78
bool wasSuccessful() const
Definition: asset_ops.cc:216
void operator()(PointerRNAVec &ids)
Definition: asset_ops.cc:184
void reportResults(ReportList &reports) const
Definition: asset_ops.cc:201
bool wasSuccessful() const
Definition: asset_ops.cc:104
void operator()(const bContext &C, PointerRNAVec &ids)
Definition: asset_ops.cc:86
void reportResults(ReportList &reports) const
Definition: asset_ops.cc:109
Stats()
Definition: util_stats.h:29
void append(const T &value)
Definition: BLI_vector.hh:438
bool RNA_struct_is_ID(const StructRNA *type)
Definition: rna_access.c:797
Definition: DNA_ID.h:273
struct AssetMetaData * asset_data
Definition: DNA_ID.h:280
struct StructRNA * type
Definition: RNA_types.h:51
void * data
Definition: RNA_types.h:52
const char * name
Definition: WM_types.h:721
const char * idname
Definition: WM_types.h:723
bool(* poll)(struct bContext *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:776
const char * description
Definition: WM_types.h:726
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:736
struct ReportList * reports
void WM_main_add_notifier(unsigned int type, void *reference)
PointerRNA * ptr
Definition: wm_files.c:3157
wmOperatorType * ot
Definition: wm_files.c:3156
void WM_operatortype_append(void(*opfunc)(wmOperatorType *))