Blender V5.0
undo.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_task.hh"
10
12#include "BKE_context.hh"
13#include "BKE_main.hh"
14#include "BKE_object.hh"
15#include "BKE_pointcloud.hh"
16#include "BKE_undo_system.hh"
17
18#include "CLG_log.h"
19
20#include "DEG_depsgraph.hh"
21
22#include "ED_pointcloud.hh"
23#include "ED_undo.hh"
24
25#include "WM_api.hh"
26#include "WM_types.hh"
27
28static CLG_LogRef LOG = {"undo.pointcloud"};
29
31namespace undo {
32
33/* -------------------------------------------------------------------- */
38
39struct StepObject {
40 UndoRefID_Object obedit_ref = {};
42 int totpoint = 0;
43 /* Store the bounds caches because they are small. */
46};
47
54
55static bool step_encode(bContext *C, Main *bmain, UndoStep *us_p)
56{
57 PointCloudUndoStep *us = reinterpret_cast<PointCloudUndoStep *>(us_p);
58
59 Scene *scene = CTX_data_scene(C);
60 ViewLayer *view_layer = CTX_data_view_layer(C);
62
63 us->scene_ref.ptr = scene;
64 new (&us->objects) Array<StepObject>(objects.size());
65
66 threading::parallel_for(us->objects.index_range(), 8, [&](const IndexRange range) {
67 for (const int i : range) {
68 Object *ob = objects[i];
69 StepObject &object = us->objects[i];
70 const PointCloud &pointcloud = *static_cast<const PointCloud *>(ob->data);
71 object.obedit_ref.ptr = ob;
72 object.attribute_storage.wrap() = pointcloud.attribute_storage.wrap();
73 object.bounds_cache = pointcloud.runtime->bounds_cache;
74 object.bounds_with_radius_cache = pointcloud.runtime->bounds_with_radius_cache;
75 object.totpoint = pointcloud.totpoint;
76 }
77 });
78
79 bmain->is_memfile_undo_flush_needed = true;
80
81 return true;
82}
83
84static void step_decode(
85 bContext *C, Main *bmain, UndoStep *us_p, const eUndoStepDir /*dir*/, bool /*is_final*/)
86{
87 PointCloudUndoStep *us = reinterpret_cast<PointCloudUndoStep *>(us_p);
88 Scene *scene = CTX_data_scene(C);
89 ViewLayer *view_layer = CTX_data_view_layer(C);
90
92 CTX_wm_manager(C), us->scene_ref.ptr, &scene, &view_layer);
94 view_layer,
95 &us->objects.first().obedit_ref.ptr,
96 us->objects.size(),
97 sizeof(decltype(us->objects)::value_type));
98
99 BLI_assert(BKE_object_is_in_editmode(us->objects.first().obedit_ref.ptr));
100
101 for (const StepObject &object : us->objects) {
102 PointCloud &pointcloud = *static_cast<PointCloud *>(object.obedit_ref.ptr->data);
103
104 const bool positions_changed = [&]() {
105 const bke::Attribute *attr_a = pointcloud.attribute_storage.wrap().lookup("position");
106 const bke::Attribute *attr_b = object.attribute_storage.wrap().lookup("position");
107 if (!attr_b && !attr_a) {
108 return false;
109 }
110 if (!attr_a || !attr_b) {
111 return true;
112 }
113 return std::get<bke::Attribute::ArrayData>(attr_a->data()).data !=
114 std::get<bke::Attribute::ArrayData>(attr_b->data()).data;
115 }();
116
117 pointcloud.attribute_storage.wrap() = object.attribute_storage.wrap();
118
119 pointcloud.totpoint = object.totpoint;
120 pointcloud.runtime->bounds_cache = object.bounds_cache;
121 pointcloud.runtime->bounds_with_radius_cache = object.bounds_with_radius_cache;
122 if (positions_changed) {
123 pointcloud.runtime->bvh_cache.tag_dirty();
124 }
126 }
127
129 scene, view_layer, us->objects.first().obedit_ref.ptr, us_p->name, &LOG);
130
131 bmain->is_memfile_undo_flush_needed = true;
132
134}
135
136static void step_free(UndoStep *us_p)
137{
138 PointCloudUndoStep *us = reinterpret_cast<PointCloudUndoStep *>(us_p);
139 us->objects.~Array();
140}
141
142static void foreach_ID_ref(UndoStep *us_p,
143 UndoTypeForEachIDRefFn foreach_ID_ref_fn,
144 void *user_data)
145{
146 PointCloudUndoStep *us = reinterpret_cast<PointCloudUndoStep *>(us_p);
147
148 foreach_ID_ref_fn(user_data, ((UndoRefID *)&us->scene_ref));
149 for (const StepObject &object : us->objects) {
150 foreach_ID_ref_fn(user_data, ((UndoRefID *)&object.obedit_ref));
151 }
152}
153
155
156} // namespace undo
157
172
173} // namespace blender::ed::pointcloud
Scene * CTX_data_scene(const bContext *C)
wmWindowManager * CTX_wm_manager(const bContext *C)
ViewLayer * CTX_data_view_layer(const bContext *C)
General operations, lookup, etc. for blender objects.
bool BKE_object_is_in_editmode(const Object *ob)
General operations for point clouds.
@ UNDOTYPE_FLAG_NEED_CONTEXT_FOR_ENCODE
void(*)(void *user_data, UndoRefID *id_ref) UndoTypeForEachIDRefFn
eUndoStepDir
#define BLI_assert(a)
Definition BLI_assert.h:46
void DEG_id_tag_update(ID *id, unsigned int flags)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
void ED_undo_object_set_active_or_warn(Scene *scene, ViewLayer *view_layer, Object *ob, const char *info, CLG_LogRef *log)
Definition ed_undo.cc:775
void ED_undo_object_editmode_restore_helper(Scene *scene, ViewLayer *view_layer, Object **object_array, uint object_array_len, uint object_array_stride)
Definition ed_undo.cc:811
blender::Vector< Object * > ED_undo_editmode_objects_from_view_layer(const Scene *scene, ViewLayer *view_layer)
Definition ed_undo.cc:856
void ED_undo_object_editmode_validate_scene_from_windows(wmWindowManager *wm, const Scene *scene_ref, Scene **scene_p, ViewLayer **view_layer_p)
Definition ed_undo.cc:794
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:393
#define ND_DATA
Definition WM_types.hh:509
int64_t size() const
const DataVariant & data() const
#define LOG(level)
Definition log.h:97
static void foreach_ID_ref(UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
Definition undo.cc:142
static void step_free(UndoStep *us_p)
Definition undo.cc:136
static bool step_encode(bContext *C, Main *bmain, UndoStep *us_p)
Definition undo.cc:55
static void step_decode(bContext *C, Main *bmain, UndoStep *us_p, const eUndoStepDir, bool)
Definition undo.cc:84
bool editable_pointcloud_in_edit_mode_poll(bContext *C)
Definition operators.cc:73
void undosys_type_register(UndoType *ut)
Definition undo.cc:158
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:93
bool is_memfile_undo_flush_needed
Definition BKE_main.hh:213
char name[64]
void(* step_foreach_ID_ref)(UndoStep *us, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
const char * name
void(* step_free)(UndoStep *us)
bool(* poll)(struct bContext *C)
void(* step_decode)(bContext *C, Main *bmain, UndoStep *us, eUndoStepDir dir, bool is_final)
bool(* step_encode)(bContext *C, Main *bmain, UndoStep *us)
bke::AttributeStorage attribute_storage
Definition undo.cc:41
SharedCache< Bounds< float3 > > bounds_with_radius_cache
Definition undo.cc:45
SharedCache< Bounds< float3 > > bounds_cache
Definition undo.cc:44
void WM_event_add_notifier(const bContext *C, uint type, void *reference)