Blender V5.3
ed_util_ops.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
10
11#include <cstring>
12
13#include "DNA_space_types.h"
15
16#include "BLI_fileops.hh"
17#include "BLI_utildefines.hh"
18#include "BLI_vector.hh"
19
20#include "BLT_translation.hh"
21
22#include "BKE_context.hh"
23#include "BKE_global.hh"
24#include "BKE_lib_id.hh"
25#include "BKE_lib_override.hh"
26#include "BKE_library.hh"
27#include "BKE_preview_image.hh"
28#include "BKE_report.hh"
29
30#include "ED_asset.hh"
31#include "ED_render.hh"
32#include "ED_undo.hh"
33#include "ED_util.hh"
34
35#include "RNA_access.hh"
36#include "RNA_prototypes.hh"
37
38#include "UI_interface.hh"
39
40#include "WM_api.hh"
41#include "WM_types.hh"
42
43namespace blender {
44
45/* -------------------------------------------------------------------- */
48
50{
52 PointerRNA idptr = CTX_data_pointer_get_type(C, "id", RNA_ID);
53 if (idptr) {
54 ids.append(idptr);
55 }
56 return ids;
57}
58
60{
62
63 /* "selected_ids" context member. */
65 if (!ids.is_empty()) {
66 return ids;
67 }
68
69 /* "id" context member. */
71}
72
74
75/* -------------------------------------------------------------------- */
78
79static bool lib_id_preview_editing_poll_ex(const ID *id, const char **r_disabled_hint)
80{
81 if (!id) {
82 return false;
83 }
84 if (!ID_IS_EDITABLE(id)) {
85 if (r_disabled_hint) {
86 *r_disabled_hint = RPT_("Cannot edit external library data");
87 }
88 return false;
89 }
90 if (ID_IS_OVERRIDE_LIBRARY(id)) {
91 if (r_disabled_hint) {
92 *r_disabled_hint = RPT_("Cannot edit previews of overridden library data");
93 }
94 return false;
95 }
96 if (!BKE_previewimg_id_get_p(id)) {
97 if (r_disabled_hint) {
98 *r_disabled_hint = RPT_("Data-block does not support previews");
99 }
100 return false;
101 }
102
103 return true;
104}
105
107{
108 const PointerRNA idptr = CTX_data_pointer_get(C, "id");
109 BLI_assert(!idptr || RNA_struct_is_ID(idptr.type));
110
111 const ID *id = static_cast<ID *>(idptr.data);
112 const char *disabled_hint = nullptr;
113 if (!lib_id_preview_editing_poll_ex(id, &disabled_hint)) {
114 CTX_wm_operator_poll_msg_set(C, disabled_hint);
115 return false;
116 }
117
118 return true;
119}
120
122{
123 /* #invoke() gets the ID from context and saves it in the custom data. */
124 if (op->customdata) {
125 return static_cast<ID *>(op->customdata);
126 }
127
128 PointerRNA idptr = CTX_data_pointer_get(C, "id");
129 return static_cast<ID *>(idptr.data);
130}
131
133{
134 char filepath[FILE_MAX];
135
136 RNA_string_get(op->ptr, "filepath", filepath);
137
138 if (!BLI_is_file(filepath)) {
139 BKE_reportf(op->reports, RPT_ERROR, "File not found '%s'", filepath);
140 return OPERATOR_CANCELLED;
141 }
142
144 if (!id) {
146 op->reports, RPT_ERROR, "Failed to set preview: no ID in context (incorrect context?)");
147 return OPERATOR_CANCELLED;
148 }
149
150 BKE_previewimg_id_custom_set(id, filepath);
151
153
154 return OPERATOR_FINISHED;
155}
156
164 wmOperator *op,
165 const wmEvent *event)
166{
168 return WM_operator_filesel(C, op, event);
169}
170
172{
173 ot->name = "Load Custom Preview";
174 ot->description = "Choose an image to help identify the data-block visually";
175 ot->idname = "ED_OT_lib_id_load_custom_preview";
176
177 /* API callbacks. */
181
182 /* flags */
184
192}
193
198static void lib_id_batch_edit_previews(bContext *C, FunctionRef<void(ID *)> foreach_id)
199{
201 for (PointerRNA &idptr : id_pointers) {
202 ID *id = static_cast<ID *>(idptr.data);
203
204 if (lib_id_preview_editing_poll_ex(id, nullptr)) {
205 foreach_id(id);
206 }
207 }
208}
209
220 bContext *C,
221 FunctionRef<bool(const ID *, const char **r_disabled_hint)> additional_condition = nullptr)
222{
224 if (id_pointers.is_empty()) {
225 CTX_wm_operator_poll_msg_set(C, "No data-block selected or active");
226 return false;
227 }
228
229 const char *disabled_hint = nullptr;
230
231 for (const PointerRNA &idptr : id_pointers) {
232 const ID *id = static_cast<const ID *>(idptr.data);
233
234 const char *iter_disabled_hint = nullptr;
235 if (lib_id_preview_editing_poll_ex(id, &iter_disabled_hint) &&
236 (!additional_condition || additional_condition(id, &iter_disabled_hint)))
237 {
238 /* Operator can run if there's at least one ID supporting previews. */
239 return true;
240 }
241
242 if (iter_disabled_hint && !disabled_hint) {
243 disabled_hint = iter_disabled_hint;
244 }
245 }
246
247 /* Will only hold the first disabled hint set. That often gives some more specific information,
248 * so it's more useful than a generic message. */
249 if (disabled_hint) {
250 CTX_wm_operator_poll_msg_set(C, disabled_hint);
251 }
252 else {
253 CTX_wm_operator_poll_msg_set(C, "None of the selected data-blocks supports previews");
254 }
255 return false;
256}
257
259{
260 /* Requires GPU for viewport off-screen drawing. */
261 if (G.background) {
262 return false;
263 }
264 return lib_id_batch_editing_preview_poll(C, [](const ID *id, const char **r_disabled_hint) {
265 return ED_preview_id_render_is_supported(id, r_disabled_hint);
266 });
267}
268
270{
271 using namespace blender::ed;
272
274
276 if (ED_preview_id_render_is_supported(id, nullptr)) {
277 PreviewImage *preview = BKE_previewimg_id_get(id);
278
279 if (preview) {
280 BKE_previewimg_clear(preview);
281 }
282
283 ui::icon_render_id(C, nullptr, id, ICON_SIZE_PREVIEW, true);
284 }
285 });
286
289
290 return OPERATOR_FINISHED;
291}
292
294{
295 ot->name = "Generate Preview";
296 ot->description = "Create an automatic preview for the selected data-block";
297 ot->idname = "ED_OT_lib_id_generate_preview";
298
299 /* API callbacks. */
302
303 /* flags */
305}
306
308{
309 if (G.background) {
310 /* Unsupported as it requires GPU for off-screen viewport drawing. */
311 return false;
312 }
313 /* This already checks if the IDs in context (e.g. selected in the Asset browser) can generate
314 * previews... */
316 return false;
317 }
318
319 /* ... but we also need to check this for the active object (since this is what is being
320 * rendered). */
321 Object *object_to_render = CTX_data_active_object(C);
322 if (object_to_render == nullptr) {
323 return false;
324 }
325 const char *disabled_hint = nullptr;
326 if (!ED_preview_id_render_is_supported(&object_to_render->id, &disabled_hint)) {
327 CTX_wm_operator_poll_msg_set(C, disabled_hint);
328 return false;
329 }
330
331 return true;
332}
333
335{
336 using namespace blender::ed;
337
339
340 Object *object_to_render = CTX_data_active_object(C);
341
344
345 PreviewImage *preview_image = BKE_previewimg_id_ensure(id);
347 C, nullptr, &object_to_render->id, ICON_SIZE_PREVIEW, true, preview_image);
348 });
349
352
353 return OPERATOR_FINISHED;
354}
355
357{
358 ot->name = "Generate Preview from Object";
359 ot->description = "Create a preview for this asset by rendering the active object";
360 ot->idname = "ED_OT_lib_id_generate_preview_from_object";
361
362 /* API callbacks. */
365
366 /* flags */
368}
369
371{
373 return false;
374 }
375
376 bool has_any_removable = false;
378 if (BKE_previewimg_id_get(id)) {
379 has_any_removable = true;
380 }
381 });
382
383 if (!has_any_removable) {
384 CTX_wm_operator_poll_msg_set(C, "No preview available to remove");
385 return false;
386 }
387
388 return true;
389}
390
399
401{
402 /* identifiers */
403 ot->name = "Remove Preview";
404 ot->description = "Remove the preview of this data-block";
405 ot->idname = "ED_OT_lib_id_remove_preview";
406
407 /* API callbacks. */
410
411 /* flags */
413}
414
416
417/* -------------------------------------------------------------------- */
420
422{
423 PropertyPointerRNA pprop;
424 PointerRNA idptr = {};
425
427
428 if (pprop.prop) {
429 idptr = RNA_property_pointer_get(&pprop.ptr, pprop.prop);
430 }
431
432 if ((pprop.prop == nullptr) || !idptr || !RNA_struct_is_ID(idptr.type)) {
434 op->reports, RPT_ERROR, "Incorrect context for running data-block fake user toggling");
435 return OPERATOR_CANCELLED;
436 }
437
438 ID *id = static_cast<ID *>(idptr.data);
439
441 ELEM(GS(id->name), ID_GR, ID_SCE, ID_SCR, ID_TXT, ID_OB, ID_WS))
442 {
443 BKE_report(op->reports, RPT_ERROR, "Data-block type does not support fake user");
444 return OPERATOR_CANCELLED;
445 }
446
447 if (ID_FAKE_USERS(id)) {
449 }
450 else {
452 }
453
454 return OPERATOR_FINISHED;
455}
456
458{
459 /* identifiers */
460 ot->name = "Toggle Fake User";
461 ot->description = "Save this data-block even if it has no users";
462 ot->idname = "ED_OT_lib_id_fake_user_toggle";
463
464 /* API callbacks. */
466
467 /* flags */
469}
470
472{
473 PropertyPointerRNA pprop;
474 PointerRNA idptr;
475
477
478 if (pprop.prop) {
479 idptr = RNA_property_pointer_get(&pprop.ptr, pprop.prop);
480 }
481
482 if ((pprop.prop == nullptr) || !idptr || !RNA_struct_is_ID(idptr.type)) {
484 op->reports, RPT_ERROR, "Incorrect context for running data-block fake user toggling");
485 return OPERATOR_CANCELLED;
486 }
487
488 idptr = {};
489 RNA_property_pointer_set(&pprop.ptr, pprop.prop, idptr, nullptr);
490 RNA_property_update(C, &pprop.ptr, pprop.prop);
491
492 return OPERATOR_FINISHED;
493}
494
496{
497 /* identifiers */
498 ot->name = "Unlink Data-Block";
499 ot->description = "Remove a usage of a data-block, clearing the assignment";
500 ot->idname = "ED_OT_lib_id_unlink";
501
502 /* API callbacks. */
504
505 /* flags */
507}
508
510{
511 const PointerRNA id_ptr = CTX_data_pointer_get_type(C, "id", RNA_ID);
512 const ID *id = static_cast<ID *>(id_ptr.data);
513
514 return id && ID_IS_OVERRIDE_LIBRARY_REAL(id) && !ID_IS_LINKED(id);
515}
516
518{
519 Main *bmain = CTX_data_main(C);
520 const PointerRNA id_ptr = CTX_data_pointer_get_type(C, "id", RNA_ID);
521 ID *id = static_cast<ID *>(id_ptr.data);
522
523 const bool is_system_override = BKE_lib_override_library_is_system_defined(bmain, id);
524 if (is_system_override) {
525 /* A system override is not editable. Make it an editable (non-system-defined) one. */
526 id->override_library->flag &= ~LIBOVERRIDE_FLAG_SYSTEM_DEFINED;
527 }
528 else {
529 /* Reset override, which makes it non-editable (i.e. a system define override). */
530 BKE_lib_override_library_id_reset(bmain, id, true);
531
534 }
535
537
538 return OPERATOR_FINISHED;
539}
540
542{
543 /* identifiers */
544 ot->name = "Toggle Library Override Editable";
545 ot->description = "Set if this library override data-block can be edited";
546 ot->idname = "ED_OT_lib_id_override_editable_toggle";
547
548 /* API callbacks. */
551
552 /* flags */
554}
555
557
558/* -------------------------------------------------------------------- */
561
563{
564 Main *bmain = CTX_data_main(C);
566 return OPERATOR_FINISHED;
567}
568
570{
571 /* identifiers */
572 ot->name = "Flush Edits";
573 ot->description = "Flush edit data from active editing modes";
574 ot->idname = "ED_OT_flush_edits";
575
576 /* API callbacks. */
578
579 /* flags */
580 ot->flag = OPTYPE_INTERNAL;
581}
582
584
604
605} // namespace blender
File and directory operations.
#define FILE_MAX
#define RPT_(msgid)
#define ID_FAKE_USERS(id)
Definition DNA_ID.h:658
#define ID_IS_OVERRIDE_LIBRARY_REAL(_id)
Definition DNA_ID.h:729
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
#define C
Definition RandGen.cpp:29
#define NC_WINDOW
Definition WM_types.hh:388
#define NC_WM
Definition WM_types.hh:387
#define ND_LIB_OVERRIDE_CHANGED
Definition WM_types.hh:435
#define NA_EDITED
Definition WM_types.hh:634
#define NC_ASSET
Definition WM_types.hh:417
#define ND_SPACE_VIEW3D
Definition WM_types.hh:571
#define NC_SPACE
Definition WM_types.hh:405
#define ELEM(...)
void append(const T &value)
bool is_empty() const
void context_active_but_prop_get_templateID(const bContext *C, PointerRNA *r_ptr, PropertyRNA **r_prop)
void icon_render_id_ex(const bContext *C, Scene *scene, ID *id_to_render, const enum eIconSizes size, const bool use_job, PreviewImage *r_preview_image)
void icon_render_id(const bContext *C, Scene *scene, ID *id, enum eIconSizes size, bool use_job)
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
static void ED_OT_lib_id_override_editable_toggle(wmOperatorType *ot)
bool CTX_data_selected_ids(const bContext *C, Vector< PointerRNA > *list)
void BKE_previewimg_id_custom_set(ID *id, const char *filepath)
bool ED_preview_id_render_is_supported(const ID *id, const char **r_disabled_hint=nullptr)
static bool lib_id_generate_preview_from_object_poll(bContext *C)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static bool lib_id_override_editable_toggle_poll(bContext *C)
void WM_operatortype_append(void(*opfunc)(wmOperatorType *))
void ED_OT_undo_push(wmOperatorType *ot)
Definition ed_undo.cc:818
PointerRNA RNA_property_pointer_get(PointerRNA *ptr, PropertyRNA *prop)
static wmOperatorStatus lib_id_generate_preview_from_object_exec(bContext *C, wmOperator *)
void BKE_previewimg_id_free(ID *id)
static void lib_id_batch_edit_previews(bContext *C, FunctionRef< void(ID *)> foreach_id)
void BKE_previewimg_clear(PreviewImage *prv)
@ ICON_SIZE_PREVIEW
bool ED_editors_flush_edits(Main *bmain)
Definition ed_util.cc:335
static void ED_OT_lib_id_generate_preview(wmOperatorType *ot)
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
void RNA_property_pointer_set(PointerRNA *ptr, PropertyRNA *prop, PointerRNA ptr_value, ReportList *reports)
static bool lib_id_preview_editing_poll_ex(const ID *id, const char **r_disabled_hint)
void ED_OT_undo_redo(wmOperatorType *ot)
Definition ed_undo.cc:861
wmOperatorStatus WM_operator_filesel(bContext *C, wmOperator *op, const wmEvent *)
static wmOperatorStatus lib_id_fake_user_toggle_exec(bContext *C, wmOperator *op)
static bool lib_id_remove_preview_poll(bContext *C)
static bool lib_id_generate_preview_poll(bContext *C)
void ED_OT_undo(wmOperatorType *ot)
Definition ed_undo.cc:806
static wmOperatorStatus lib_id_unlink_exec(bContext *C, wmOperator *op)
void ED_OT_redo(wmOperatorType *ot)
Definition ed_undo.cc:849
static bool lib_id_batch_editing_preview_poll(bContext *C, FunctionRef< bool(const ID *, const char **r_disabled_hint)> additional_condition=nullptr)
bool BKE_lib_override_library_is_system_defined(const Main *bmain, const ID *id)
void id_fake_user_set(ID *id)
Definition lib_id.cc:471
@ LIBOVERRIDE_FLAG_SYSTEM_DEFINED
Definition DNA_ID.h:308
static void ED_OT_flush_edits(wmOperatorType *ot)
void WM_operator_properties_filesel(wmOperatorType *ot, const int filter, const short type, const eFileSel_Action action, const eFileSel_Flag flag, const short display, const short sort)
static void ED_OT_lib_id_remove_preview(wmOperatorType *ot)
PreviewImage ** BKE_previewimg_id_get_p(const ID *id)
Vector< PointerRNA > ED_operator_get_ids_from_context_as_vec(const bContext *C)
Global G
Definition blender.cc:57
void ED_preview_kill_jobs(wmWindowManager *wm, Main *bmain)
void CTX_wm_operator_poll_msg_set(bContext *C, const char *msg)
static wmOperatorStatus lib_id_generate_preview_exec(bContext *C, wmOperator *)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
@ WM_FILESEL_FILEPATH
Definition WM_api.hh:1332
PointerRNA CTX_data_pointer_get(const bContext *C, const char *member)
static wmOperatorStatus ed_flush_edits_exec(bContext *C, wmOperator *)
wmWindowManager * CTX_wm_manager(const bContext *C)
PreviewImage * BKE_previewimg_id_ensure(ID *id)
void BKE_lib_override_library_id_reset(Main *bmain, ID *id_root, bool do_reset_system_override)
void ED_OT_undo_history(wmOperatorType *ot)
Definition ed_undo.cc:1002
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
PreviewImage * BKE_previewimg_id_get(const ID *id)
bool RNA_struct_is_ID(const StructRNA *type)
static void ED_OT_lib_id_unlink(wmOperatorType *ot)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
@ FILE_OPENFILE
Definition WM_api.hh:1341
Main * CTX_data_main(const bContext *C)
static wmOperatorStatus lib_id_remove_preview_exec(bContext *C, wmOperator *)
static void ED_OT_lib_id_fake_user_toggle(wmOperatorType *ot)
ID_Type GS(const char *name)
Definition DNA_ID.h:410
static wmOperatorStatus lib_id_load_custom_preview_invoke(bContext *C, wmOperator *op, const wmEvent *event)
static void ED_OT_lib_id_generate_preview_from_object(wmOperatorType *ot)
static ID * lib_id_load_custom_preview_id_get(bContext *C, const wmOperator *op)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ OPTYPE_INTERNAL
Definition WM_types.hh:210
std::string RNA_string_get(PointerRNA *ptr, const char *name)
bool BKE_id_is_editable(const Main *bmain, const ID *id)
Definition lib_id.cc:2782
static wmOperatorStatus lib_id_load_custom_preview_exec(bContext *C, wmOperator *op)
bool BLI_is_file(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:469
Vector< PointerRNA > ED_operator_single_id_from_context_as_vec(const bContext *C)
Object * CTX_data_active_object(const bContext *C)
static wmOperatorStatus lib_id_override_editable_toggle_exec(bContext *C, wmOperator *)
void id_fake_user_clear(ID *id)
Definition lib_id.cc:479
PointerRNA CTX_data_pointer_get_type(const bContext *C, const char *member, StructRNA *type)
static void ED_OT_lib_id_load_custom_preview(wmOperatorType *ot)
void ED_operatortypes_edutils()
static bool lib_id_preview_editing_poll(bContext *C)
StructRNA * type
Definition RNA_types.hh:63
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
struct ReportList * reports