Blender V5.3
physics_pointcache.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 "BLI_listbase.hh"
15#include "BLI_utildefines.hh"
16
17#include "BKE_context.hh"
18#include "BKE_duplilist.hh"
19#include "BKE_global.hh"
20#include "BKE_layer.hh"
21#include "BKE_library.hh"
22#include "BKE_pointcache.h"
23
24#include "DEG_depsgraph.hh"
25
26#include "ED_particle.hh"
27
28#include "WM_api.hh"
29#include "WM_types.hh"
30
31#include "RNA_access.hh"
32#include "RNA_define.hh"
33#include "RNA_prototypes.hh"
34
35#include "physics_intern.hh"
36
37namespace blender {
38
40{
41 return CTX_data_scene(C) != nullptr;
42}
43
44static bool ptcache_poll(bContext *C)
45{
46 PointerRNA ptr = CTX_data_pointer_get_type(C, "point_cache", RNA_PointCache);
47
48 ID *id = ptr.owner_id;
49 PointCache *point_cache = static_cast<PointCache *>(ptr.data);
50
51 if (id == nullptr || point_cache == nullptr) {
52 return false;
53 }
54
55 if (ID_IS_OVERRIDE_LIBRARY_REAL(id) && (point_cache->flag & PTCACHE_DISK_CACHE) == false) {
57 "Library override data-blocks only support Disk Cache storage");
58 return false;
59 }
60
61 if (!ID_IS_EDITABLE(id) && (point_cache->flag & PTCACHE_DISK_CACHE) == false) {
62 CTX_wm_operator_poll_msg_set(C, "Linked data-blocks do not allow editing caches");
63 return false;
64 }
65
66 return true;
67}
68
70{
71 PointerRNA ptr = CTX_data_pointer_get_type(C, "point_cache", RNA_PointCache);
72
73 ID *id = ptr.owner_id;
74 PointCache *point_cache = static_cast<PointCache *>(ptr.data);
75
76 if (id == nullptr || point_cache == nullptr) {
77 return false;
78 }
79
82 C, "Linked or library override data-blocks do not allow adding or removing caches");
83 return false;
84 }
85
86 return true;
87}
88
97
98static void ptcache_job_free(void *customdata)
99{
100 PointCacheJob *job = static_cast<PointCacheJob *>(customdata);
101 MEM_delete(job->baker);
102 MEM_delete(job);
103}
104
105static int ptcache_job_break(void *customdata)
106{
107 PointCacheJob *job = static_cast<PointCacheJob *>(customdata);
108
109 if (G.is_break) {
110 return 1;
111 }
112
113 if (job->stop && *(job->stop)) {
114 return 1;
115 }
116
117 return 0;
118}
119
120static void ptcache_job_update(void *customdata, float progress, int *cancel)
121{
122 PointCacheJob *job = static_cast<PointCacheJob *>(customdata);
123
124 if (ptcache_job_break(job)) {
125 *cancel = 1;
126 }
127
128 *(job->do_update) = true;
129 *(job->progress) = progress;
130}
131
132static void ptcache_job_startjob(void *customdata, wmJobWorkerStatus *worker_status)
133{
134 PointCacheJob *job = static_cast<PointCacheJob *>(customdata);
135
136 job->stop = &worker_status->stop;
137 job->do_update = &worker_status->do_update;
138 job->progress = &worker_status->progress;
139
140 G.is_break = false;
141
142 /* XXX annoying hack: needed to prevent data corruption when changing
143 * scene frame in separate threads
144 */
145 WM_locked_interface_set(job->wm, true);
146
148
149 worker_status->do_update = true;
150 worker_status->stop = false;
151}
152
153static void ptcache_job_endjob(void *customdata)
154{
155 PointCacheJob *job = static_cast<PointCacheJob *>(customdata);
156 Scene *scene = job->baker->scene;
157
158 WM_locked_interface_set(job->wm, false);
159
162}
163
164static void ptcache_free_bake(PointCache *cache)
165{
166 if (cache->edit) {
167 if (!cache->edit->edited || true) { // XXX okee("Lose changes done in particle mode?")) {
169 cache->edit = nullptr;
170 cache->flag &= ~PTCACHE_BAKED;
171 }
172 }
173 else {
174 cache->flag &= ~PTCACHE_BAKED;
175 }
176}
177
179{
180 PTCacheBaker *baker = MEM_new_zeroed<PTCacheBaker>("PTCacheBaker");
181
182 baker->bmain = CTX_data_main(C);
183 baker->scene = CTX_data_scene(C);
185 /* Depsgraph is used to sweep the frame range and evaluate scene at different times. */
187 baker->bake = RNA_boolean_get(op->ptr, "bake");
188 baker->render = false;
189 baker->anim_init = false;
190 baker->quick_step = 1;
191
192 if (!all) {
193 PointerRNA ptr = CTX_data_pointer_get_type(C, "point_cache", RNA_PointCache);
194 ID *id = ptr.owner_id;
195 Object *ob = (GS(id->name) == ID_OB) ? id_cast<Object *>(id) : nullptr;
196 PointCache *cache = static_cast<PointCache *>(ptr.data);
197 baker->pid = BKE_ptcache_id_find(ob, baker->scene, cache);
198 }
199
200 return baker;
201}
202
204{
205 bool all = STREQ(op->type->idname, "PTCACHE_OT_bake_all");
206
207 PTCacheBaker *baker = ptcache_baker_create(C, op, all);
208 BKE_ptcache_bake(baker);
209 MEM_delete(baker);
210
211 return OPERATOR_FINISHED;
212}
213
215{
216 bool all = STREQ(op->type->idname, "PTCACHE_OT_bake_all");
217
219 job->wm = CTX_wm_manager(C);
220 job->baker = ptcache_baker_create(C, op, all);
221 job->baker->bake_job = job;
223
227 "Baking point cache...",
230
234
236
238
240
241 /* we must run modal until the bake job is done, otherwise the undo push
242 * happens before the job ends, which can lead to race conditions between
243 * the baking and file writing code */
245}
246
248{
249 Scene *scene = static_cast<Scene *>(op->customdata);
250
251 /* no running blender, remove handler and pass through */
254 }
255
257}
258
260{
262 Scene *scene = static_cast<Scene *>(op->customdata);
263
264 /* kill on cancel, because job is using op->reports */
266}
267
269{
270 Scene *scene = CTX_data_scene(C);
271 ListBaseT<PTCacheID> pidlist;
272
273 FOREACH_SCENE_OBJECT_BEGIN (scene, ob) {
274 BKE_ptcache_ids_from_object(&pidlist, ob, scene, MAX_DUPLI_RECUR);
275
276 for (PTCacheID &pid : pidlist) {
277 ptcache_free_bake(pid.cache);
278 }
279
280 pidlist.free_no_destruct();
281
283 }
285
287
288 return OPERATOR_FINISHED;
289}
290
292{
293 /* identifiers */
294 ot->name = "Bake All Physics";
295 ot->description = "Bake all physics simulations in the current scene";
296 ot->idname = "PTCACHE_OT_bake_all";
297
298 /* API callbacks. */
304
305 /* flags */
307
308 RNA_def_boolean(ot->srna, "bake", true, "Bake", "");
309}
311{
312 /* identifiers */
313 ot->name = "Delete All Physics Bakes";
314 ot->idname = "PTCACHE_OT_free_bake_all";
315 ot->description = "Delete all baked caches of all objects in the current scene";
316
317 /* API callbacks. */
320
321 /* flags */
323}
324
326{
327 PointerRNA ptr = CTX_data_pointer_get_type(C, "point_cache", RNA_PointCache);
328 PointCache *cache = static_cast<PointCache *>(ptr.data);
329 Object *ob = id_cast<Object *>(ptr.owner_id);
330
331 ptcache_free_bake(cache);
332
334
335 return OPERATOR_FINISHED;
336}
338{
339 PointerRNA ptr = CTX_data_pointer_get_type(C, "point_cache", RNA_PointCache);
340 PointCache *cache = static_cast<PointCache *>(ptr.data);
341 Object *ob = id_cast<Object *>(ptr.owner_id);
342
343 cache->flag |= PTCACHE_BAKED;
344
346
347 return OPERATOR_FINISHED;
348}
350{
351 /* identifiers */
352 ot->name = "Bake Physics";
353 ot->description = "Bake physics";
354 ot->idname = "PTCACHE_OT_bake";
355
356 /* API callbacks. */
361 ot->poll = ptcache_poll;
362
363 /* flags */
365
366 RNA_def_boolean(ot->srna, "bake", false, "Bake", "");
367}
369{
370 /* identifiers */
371 ot->name = "Delete Physics Bake";
372 ot->description = "Delete physics bake";
373 ot->idname = "PTCACHE_OT_free_bake";
374
375 /* API callbacks. */
377 ot->poll = ptcache_poll;
378
379 /* flags */
381}
383{
384 /* identifiers */
385 ot->name = "Bake from Cache";
386 ot->description = "Bake from cache";
387 ot->idname = "PTCACHE_OT_bake_from_cache";
388
389 /* API callbacks. */
391 ot->poll = ptcache_poll;
392
393 /* flags */
395}
396
398{
399 Scene *scene = CTX_data_scene(C);
400 PointerRNA ptr = CTX_data_pointer_get_type(C, "point_cache", RNA_PointCache);
401 Object *ob = id_cast<Object *>(ptr.owner_id);
402 PointCache *cache = static_cast<PointCache *>(ptr.data);
403 PTCacheID pid = BKE_ptcache_id_find(ob, scene, cache);
404
405 if (pid.cache) {
406 PointCache *cache_new = BKE_ptcache_add(pid.ptcaches);
407 cache_new->step = pid.default_step;
408 *(pid.cache_ptr) = cache_new;
409
413 }
414
415 return OPERATOR_FINISHED;
416}
418{
419 PointerRNA ptr = CTX_data_pointer_get_type(C, "point_cache", RNA_PointCache);
420 Scene *scene = CTX_data_scene(C);
421 Object *ob = id_cast<Object *>(ptr.owner_id);
422 PointCache *cache = static_cast<PointCache *>(ptr.data);
423 PTCacheID pid = BKE_ptcache_id_find(ob, scene, cache);
424
425 /* don't delete last cache */
426 if (pid.cache && pid.ptcaches->first() != pid.ptcaches->last()) {
427 BLI_remlink(pid.ptcaches, pid.cache);
429 *(pid.cache_ptr) = pid.ptcaches->first();
430
433 }
434
435 return OPERATOR_FINISHED;
436}
438{
439 /* identifiers */
440 ot->name = "Add New Cache";
441 ot->description = "Add new cache";
442 ot->idname = "PTCACHE_OT_add";
443
444 /* API callbacks. */
447
448 /* flags */
450}
452{
453 /* identifiers */
454 ot->name = "Delete Current Cache";
455 ot->description = "Delete current cache";
456 ot->idname = "PTCACHE_OT_remove";
457
458 /* API callbacks. */
461
462 /* flags */
464}
465
466} // namespace blender
#define FOREACH_SCENE_OBJECT_END
#define FOREACH_SCENE_OBJECT_BEGIN(scene, _instance)
#define ID_IS_OVERRIDE_LIBRARY_REAL(_id)
Definition DNA_ID.h:729
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NC_SCENE
Definition WM_types.hh:391
#define ND_FRAME
Definition WM_types.hh:453
#define ND_POINTCACHE
Definition WM_types.hh:507
#define NC_OBJECT
Definition WM_types.hh:392
#define STREQ(a, b)
bool all(VecOp< bool, D >) RET
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
static bool ptcache_add_remove_poll(bContext *C)
void WM_jobs_callbacks(wmJob *wm_job, wm_jobs_start_callback startjob, void(*initjob)(void *), void(*update)(void *), void(*endjob)(void *))
Definition wm_jobs.cc:407
static wmOperatorStatus ptcache_bake_invoke(bContext *C, wmOperator *op, const wmEvent *)
static void ptcache_free_bake(PointCache *cache)
static wmOperatorStatus ptcache_add_new_exec(bContext *C, wmOperator *)
Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
static int ptcache_job_break(void *customdata)
@ WM_JOB_TYPE_POINTCACHE
Definition WM_api.hh:2034
static wmOperatorStatus ptcache_free_bake_exec(bContext *C, wmOperator *)
void PTCACHE_OT_bake(wmOperatorType *ot)
wmWindow * CTX_wm_window(const bContext *C)
void WM_jobs_customdata_set(wmJob *wm_job, void *customdata, void(*free)(void *customdata))
Definition wm_jobs.cc:377
@ WM_JOB_PROGRESS
Definition WM_api.hh:1997
ViewLayer * CTX_data_view_layer(const bContext *C)
void WM_jobs_start(wmWindowManager *wm, wmJob *wm_job)
Definition wm_jobs.cc:498
void BKE_ptcache_free(struct PointCache *cache)
Global G
Definition blender.cc:57
void PTCACHE_OT_bake_all(wmOperatorType *ot)
void WM_locked_interface_set(wmWindowManager *wm, bool lock)
void PTCACHE_OT_remove(wmOperatorType *ot)
void CTX_wm_operator_poll_msg_set(bContext *C, const char *msg)
void PTCACHE_OT_add(wmOperatorType *ot)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
static bool ptcache_poll(bContext *C)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void DEG_id_tag_update(ID *id, unsigned int flags)
bool WM_jobs_has_running(const wmWindowManager *window_manager, const void *owner, const eWM_JobType type, const eWM_JobFlag exclude_flags)
Definition wm_jobs.cc:809
wmWindowManager * CTX_wm_manager(const bContext *C)
void PTCACHE_OT_free_bake_all(wmOperatorType *ot)
static void ptcache_job_startjob(void *customdata, wmJobWorkerStatus *worker_status)
static wmOperatorStatus ptcache_bake_from_cache_exec(bContext *C, wmOperator *)
void BKE_ptcache_bake(struct PTCacheBaker *baker)
void BKE_ptcache_ids_from_object(ListBaseT< PTCacheID > *lb, struct Object *ob, struct Scene *scene, int duplis)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
static void ptcache_job_free(void *customdata)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
static void ptcache_bake_cancel(bContext *C, wmOperator *op)
static wmOperatorStatus ptcache_free_bake_all_exec(bContext *C, wmOperator *)
void PTCACHE_OT_bake_from_cache(wmOperatorType *ot)
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
constexpr int MAX_DUPLI_RECUR
void WM_jobs_kill_type(wmWindowManager *wm, const void *owner, int job_type)
Definition wm_jobs.cc:642
void PTCACHE_OT_free_bake(wmOperatorType *ot)
Main * CTX_data_main(const bContext *C)
wmJob * WM_jobs_get(wmWindowManager *wm, wmWindow *win, const void *owner, const char *name, const eWM_JobFlag flag, const eWM_JobType job_type)
Definition wm_jobs.cc:228
void PE_free_ptcache_edit(PTCacheEdit *edit)
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ ID_RECALC_POINT_CACHE
Definition DNA_ID.h:1105
ID_Type GS(const char *name)
Definition DNA_ID.h:410
struct PointCache * BKE_ptcache_add(ListBaseT< PointCache > *ptcaches)
PTCacheID BKE_ptcache_id_find(struct Object *ob, struct Scene *scene, struct PointCache *cache)
Scene * CTX_data_scene(const bContext *C)
static void ptcache_job_endjob(void *customdata)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
static wmOperatorStatus ptcache_bake_exec(bContext *C, wmOperator *op)
void WM_jobs_timer(wmJob *wm_job, double time_step, uint note, uint endnote, void(*timer_step)(void *))
Definition wm_jobs.cc:393
static bool ptcache_bake_all_poll(bContext *C)
static void ptcache_job_update(void *customdata, float progress, int *cancel)
PointerRNA CTX_data_pointer_get_type(const bContext *C, const char *member, StructRNA *type)
static PTCacheBaker * ptcache_baker_create(bContext *C, wmOperator *op, bool all)
static wmOperatorStatus ptcache_remove_exec(bContext *C, wmOperator *)
static wmOperatorStatus ptcache_bake_modal(bContext *C, wmOperator *op, const wmEvent *)
struct PTCacheID pid
struct Depsgraph * depsgraph
void(*) update_progress(void *data, float progress, int *cancel)
struct ViewLayer * view_layer
struct PointCache * cache
unsigned int default_step
struct PointCache ** cache_ptr
ListBaseT< PointCache > * ptcaches
struct PTCacheEdit * edit
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
void(*) cancel(bContext *C, wmOperator *op)
Definition WM_types.hh:1149
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
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157
struct wmOperatorType * type