Blender  V2.93
ed_util_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 
23 #include <cstring>
24 
25 #include "DNA_space_types.h"
27 
28 #include "BLI_fileops.h"
29 #include "BLI_utildefines.h"
30 
31 #include "BKE_context.h"
32 #include "BKE_icons.h"
33 #include "BKE_lib_id.h"
34 #include "BKE_main.h"
35 #include "BKE_report.h"
36 
37 #include "BLT_translation.h"
38 
39 #include "ED_render.h"
40 #include "ED_undo.h"
41 #include "ED_util.h"
42 
43 #include "RNA_access.h"
44 
45 #include "UI_interface.h"
46 
47 #include "WM_api.h"
48 #include "WM_types.h"
49 
50 /* -------------------------------------------------------------------- */
55 {
56  const PointerRNA idptr = CTX_data_pointer_get(C, "id");
57  BLI_assert(!idptr.data || RNA_struct_is_ID(idptr.type));
58 
59  const ID *id = (ID *)idptr.data;
60  if (!id) {
61  return false;
62  }
63  if (ID_IS_LINKED(id)) {
64  CTX_wm_operator_poll_msg_set(C, TIP_("Can't edit external library data"));
65  return false;
66  }
67  if (ID_IS_OVERRIDE_LIBRARY(id)) {
68  CTX_wm_operator_poll_msg_set(C, TIP_("Can't edit previews of overridden library data"));
69  return false;
70  }
71  if (!BKE_previewimg_id_get_p(id)) {
72  CTX_wm_operator_poll_msg_set(C, TIP_("Data-block does not support previews"));
73  return false;
74  }
75 
76  return true;
77 }
78 
80 {
81  char path[FILE_MAX];
82 
83  RNA_string_get(op->ptr, "filepath", path);
84 
85  if (!BLI_is_file(path)) {
86  BKE_reportf(op->reports, RPT_ERROR, "File not found '%s'", path);
87  return OPERATOR_CANCELLED;
88  }
89 
90  PointerRNA idptr = CTX_data_pointer_get(C, "id");
91  ID *id = (ID *)idptr.data;
92 
94 
96 
97  return OPERATOR_FINISHED;
98 }
99 
101 {
102  ot->name = "Load Custom Preview";
103  ot->description = "Choose an image to help identify the data-block visually";
104  ot->idname = "ED_OT_lib_id_load_custom_preview";
105 
106  /* api callbacks */
110 
111  /* flags */
113 
116  FILE_SPECIAL,
121 }
122 
124 {
125  PointerRNA idptr = CTX_data_pointer_get(C, "id");
126  ID *id = (ID *)idptr.data;
127 
129 
130  PreviewImage *preview = BKE_previewimg_id_get(id);
131  if (preview) {
132  BKE_previewimg_clear(preview);
133  }
134  UI_icon_render_id(C, nullptr, id, ICON_SIZE_PREVIEW, true);
135 
137 
138  return OPERATOR_FINISHED;
139 }
140 
142 {
143  ot->name = "Generate Preview";
144  ot->description = "Create an automatic preview for the selected data-block";
145  ot->idname = "ED_OT_lib_id_generate_preview";
146 
147  /* api callbacks */
150 
151  /* flags */
153 }
154 
157 /* -------------------------------------------------------------------- */
162 {
163  PropertyPointerRNA pprop;
164  PointerRNA idptr = PointerRNA_NULL;
165 
167 
168  if (pprop.prop) {
169  idptr = RNA_property_pointer_get(&pprop.ptr, pprop.prop);
170  }
171 
172  if ((pprop.prop == nullptr) || RNA_pointer_is_null(&idptr) || !RNA_struct_is_ID(idptr.type)) {
173  BKE_report(
174  op->reports, RPT_ERROR, "Incorrect context for running data-block fake user toggling");
175  return OPERATOR_CANCELLED;
176  }
177 
178  ID *id = (ID *)idptr.data;
179 
180  if ((id->lib != nullptr) || (ELEM(GS(id->name), ID_GR, ID_SCE, ID_SCR, ID_TXT, ID_OB, ID_WS))) {
181  BKE_report(op->reports, RPT_ERROR, "Data-block type does not support fake user");
182  return OPERATOR_CANCELLED;
183  }
184 
185  if (ID_FAKE_USERS(id)) {
186  id_fake_user_clear(id);
187  }
188  else {
189  id_fake_user_set(id);
190  }
191 
192  return OPERATOR_FINISHED;
193 }
194 
196 {
197  /* identifiers */
198  ot->name = "Toggle Fake User";
199  ot->description = "Save this data-block even if it has no users";
200  ot->idname = "ED_OT_lib_id_fake_user_toggle";
201 
202  /* api callbacks */
204 
205  /* flags */
207 }
208 
210 {
211  PropertyPointerRNA pprop;
212  PointerRNA idptr;
213 
215 
216  if (pprop.prop) {
217  idptr = RNA_property_pointer_get(&pprop.ptr, pprop.prop);
218  }
219 
220  if ((pprop.prop == nullptr) || RNA_pointer_is_null(&idptr) || !RNA_struct_is_ID(idptr.type)) {
221  BKE_report(
222  op->reports, RPT_ERROR, "Incorrect context for running data-block fake user toggling");
223  return OPERATOR_CANCELLED;
224  }
225 
226  memset(&idptr, 0, sizeof(idptr));
227  RNA_property_pointer_set(&pprop.ptr, pprop.prop, idptr, nullptr);
228  RNA_property_update(C, &pprop.ptr, pprop.prop);
229 
230  return OPERATOR_FINISHED;
231 }
232 
234 {
235  /* identifiers */
236  ot->name = "Unlink Data-Block";
237  ot->description = "Remove a usage of a data-block, clearing the assignment";
238  ot->idname = "ED_OT_lib_id_unlink";
239 
240  /* api callbacks */
242 
243  /* flags */
245 }
246 
249 /* -------------------------------------------------------------------- */
254 {
255  Main *bmain = CTX_data_main(C);
256  ED_editors_flush_edits(bmain);
257  return OPERATOR_FINISHED;
258 }
259 
261 {
262  /* identifiers */
263  ot->name = "Flush Edits";
264  ot->description = "Flush edit data from active editing modes";
265  ot->idname = "ED_OT_flush_edits";
266 
267  /* api callbacks */
269 
270  /* flags */
272 }
273 
277 {
280 
283 
285 
291 }
PointerRNA CTX_data_pointer_get(const bContext *C, const char *member)
Definition: context.c:445
struct wmWindowManager * CTX_wm_manager(const bContext *C)
Definition: context.c:689
void CTX_wm_operator_poll_msg_set(struct bContext *C, const char *msg)
Definition: context.c:1006
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1018
void BKE_previewimg_clear(struct PreviewImage *prv)
Definition: icons.cc:315
struct PreviewImage ** BKE_previewimg_id_get_p(const struct ID *id)
void BKE_previewimg_id_custom_set(struct ID *id, const char *path)
Definition: icons.cc:414
struct PreviewImage * BKE_previewimg_id_get(const struct ID *id)
void id_fake_user_set(struct ID *id)
Definition: lib_id.c:328
void id_fake_user_clear(struct ID *id)
Definition: lib_id.c:336
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
File and directory operations.
bool BLI_is_file(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: storage.c:444
#define FILE_MAX
#define UNUSED(x)
#define ELEM(...)
#define TIP_(msgid)
#define ID_FAKE_USERS(id)
Definition: DNA_ID.h:412
#define ID_IS_LINKED(_id)
Definition: DNA_ID.h:426
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition: DNA_ID.h:445
@ ICON_SIZE_PREVIEW
Definition: DNA_ID_enums.h:30
@ ID_WS
Definition: DNA_ID_enums.h:91
@ ID_TXT
Definition: DNA_ID_enums.h:74
@ ID_SCE
Definition: DNA_ID_enums.h:57
@ ID_SCR
Definition: DNA_ID_enums.h:72
@ ID_GR
Definition: DNA_ID_enums.h:77
@ ID_OB
Definition: DNA_ID_enums.h:59
@ FILE_SORT_DEFAULT
@ FILE_SPECIAL
@ FILE_TYPE_FOLDER
@ FILE_TYPE_IMAGE
@ FILE_OPENFILE
@ FILE_DEFAULTDISPLAY
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
void ED_preview_kill_jobs(struct wmWindowManager *wm, struct Main *bmain)
void ED_OT_undo_history(struct wmOperatorType *ot)
Definition: ed_undo.c:856
void ED_OT_undo(struct wmOperatorType *ot)
Definition: ed_undo.c:604
void ED_OT_undo_redo(struct wmOperatorType *ot)
Definition: ed_undo.c:659
void ED_OT_redo(struct wmOperatorType *ot)
Definition: ed_undo.c:647
void ED_OT_undo_push(struct wmOperatorType *ot)
Definition: ed_undo.c:616
bool ED_editors_flush_edits(struct Main *bmain)
Definition: ed_util.c:295
#define C
Definition: RandGen.cpp:39
void UI_context_active_but_prop_get_templateID(struct bContext *C, struct PointerRNA *r_ptr, struct PropertyRNA **r_prop)
void UI_icon_render_id(const struct bContext *C, struct Scene *scene, struct ID *id, const enum eIconSizes size, const bool use_job)
#define WM_FILESEL_FILEPATH
Definition: WM_api.h:537
@ OPTYPE_INTERNAL
Definition: WM_types.h:175
@ OPTYPE_UNDO
Definition: WM_types.h:155
@ OPTYPE_REGISTER
Definition: WM_types.h:153
#define NA_EDITED
Definition: WM_types.h:462
#define NC_ASSET
Definition: WM_types.h:305
static int lib_id_fake_user_toggle_exec(bContext *C, wmOperator *op)
Definition: ed_util_ops.cc:161
static void ED_OT_lib_id_generate_preview(wmOperatorType *ot)
Definition: ed_util_ops.cc:141
static void ED_OT_lib_id_unlink(wmOperatorType *ot)
Definition: ed_util_ops.cc:233
static int ed_flush_edits_exec(bContext *C, wmOperator *UNUSED(op))
Definition: ed_util_ops.cc:253
static int lib_id_load_custom_preview_exec(bContext *C, wmOperator *op)
Definition: ed_util_ops.cc:79
void ED_operatortypes_edutils(void)
Definition: ed_util_ops.cc:276
static void ED_OT_flush_edits(wmOperatorType *ot)
Definition: ed_util_ops.cc:260
static int lib_id_generate_preview_exec(bContext *C, wmOperator *UNUSED(op))
Definition: ed_util_ops.cc:123
static bool lib_id_preview_editing_poll(bContext *C)
Definition: ed_util_ops.cc:54
static void ED_OT_lib_id_load_custom_preview(wmOperatorType *ot)
Definition: ed_util_ops.cc:100
static void ED_OT_lib_id_fake_user_toggle(wmOperatorType *ot)
Definition: ed_util_ops.cc:195
static int lib_id_unlink_exec(bContext *C, wmOperator *op)
Definition: ed_util_ops.cc:209
#define GS(x)
Definition: iris.c:241
bool RNA_struct_is_ID(const StructRNA *type)
Definition: rna_access.c:797
void RNA_property_pointer_set(PointerRNA *ptr, PropertyRNA *prop, PointerRNA ptr_value, ReportList *reports)
Definition: rna_access.c:3673
const PointerRNA PointerRNA_NULL
Definition: rna_access.c:71
PointerRNA RNA_property_pointer_get(PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:3641
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
Definition: rna_access.c:2317
void RNA_string_get(PointerRNA *ptr, const char *name, char *value)
Definition: rna_access.c:6514
bool RNA_pointer_is_null(const PointerRNA *ptr)
Definition: rna_access.c:174
Definition: DNA_ID.h:273
struct Library * lib
Definition: DNA_ID.h:277
char name[66]
Definition: DNA_ID.h:283
Definition: BKE_main.h:116
struct StructRNA * type
Definition: RNA_types.h:51
void * data
Definition: RNA_types.h:52
struct PropertyRNA * prop
Definition: RNA_types.h:57
PointerRNA ptr
Definition: RNA_types.h:56
int(* invoke)(struct bContext *, struct wmOperator *, const struct wmEvent *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:752
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
struct PointerRNA * ptr
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
wmOperatorType * ot
Definition: wm_files.c:3156
void WM_operator_properties_filesel(wmOperatorType *ot, int filter, short type, short action, short flag, short display, short sort)
void WM_operatortype_append(void(*opfunc)(wmOperatorType *))
int WM_operator_filesel(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))