Blender V5.3
ed_util.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cmath>
10#include <cstdlib>
11#include <cstring>
12
13#include "BLI_listbase.hh"
14#include "BLI_path_utils.hh"
15#include "BLI_string_utf8.hh"
16
17#include "BLT_translation.hh"
18
19#include "BKE_collection.hh"
20#include "BKE_global.hh"
21#include "BKE_layer.hh"
22#include "BKE_lib_id.hh"
23#include "BKE_lib_remap.hh"
24#include "BKE_main.hh"
25#include "BKE_material.hh"
26#include "BKE_multires.hh"
27#include "BKE_object.hh"
28#include "BKE_object_types.hh"
29#include "BKE_packedFile.hh"
30#include "BKE_paint.hh"
31#include "BKE_scene.hh"
32#include "BKE_screen.hh"
33#include "BKE_undo_system.hh"
34
35#include "DEG_depsgraph.hh"
36
37#include "ED_armature.hh"
38#include "ED_asset.hh"
39#include "ED_gpencil_legacy.hh"
40#include "ED_image.hh"
41#include "ED_mesh.hh"
42#include "ED_object.hh"
43#include "ED_paint.hh"
44#include "ED_screen.hh"
45#include "ED_screen_types.hh"
46#include "ED_sculpt.hh"
47#include "ED_space_api.hh"
48#include "ED_util.hh"
49#include "ED_view3d.hh"
50
51#include "UI_interface.hh"
53#include "UI_resources.hh"
54
55#include "RNA_access.hh"
56
57#include "WM_api.hh"
58#include "WM_types.hh"
59
60namespace blender {
61
62/* ********* general editor util functions, not BKE stuff please! ********* */
63
65{
66 wmWindowManager *wm = bmain->wm.first();
67 for (wmWindow &win : wm->windows) {
68 Scene *scene = WM_window_get_active_scene(&win);
70 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
72 if (ob && (ob->mode & OB_MODE_TEXTURE_PAINT)) {
74 ED_paint_proj_mesh_data_check(*scene, *ob, nullptr, nullptr, nullptr, nullptr);
75 }
76
77 /* Stop animation from playing.
78 * TODO: There might be a way to keep the animation from playing, but sad->scene and
79 * sad->view_layer pointers are outdated and would need to be updated somehow. */
81 if (animscreen && animscreen->animtimer) {
82 WM_event_timer_remove(wm, &win, animscreen->animtimer);
83 animscreen->animtimer = nullptr;
84 }
85
86 /* UI Updates. */
87 /* Flag local View3D's to check and exit if they are empty. */
88 for (bScreen &screen : bmain->screens) {
89 for (ScrArea &area : screen.areabase) {
90 for (SpaceLink &sl : area.spacedata) {
91 if (sl.spacetype == SPACE_VIEW3D) {
92 View3D *v3d = reinterpret_cast<View3D *>(&sl);
93 if (v3d->localvd) {
95 }
96 }
97 }
98 }
99 }
100 }
101}
102
104{
105 using namespace blender::ed;
106 Depsgraph *depsgraph = CTX_data_expect_evaluated_depsgraph(C);
107 Main *bmain = CTX_data_main(C);
108 Scene *scene = CTX_data_scene(C);
110
111 /* This is called during initialization, so we don't want to store any reports */
112 ReportList *reports = CTX_wm_reports(C);
113 int reports_flag_prev = reports->flag & ~RPT_STORE;
114
115 std::swap(reports->flag, reports_flag_prev);
116
117 /* Don't do undo pushes when calling an operator. */
118 wm->op_undo_depth++;
119
120 /* toggle on modes for objects that were saved with these enabled. for
121 * e.g. linked objects we have to ensure that they are actually the
122 * active object in this scene. */
124 for (Object &ob : bmain->objects) {
125 eObjectMode mode = ob.mode;
126 if (mode == OB_MODE_OBJECT) {
127 continue;
128 }
129 if (BKE_object_has_mode_data(&ob, mode)) {
130 /* For multi-edit mode we may already have mode data. */
131 continue;
132 }
133
134 /* Reset object to Object mode, so that code below can properly re-switch it to its
135 * previous mode if possible, re-creating its mode data, etc. */
136 ID *ob_data = ob.data;
137 ob.mode = OB_MODE_OBJECT;
139
140 /* Object mode is enforced if there is no active object, or if the active object's type is
141 * different. */
142 if (obact == nullptr || ob.type != obact->type) {
143 continue;
144 }
145 /* Object mode is enforced for non-editable data (or their obdata). */
146 if (!BKE_id_is_editable(bmain, &ob.id) ||
147 (ob_data != nullptr && !BKE_id_is_editable(bmain, ob_data)))
148 {
149 continue;
150 }
151
152 /* Pose mode is very similar to Object one, we can apply it even on objects not in current
153 * scene. */
154 if (mode == OB_MODE_POSE) {
155 ED_object_posemode_enter_ex(bmain, &ob);
156 }
157
158 /* Other edit/paint/etc. modes are only settable for objects visible in active scene currently.
159 * Otherwise, they (and their obdata) may not be (fully) evaluated, which is mandatory for some
160 * modes like Sculpt.
161 * Ref. #98225. */
163 !BKE_scene_has_object(*bmain, scene, &ob) || (ob.visibility_flag & OB_HIDE_VIEWPORT) != 0)
164 {
165 continue;
166 }
167
168 if (mode == OB_MODE_EDIT) {
169 object::editmode_enter_ex(bmain, scene, &ob, 0);
170 }
171 else if (BKE_object_use_sculptsession(mode)) {
172 if (obact == &ob) {
173 if (mode == OB_MODE_SCULPT) {
175 *bmain, *depsgraph, *scene, ob, true, reports);
176 }
177 else if (mode == OB_MODE_VERTEX_PAINT) {
178 ED_object_vpaintmode_enter_ex(*bmain, *depsgraph, *scene, ob);
179 }
180 else if (mode == OB_MODE_WEIGHT_PAINT) {
181 ED_object_wpaintmode_enter_ex(*bmain, *depsgraph, *scene, ob);
182 }
183 else if (mode == OB_MODE_TEXTURE_PAINT) {
184 ED_object_texture_paint_mode_enter_ex(*bmain, *scene, *depsgraph, ob);
185 }
186 else {
188 }
189 }
190 else {
191 /* Create data for non-active objects which need it for
192 * mode-switching but don't yet support multi-editing. */
194 ob.mode = mode;
196 }
197 }
198 }
199 else {
200 /* TODO(@ideasman42): avoid operator calls. */
201 if (obact == &ob) {
202 object::mode_set_ex(C, mode, true, reports);
203 }
204 }
205 }
206
207 /* image editor paint mode */
208 if (scene) {
209 ED_space_image_paint_update(bmain, wm, scene);
210 }
211
212 /* Enforce a full redraw for the first time areas/regions get drawn. Further region init/refresh
213 * just triggers non-rebuild redraws (#RGN_DRAW_NO_REBUILD). Usually a full redraw would be
214 * triggered by a `NC_WM | ND_FILEREAD` notifier, but if a startup script calls an operator that
215 * redraws the window, notifiers are not handled before the operator runs. See #98461. */
216 for (wmWindow &win : wm->windows) {
217 const bScreen *screen = WM_window_get_active_screen(&win);
218
219 ED_screen_areas_iter (&win, screen, area) {
220 ED_area_tag_redraw(area);
221 }
222 }
223
225
226 std::swap(reports->flag, reports_flag_prev);
227 wm->op_undo_depth--;
228}
229
230void ED_editors_exit(Main *bmain, bool do_undo_system)
231{
232 using namespace blender::ed;
233 if (!bmain) {
234 return;
235 }
236
237 /* Frees all edit-mode undo-steps. */
238 if (do_undo_system && G_MAIN->wm.first_) {
239 wmWindowManager *wm = G_MAIN->wm.first();
240 /* normally we don't check for null undo stack,
241 * do here since it may run in different context. */
242 if (wm->runtime->undo_stack) {
243 BKE_undosys_stack_destroy(wm->runtime->undo_stack);
244 wm->runtime->undo_stack = nullptr;
245 }
246 }
247
248 /* On undo, tag for update so the depsgraph doesn't use stale edit-mode data,
249 * this is possible when mixing edit-mode and memory-file undo.
250 *
251 * By convention, objects are not left in edit-mode - so this isn't often problem in practice,
252 * since exiting edit-mode will tag the objects too.
253 *
254 * However there is no guarantee the active object _never_ changes while in edit-mode.
255 * Python for example can do this, some callers to #object::base_activate
256 * don't handle modes either (doing so isn't always practical).
257 *
258 * To reproduce the problem where stale data is used, see: #84920. */
259 for (Object &ob : bmain->objects) {
260 if (object::editmode_free_ex(bmain, &ob)) {
261 if (do_undo_system == false) {
264 }
265 }
266 }
267
268 /* global in meshtools... */
271}
272
273bool ED_editors_flush_edits_for_object_ex(Main *bmain, Object *ob, bool check_needs_flush)
274{
275 using namespace blender::ed;
276 bool has_edited = false;
277 if (ob->mode & OB_MODE_SCULPT) {
278 /* Don't allow flushing while in the middle of a stroke (frees data in use).
279 * Auto-save prevents this from happening but scripts
280 * may cause a flush on saving: #53986. */
281 if (ob->runtime->sculpt_session != nullptr && ob->runtime->sculpt_session->cache == nullptr) {
282 if (check_needs_flush && !ob->runtime->sculpt_session->needs_flush_to_id) {
283 return false;
284 }
286 has_edited = true;
287
288 /* flush multires changes */
290
291 /* flush dyntopo changes */
293
295 }
296 }
297 else if (ob->mode & OB_MODE_EDIT) {
298
299 char *needs_flush_ptr = BKE_object_data_editmode_flush_ptr_get(ob->data);
300 if (needs_flush_ptr != nullptr) {
301 if (check_needs_flush && (*needs_flush_ptr == 0)) {
302 return false;
303 }
304 *needs_flush_ptr = 0;
305 }
306
307 /* get editmode results */
308 has_edited = true;
309 object::editmode_load(bmain, ob);
310 }
311 return has_edited;
312}
313
315{
316 return ED_editors_flush_edits_for_object_ex(bmain, ob, false);
317}
318
319bool ED_editors_flush_edits_ex(Main *bmain, bool check_needs_flush)
320{
321 bool has_edited = false;
322
323 /* loop through all data to find edit mode or object mode, because during
324 * exiting we might not have a context for edit object and multiple sculpt
325 * objects can exist at the same time */
326 for (Object &ob : bmain->objects) {
327 has_edited |= ED_editors_flush_edits_for_object_ex(bmain, &ob, check_needs_flush);
328 }
329
330 bmain->is_memfile_undo_flush_needed = false;
331
332 return has_edited;
333}
334
336{
337 return ED_editors_flush_edits_ex(bmain, false);
338}
339
340/* ***** XXX: functions are using old blender names, cleanup later ***** */
341
343 bool shift, bool ctrl, float *val, float fac1, float fac2, float fac3, int invert)
344{
345 /* fac1 is for 'nothing', fac2 for CTRL, fac3 for SHIFT */
346 if (invert) {
347 ctrl = !ctrl;
348 }
349
350 if (ctrl && shift) {
351 if (fac3 != 0.0f) {
352 *val = fac3 * floorf(*val / fac3 + 0.5f);
353 }
354 }
355 else if (ctrl) {
356 if (fac2 != 0.0f) {
357 *val = fac2 * floorf(*val / fac2 + 0.5f);
358 }
359 }
360 else {
361 if (fac1 != 0.0f) {
362 *val = fac1 * floorf(*val / fac1 + 0.5f);
363 }
364 }
365}
366
368 const char *opname,
369 const char *id_name,
370 const char *abs_name,
371 const char *folder,
372 PackedFile *pf)
373{
374 Main *bmain = CTX_data_main(C);
375 char line[FILE_MAX + 100];
376 wmOperatorType *ot = WM_operatortype_find(opname, true);
377 const char *blendfile_path = BKE_main_blendfile_path(bmain);
378
379 ui::PopupMenu *pup = ui::popup_menu_begin(C, IFACE_("Unpack File"), ICON_NONE);
380 ui::Layout &layout = *popup_menu_layout(pup);
381
382 PointerRNA props_ptr = layout.op(
383 ot, IFACE_("Remove Pack"), ICON_NONE, wm::OpCallContext::ExecDefault, UI_ITEM_NONE);
384 RNA_enum_set(&props_ptr, "method", PF_REMOVE);
385 RNA_string_set(&props_ptr, "id", id_name);
386
387 if (blendfile_path[0] != '\0') {
388 char local_name[FILE_MAXDIR + FILE_MAX], fi[FILE_MAX];
389
390 BLI_path_split_file_part(abs_name, fi, sizeof(fi));
391 BLI_path_join(local_name, sizeof(local_name), "//", folder, fi);
392 if (!STREQ(abs_name, local_name)) {
393 switch (BKE_packedfile_compare_to_file(blendfile_path, local_name, pf)) {
394 case PF_CMP_NOFILE:
395 SNPRINTF_UTF8(line, IFACE_("Create %s"), local_name);
396 props_ptr = layout.op(ot, line, ICON_NONE, wm::OpCallContext::ExecDefault, UI_ITEM_NONE);
397 RNA_enum_set(&props_ptr, "method", PF_WRITE_LOCAL);
398 RNA_string_set(&props_ptr, "id", id_name);
399
400 break;
401 case PF_CMP_EQUAL:
402 SNPRINTF_UTF8(line, IFACE_("Use %s (identical)"), local_name);
403 props_ptr = layout.op(ot, line, ICON_NONE, wm::OpCallContext::ExecDefault, UI_ITEM_NONE);
404 RNA_enum_set(&props_ptr, "method", PF_USE_LOCAL);
405 RNA_string_set(&props_ptr, "id", id_name);
406
407 break;
408 case PF_CMP_DIFFERS:
409 SNPRINTF_UTF8(line, IFACE_("Use %s (differs)"), local_name);
410 props_ptr = layout.op(ot, line, ICON_NONE, wm::OpCallContext::ExecDefault, UI_ITEM_NONE);
411 RNA_enum_set(&props_ptr, "method", PF_USE_LOCAL);
412 RNA_string_set(&props_ptr, "id", id_name);
413
414 SNPRINTF_UTF8(line, IFACE_("Overwrite %s"), local_name);
415 props_ptr = layout.op(ot, line, ICON_NONE, wm::OpCallContext::ExecDefault, UI_ITEM_NONE);
416 RNA_enum_set(&props_ptr, "method", PF_WRITE_LOCAL);
417 RNA_string_set(&props_ptr, "id", id_name);
418 break;
419 }
420 }
421 }
422
423 switch (BKE_packedfile_compare_to_file(blendfile_path, abs_name, pf)) {
424 case PF_CMP_NOFILE:
425 SNPRINTF_UTF8(line, IFACE_("Create %s"), abs_name);
426 props_ptr = layout.op(ot, line, ICON_NONE, wm::OpCallContext::ExecDefault, UI_ITEM_NONE);
427 RNA_enum_set(&props_ptr, "method", PF_WRITE_ORIGINAL);
428 RNA_string_set(&props_ptr, "id", id_name);
429 break;
430 case PF_CMP_EQUAL:
431 SNPRINTF_UTF8(line, IFACE_("Use %s (identical)"), abs_name);
432 props_ptr = layout.op(ot, line, ICON_NONE, wm::OpCallContext::ExecDefault, UI_ITEM_NONE);
433 RNA_enum_set(&props_ptr, "method", PF_USE_ORIGINAL);
434 RNA_string_set(&props_ptr, "id", id_name);
435 break;
436 case PF_CMP_DIFFERS:
437 SNPRINTF_UTF8(line, IFACE_("Use %s (differs)"), abs_name);
438 props_ptr = layout.op(ot, line, ICON_NONE, wm::OpCallContext::ExecDefault, UI_ITEM_NONE);
439 RNA_enum_set(&props_ptr, "method", PF_USE_ORIGINAL);
440 RNA_string_set(&props_ptr, "id", id_name);
441
442 SNPRINTF_UTF8(line, IFACE_("Overwrite %s"), abs_name);
443 props_ptr = layout.op(ot, line, ICON_NONE, wm::OpCallContext::ExecDefault, UI_ITEM_NONE);
444 RNA_enum_set(&props_ptr, "method", PF_WRITE_ORIGINAL);
445 RNA_string_set(&props_ptr, "id", id_name);
446 break;
447 }
448
449 popup_menu_end(C, pup);
450}
451
453{
455 if (st && st->id_remap) {
456 st->id_remap(area, sl, mappings);
457 }
458}
459
460void ED_spacedata_id_remap_single(ScrArea *area, SpaceLink *sl, ID *old_id, ID *new_id)
461{
463
464 if (st && st->id_remap) {
465 bke::id::IDRemapper mappings;
466 mappings.add(old_id, new_id);
467 st->id_remap(area, sl, mappings);
468 }
469}
470
471} // namespace blender
#define G_MAIN
General operations, lookup, etc. for materials.
General operations, lookup, etc. for blender objects.
#define FILE_MAX
#define BLI_path_join(...)
#define FILE_MAXDIR
#define SNPRINTF_UTF8(dst, format,...)
#define IFACE_(msgid)
#define ED_screen_areas_iter(win, screen, area_name)
Definition ED_screen.hh:322
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define BLI_assert_unreachable()
#define STREQ(a, b)
void add(ID *old_id, ID *new_id)
#define pf(_x, _i)
Prefetch 64.
Definition gim_memory.h:48
float2x2 invert(float2x2 mat)
bool editmode_free_ex(Main *bmain, Object *obedit)
bool mode_set_ex(bContext *C, eObjectMode mode, bool use_undo, ReportList *reports)
bool editmode_enter_ex(Main *bmain, Scene *scene, Object *ob, int flag)
bool editmode_load(Main *bmain, Object *obedit)
void object_sculpt_mode_enter(Main &bmain, Depsgraph &depsgraph, Scene &scene, Object &ob, bool force_dyntopo, ReportList *reports)
PopupMenu * popup_menu_begin(bContext *C, const char *title, int icon) ATTR_NONNULL()
BMesh Iterators.
void ED_mesh_mirror_topo_table_end(Object *ob)
Definition meshtools.cc:253
@ V3D_RUNTIME_LOCAL_MAYBE_EMPTY
void BKE_object_sculpt_data_create(Object *ob)
Definition object.cc:4337
ePF_FileCompare BKE_packedfile_compare_to_file(const char *ref_file_name, const char *filepath_rel, const PackedFile *pf)
Object * BKE_view_layer_active_object_get(const ViewLayer *view_layer)
bool ED_editors_flush_edits(Main *bmain)
Definition ed_util.cc:335
void unpack_menu(bContext *C, const char *opname, const char *id_name, const char *abs_name, const char *folder, PackedFile *pf)
Definition ed_util.cc:367
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
void ED_mesh_mirror_spatial_table_end(Object *ob)
void ED_spacedata_id_remap_single(ScrArea *area, SpaceLink *sl, ID *old_id, ID *new_id)
Definition ed_util.cc:460
void ED_editors_init(bContext *C)
Definition ed_util.cc:103
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
void BKE_sculptsession_bm_to_me(Object *ob)
ViewLayer * WM_window_get_active_view_layer(const wmWindow *win)
void multires_flush_sculpt_updates(Object *object)
Definition multires.cc:277
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:1058
void ED_object_texture_paint_mode_enter_ex(Main &bmain, Scene &scene, Depsgraph &depsgraph, Object &ob)
bool BKE_scene_has_object(const Main &bmain, Scene *scene, Object *ob)
Scene * WM_window_get_active_scene(const wmWindow *win)
void ED_editors_init_for_undo(Main *bmain)
Definition ed_util.cc:64
void DEG_id_tag_update(ID *id, unsigned int flags)
@ OB_MODE_TEXTURE_PAINT
void ED_spacedata_id_remap(ScrArea *area, SpaceLink *sl, const bke::id::IDRemapper &mappings)
Definition ed_util.cc:452
wmWindowManager * CTX_wm_manager(const bContext *C)
bool BKE_object_has_mode_data(const Object *ob, eObjectMode object_mode)
Definition object.cc:2023
void ED_editors_exit(Main *bmain, bool do_undo_system)
Definition ed_util.cc:230
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
bool ED_paint_proj_mesh_data_check(Scene &scene, Object &ob, bool *r_has_uvs, bool *r_has_mat, bool *r_has_tex, bool *r_has_stencil)
bool BKE_object_use_sculptsession(eObjectMode mode)
Definition object.cc:4328
SpaceType * BKE_spacetype_from_id(int spaceid)
Definition screen.cc:354
char * BKE_object_data_editmode_flush_ptr_get(ID *id)
Definition object.cc:1961
bool ED_editors_flush_edits_for_object(Main *bmain, Object *ob)
Definition ed_util.cc:314
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)
void void void BLI_path_split_file_part(const char *filepath, char *file, size_t file_maxncpy) ATTR_NONNULL(1
Depsgraph * CTX_data_expect_evaluated_depsgraph(const bContext *C)
Main * CTX_data_main(const bContext *C)
bool ED_editors_flush_edits_for_object_ex(Main *bmain, Object *ob, bool check_needs_flush)
Definition ed_util.cc:273
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:1054
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
bool ED_object_posemode_enter_ex(Main *bmain, Object *ob)
Definition pose_edit.cc:81
void BKE_texpaint_slots_refresh_object(Scene *scene, Object *ob)
Scene * CTX_data_scene(const bContext *C)
void WM_event_timer_remove(wmWindowManager *wm, wmWindow *, wmTimer *timer)
void ED_object_vpaintmode_enter_ex(Main &bmain, Depsgraph &depsgraph, Scene &scene, Object &ob)
bool BKE_collection_has_object_recursive(Collection *collection, Object *ob)
void BKE_undosys_stack_destroy(UndoStack *ustack)
void ED_space_image_paint_update(Main *bmain, wmWindowManager *wm, Scene *scene)
ReportList * CTX_wm_reports(const bContext *C)
bool BKE_id_is_editable(const Main *bmain, const ID *id)
Definition lib_id.cc:2782
bScreen * WM_window_get_active_screen(const wmWindow *win)
Object * CTX_data_active_object(const bContext *C)
bool ED_editors_flush_edits_ex(Main *bmain, bool check_needs_flush)
Definition ed_util.cc:319
bScreen * ED_screen_animation_playing(const wmWindowManager *wm)
void ED_object_wpaintmode_enter_ex(Main &bmain, Depsgraph &depsgraph, Scene &scene, Object &ob)
void apply_keyb_grid(bool shift, bool ctrl, float *val, float fac1, float fac2, float fac3, int invert)
Definition ed_util.cc:342
void ED_area_tag_redraw(ScrArea *area)
Definition area.cc:784
#define floorf
ListBaseT< wmWindowManager > wm
Definition BKE_main.hh:415
bool is_memfile_undo_flush_needed
Definition BKE_main.hh:302
ListBaseT< Object > objects
Definition BKE_main.hh:388
ListBaseT< bScreen > screens
Definition BKE_main.hh:400
bke::ObjectRuntime * runtime
eObject_VisibilityFlag visibility_flag
struct Collection * master_collection
ListBaseT< SpaceLink > spacedata
ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:409
void(*) id_remap(ScrArea *area, SpaceLink *sl, const bke::id::IDRemapper &mappings)
struct View3D * localvd
View3D_Runtime runtime
struct wmTimer * animtimer
ListBaseT< struct ScrArea > areabase
PointerRNA op(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag)