Blender V5.3
particle_edit_undo.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2007 by Janne Karhu. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdlib>
10#include <cstring>
11
12#include "MEM_guardedalloc.h"
13
14#include "CLG_log.h"
15
16#include "DNA_scene_types.h"
18
19#include "BLI_listbase.hh"
20#include "BLI_utildefines.hh"
21
22#include "BKE_context.hh"
23#include "BKE_layer.hh"
24#include "BKE_particle.h"
25#include "BKE_pointcache.h"
26#include "BKE_undo_system.hh"
27
28#include "DEG_depsgraph.hh"
29
30#include "ED_object.hh"
31#include "ED_particle.hh"
32#include "ED_physics.hh"
33#include "ED_undo.hh"
34
36
37namespace blender {
38
40static CLG_LogRef LOG = {"undo.particle"};
41
42/* -------------------------------------------------------------------- */
45
47{
48 PTCacheEditPoint *point;
49
50 size_t mem_used_prev = MEM_get_memory_in_use();
51
52 undo->totpoint = edit->totpoint;
53
54 if (edit->psys) {
55 ParticleData *pa;
56
57 pa = undo->particles = MEM_dupalloc(edit->psys->particles);
58
59 for (int i = 0; i < edit->totpoint; i++, pa++) {
60 pa->hair = MEM_dupalloc(pa->hair);
61 }
62
63 undo->psys_flag = edit->psys->flag;
64 }
65 else {
66 PTCacheMem *pm;
67
68 BLI_duplicatelist(&undo->mem_cache, &edit->pid.cache->mem_cache);
69 pm = undo->mem_cache.first();
70
71 for (; pm; pm = pm->next) {
72 for (int i = 0; i < BPHYS_TOT_DATA; i++) {
73 pm->data[i] = MEM_dupalloc_void(pm->data[i]);
74 }
75 }
76 }
77
78 point = undo->points = MEM_dupalloc(edit->points);
79 undo->totpoint = edit->totpoint;
80
81 for (int i = 0; i < edit->totpoint; i++, point++) {
82 point->keys = MEM_dupalloc(point->keys);
83 /* no need to update edit key->co & key->time pointers here */
84 }
85
86 size_t mem_used_curr = MEM_get_memory_in_use();
87
88 undo->undo_size = mem_used_prev < mem_used_curr ? mem_used_curr - mem_used_prev :
89 sizeof(PTCacheUndo);
90}
91
93{
94 ParticleSystem *psys = edit->psys;
95 ParticleData *pa;
96 HairKey *hkey;
97 POINT_P;
98 KEY_K;
99
101 if (psys && psys->particles[p].hair) {
102 MEM_delete(psys->particles[p].hair);
103 }
104
105 if (point->keys) {
106 MEM_delete(point->keys);
107 }
108 }
109 if (psys && psys->particles) {
110 MEM_delete(psys->particles);
111 }
112 if (edit->points) {
113 MEM_delete(edit->points);
114 }
115 MEM_SAFE_DELETE(edit->mirror_cache);
116
117 edit->points = MEM_dupalloc(undo->points);
118 edit->totpoint = undo->totpoint;
119
121 point->keys = MEM_dupalloc(point->keys);
122 }
123
124 if (psys) {
125 psys->particles = MEM_dupalloc(undo->particles);
126
127 psys->totpart = undo->totpoint;
128
130 pa = psys->particles + p;
131 hkey = pa->hair = MEM_dupalloc(pa->hair);
132
133 LOOP_KEYS {
134 key->co = hkey->co;
135 key->time = &hkey->time;
136 hkey++;
137 }
138 }
139
140 psys->flag = undo->psys_flag;
141 }
142 else {
143 PTCacheMem *pm;
144 int i;
145
147
148 BLI_duplicatelist(&edit->pid.cache->mem_cache, &undo->mem_cache);
149
150 pm = edit->pid.cache->mem_cache.first();
151
152 for (; pm; pm = pm->next) {
153 for (i = 0; i < BPHYS_TOT_DATA; i++) {
154 pm->data[i] = MEM_dupalloc_void(pm->data[i]);
155 }
156 void *cur[BPHYS_TOT_DATA];
158
160 LOOP_KEYS {
161 if (int(key->ftime) == int(pm->frame)) {
162 key->co = static_cast<float *>(cur[BPHYS_DATA_LOCATION]);
163 key->vel = static_cast<float *>(cur[BPHYS_DATA_VELOCITY]);
164 key->rot = static_cast<float *>(cur[BPHYS_DATA_ROTATION]);
165 key->time = &key->ftime;
166 }
167 }
169 }
170 }
171 }
172}
173
175{
176 PTCacheEditPoint *point;
177 int i;
178
179 for (i = 0, point = undo->points; i < undo->totpoint; i++, point++) {
180 if (undo->particles && (undo->particles + i)->hair) {
181 MEM_delete((undo->particles + i)->hair);
182 }
183 if (point->keys) {
184 MEM_delete(point->keys);
185 }
186 }
187 if (undo->points) {
188 MEM_delete(undo->points);
189 }
190 if (undo->particles) {
191 MEM_delete(undo->particles);
192 }
193 BKE_ptcache_free_mem(&undo->mem_cache);
194}
195
197
198/* -------------------------------------------------------------------- */
201
205 UndoRefID_Scene scene_ref;
206 UndoRefID_Object object_ref;
208};
209
211{
212 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
213 const Main *bmain = CTX_data_main(C);
214 Scene *scene = CTX_data_scene(C);
215 ViewLayer *view_layer = CTX_data_view_layer(C);
216 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
218 PTCacheEdit *edit = PE_get_current(depsgraph, scene, ob);
219
220 return (edit != nullptr);
221}
222
224{
225 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
226 ParticleUndoStep *us = reinterpret_cast<ParticleUndoStep *>(us_p);
227 ViewLayer *view_layer = CTX_data_view_layer(C);
228 us->scene_ref.ptr = CTX_data_scene(C);
229 BKE_view_layer_synced_ensure(*bmain, us->scene_ref.ptr, view_layer);
230 us->object_ref.ptr = BKE_view_layer_active_object_get(view_layer);
231 PTCacheEdit *edit = PE_get_current(depsgraph, us->scene_ref.ptr, us->object_ref.ptr);
233 return true;
234}
235
237 bContext *C, Main *bmain, UndoStep *us_p, const eUndoStepDir /*dir*/, bool /*is_final*/)
238{
239 Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
240
241 ParticleUndoStep *us = reinterpret_cast<ParticleUndoStep *>(us_p);
242 Scene *scene = us->scene_ref.ptr;
243 ViewLayer *view_layer = CTX_data_view_layer(C);
244
245 /* Only to correct the `view_layer` which might not match the scene
246 * (in the case of undoing with multiple windows). */
248 CTX_wm_manager(C), us->scene_ref.ptr, &scene, &view_layer);
249
250 Object *ob = us->object_ref.ptr;
251 if (ob->mode != OB_MODE_PARTICLE_EDIT) {
252 /* Exit the current mode before restoring Particle Edit to clean up its runtime data and avoid
253 * combining incompatible mode bits. */
254 ed::object::mode_generic_exit(bmain, depsgraph, scene, ob);
255 }
256 ED_object_particle_edit_mode_enter_ex(depsgraph, scene, ob);
257
258 PTCacheEdit *edit = PE_get_current(depsgraph, scene, ob);
259
260 /* While this shouldn't happen, entering particle edit-mode uses a more complex
261 * setup compared to most other modes which we can't ensure succeeds. */
262 if (edit == nullptr) [[unlikely]] {
263 BLI_assert(0);
264 return;
265 }
266
267 undoptcache_to_editcache(&us->data, edit);
269 if ((pset->flag & PE_DRAW_PART) != 0) {
270 psys_free_path_cache(nullptr, edit);
272 }
274
275 ED_undo_object_set_active_or_warn(*bmain, scene, view_layer, ob, us_p->name, &LOG);
276
277 /* Check after setting active (unless undoing into another scene). */
279}
280
282{
283 ParticleUndoStep *us = reinterpret_cast<ParticleUndoStep *>(us_p);
285}
286
288 UndoTypeForEachIDRefFn foreach_ID_ref_fn,
289 void *user_data)
290{
291 ParticleUndoStep *us = reinterpret_cast<ParticleUndoStep *>(us_p);
292 foreach_ID_ref_fn(user_data, (reinterpret_cast<UndoRefID *>(&us->scene_ref)));
293 foreach_ID_ref_fn(user_data, (reinterpret_cast<UndoRefID *>(&us->object_ref)));
294}
295
310
312
313} // namespace blender
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
void mode_generic_exit(Main *bmain, Depsgraph *depsgraph, Scene *scene, Object *ob)
BMesh Iterators.
void psys_free_path_cache(struct ParticleSystem *psys, struct PTCacheEdit *edit)
Definition particle.cc:938
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
Object * BKE_view_layer_active_object_get(const ViewLayer *view_layer)
void BKE_particle_batch_cache_dirty_tag(struct ParticleSystem *psys, int mode)
Definition particle.cc:5580
@ UNDOTYPE_FLAG_NEED_CONTEXT_FOR_ENCODE
@ UNDOTYPE_FLAG_ENCODE_PRE_MEMFILE_SUPPORTED
void void void void void void BLI_duplicatelist(ListBase *dst, const ListBase *src) ATTR_NONNULL(1
Definition listbase.cc:834
Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
void ED_undo_object_set_active_or_warn(const Main &bmain, Scene *scene, ViewLayer *view_layer, Object *ob, const char *info, CLG_LogRef *log)
Definition ed_undo.cc:1024
static bool particle_undosys_step_encode(bContext *C, Main *bmain, UndoStep *us_p)
ViewLayer * CTX_data_view_layer(const bContext *C)
size_t(*) MEM_get_memory_in_use()
void BKE_ptcache_mem_pointers_incr(void *cur[BPHYS_TOT_DATA])
void * MEM_dupalloc_void(const void *vmemh) ATTR_WARN_UNUSED_RESULT
PTCacheEdit * PE_get_current(Depsgraph *depsgraph, Scene *scene, Object *ob)
void DEG_id_tag_update(ID *id, unsigned int flags)
@ OB_MODE_PARTICLE_EDIT
wmWindowManager * CTX_wm_manager(const bContext *C)
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
static bool particle_undosys_poll(bContext *C)
static void undoptcache_free_data(PTCacheUndo *undo)
void BKE_ptcache_free_mem(ListBaseT< PTCacheMem > *mem_cache)
static void particle_undosys_foreach_ID_ref(UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
Main * CTX_data_main(const bContext *C)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
static CLG_LogRef LOG
Definition logging.cpp:12
void(*)(void *user_data, UndoRefID *id_ref) UndoTypeForEachIDRefFn
Scene * CTX_data_scene(const bContext *C)
static void particle_undosys_step_free(UndoStep *us_p)
static void undoptcache_to_editcache(PTCacheUndo *undo, PTCacheEdit *edit)
static void particle_undosys_step_decode(bContext *C, Main *bmain, UndoStep *us_p, const eUndoStepDir, bool)
static void undoptcache_from_editcache(PTCacheUndo *undo, PTCacheEdit *edit)
void ED_object_particle_edit_mode_enter_ex(Depsgraph *depsgraph, Scene *scene, Object *ob)
void BKE_ptcache_mem_pointers_init(struct PTCacheMem *pm, void *cur[BPHYS_TOT_DATA])
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:1047
@ BKE_PARTICLE_BATCH_DIRTY_ALL
void ED_particle_undosys_type(UndoType *ut)
#define POINT_P
#define LOOP_KEYS
#define KEY_K
#define LOOP_POINTS
struct PTCacheEditKey * keys
struct PTCacheID pid
PTCacheEditPoint * points
struct ParticleSystem * psys
struct PointCache * cache
struct PTCacheMem * next
eParticleSystem_Flag flag
ListBaseT< PTCacheMem > mem_cache
struct ToolSettings * toolsettings
struct ParticleEditSettings particle
bool(*) poll(bContext *C)
void(*) step_free(UndoStep *us)
void(*) step_decode(bContext *C, Main *bmain, UndoStep *us, eUndoStepDir dir, bool is_final)
void(*) step_foreach_ID_ref(UndoStep *us, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
bool(*) step_encode(bContext *C, Main *bmain, UndoStep *us)
const char * identifier