Blender V5.3
dynamicpaint_ops.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstring>
10
11#include "MEM_guardedalloc.h"
12
13#include "BLI_path_utils.hh"
14#include "BLI_string_utf8.hh"
15#include "BLI_time.hh"
16
17#include "BLT_translation.hh"
18
20#include "DNA_mesh_types.h"
21#include "DNA_modifier_types.h"
22#include "DNA_object_types.h"
23#include "DNA_scene_types.h"
24
25#include "BKE_attribute.h"
26#include "BKE_attribute.hh"
27#include "BKE_context.hh"
28#include "BKE_deform.hh"
29#include "BKE_dynamicpaint.h"
30#include "BKE_global.hh"
31#include "BKE_main.hh"
32#include "BKE_modifier.hh"
33#include "BKE_object_deform.h"
34#include "BKE_report.hh"
35#include "BKE_screen.hh"
36
37#include "DEG_depsgraph.hh"
40
41#include "ED_mesh.hh"
42#include "ED_object.hh"
43#include "ED_screen.hh"
44
45#include "RNA_access.hh"
46#include "RNA_define.hh"
47#include "RNA_enum_types.hh"
48
49#include "WM_api.hh"
50#include "WM_types.hh"
51
52#include "physics_intern.hh" /* own include */
53
54namespace blender {
55
57{
58 DynamicPaintModifierData *pmd = nullptr;
61 DynamicPaintSurface *surface;
62
63 /* Make sure we're dealing with a canvas */
64 pmd = reinterpret_cast<DynamicPaintModifierData *>(
66 if (!pmd || !pmd->canvas) {
67 return OPERATOR_CANCELLED;
68 }
69
70 canvas = pmd->canvas;
72
73 if (!surface) {
74 return OPERATOR_CANCELLED;
75 }
76
77 canvas->active_sur = 0;
78 for (surface = surface->prev; surface; surface = surface->prev) {
79 canvas->active_sur++;
80 }
81
82 return OPERATOR_FINISHED;
83}
84
86{
87 /* identifiers */
88 ot->name = "Add Surface Slot";
89 ot->idname = "DPAINT_OT_surface_slot_add";
90 ot->description = "Add a new Dynamic Paint surface slot";
91
92 /* API callbacks. */
95
96 /* flags */
98}
99
101{
102 DynamicPaintModifierData *pmd = nullptr;
105 DynamicPaintSurface *surface;
106 int id = 0;
107
108 /* Make sure we're dealing with a canvas */
109 pmd = reinterpret_cast<DynamicPaintModifierData *>(
111 if (!pmd || !pmd->canvas) {
112 return OPERATOR_CANCELLED;
113 }
114
115 canvas = pmd->canvas;
116 surface = canvas->surfaces.first();
117
118 /* find active surface and remove it */
119 for (; surface; surface = surface->next) {
120 if (id == canvas->active_sur) {
121 canvas->active_sur -= 1;
122 dynamicPaint_freeSurface(pmd, surface);
123 break;
124 }
125 id++;
126 }
127
130
131 return OPERATOR_FINISHED;
132}
133
135{
136 /* identifiers */
137 ot->name = "Remove Surface Slot";
138 ot->idname = "DPAINT_OT_surface_slot_remove";
139 ot->description = "Remove the selected surface slot";
140
141 /* API callbacks. */
144
145 /* flags */
147}
148
150{
151
153 Scene *scene = CTX_data_scene(C);
154 DynamicPaintModifierData *pmd = reinterpret_cast<DynamicPaintModifierData *>(
156 int type = RNA_enum_get(op->ptr, "type");
157
158 if (!pmd) {
159 return OPERATOR_CANCELLED;
160 }
161
162 /* if type is already enabled, toggle it off */
163 if (type == MOD_DYNAMICPAINT_TYPE_CANVAS && pmd->canvas) {
165 }
166 else if (type == MOD_DYNAMICPAINT_TYPE_BRUSH && pmd->brush) {
168 }
169 /* else create a new type */
170 else {
171 if (!dynamicPaint_createType(pmd, type, scene)) {
172 return OPERATOR_CANCELLED;
173 }
174 }
175
176 /* update dependency */
180
181 return OPERATOR_FINISHED;
182}
183
185{
186 PropertyRNA *prop;
187
188 /* identifiers */
189 ot->name = "Toggle Type Active";
190 ot->idname = "DPAINT_OT_type_toggle";
191 ot->description = "Toggle whether given type is active or not";
192
193 /* API callbacks. */
196
197 /* flags */
199
200 /* properties */
201 prop = RNA_def_enum(ot->srna,
202 "type",
205 "Type",
206 "");
208 ot->prop = prop;
209}
210
212{
214 DynamicPaintSurface *surface;
215 DynamicPaintModifierData *pmd = reinterpret_cast<DynamicPaintModifierData *>(
217 int output = RNA_enum_get(op->ptr, "output"); /* currently only 1/0 */
218
219 if (!pmd || !pmd->canvas) {
220 return OPERATOR_CANCELLED;
221 }
222 surface = get_activeSurface(pmd->canvas);
223
224 /* if type is already enabled, toggle it off */
225 if (surface->format == MOD_DPAINT_SURFACE_F_VERTEX) {
226 bool exists = dynamicPaint_outputLayerExists(surface, ob, output);
227 const char *name;
228
229 if (output == 0) {
230 name = surface->output_name;
231 }
232 else {
233 name = surface->output_name2;
234 }
235
236 /* Vertex Color Layer */
237 if (surface->type == MOD_DPAINT_SURFACE_T_PAINT) {
238 if (!exists) {
239 ED_mesh_color_add(id_cast<Mesh *>(ob->data), name, true, true, op->reports);
240 }
241 else {
243 BKE_attribute_remove(owner, name, nullptr);
244 }
245 }
246 /* Vertex Weight Layer */
247 else if (surface->type == MOD_DPAINT_SURFACE_T_WEIGHT) {
248 if (!exists) {
251 }
252 else {
253 bDeformGroup *defgroup = BKE_object_defgroup_find_name(ob, name);
254 if (defgroup) {
255 BKE_object_defgroup_remove(ob, defgroup);
257 }
258 }
259 }
260 }
261
262 return OPERATOR_FINISHED;
263}
264
266{
267 static const EnumPropertyItem prop_output_toggle_types[] = {
268 {0, "A", 0, "Output A", ""},
269 {1, "B", 0, "Output B", ""},
270 {0, nullptr, 0, nullptr, nullptr},
271 };
272
273 /* identifiers */
274 ot->name = "Toggle Output Layer";
275 ot->idname = "DPAINT_OT_output_toggle";
276 ot->description = "Add or remove Dynamic Paint output data layer";
277
278 /* API callbacks. */
281
282 /* flags */
284
285 /* properties */
286 ot->prop = RNA_def_enum(ot->srna, "output", prop_output_toggle_types, 0, "Output Toggle", "");
287}
288
289/***************************** Image Sequence Baking ******************************/
290
308
309static void dpaint_bake_free(void *customdata)
310{
311 DynamicPaintBakeJob *job = static_cast<DynamicPaintBakeJob *>(customdata);
312 MEM_delete(job);
313}
314
315static void dpaint_bake_endjob(void *customdata)
316{
317 DynamicPaintBakeJob *job = static_cast<DynamicPaintBakeJob *>(customdata);
319
320 canvas->flags &= ~MOD_DPAINT_BAKING;
321
323
324 G.is_rendering = false;
325
326 WM_locked_interface_set(G_MAIN->wm.first(), false);
327
328 /* Bake was successful:
329 * Report for ended bake and how long it took */
330 if (job->success) {
331 /* Show bake info */
333 RPT_INFO, "DynamicPaint: Bake complete! (%.2f)", BLI_time_now_seconds() - job->start);
334 }
335 else {
336 if (strlen(canvas->error)) { /* If an error occurred */
337 WM_global_reportf(RPT_ERROR, "DynamicPaint: Bake failed: %s", canvas->error);
338 }
339 else { /* User canceled the bake */
340 WM_global_report(RPT_WARNING, "Baking canceled!");
341 }
342 }
343}
344
345/*
346 * Do actual bake operation. Loop through to-be-baked frames.
347 * Returns 0 on failure.
348 */
350{
351 DynamicPaintSurface *surface = job->surface;
352 Object *cObject = job->ob;
354 Scene *input_scene = DEG_get_input_scene(job->depsgraph);
355 Scene *scene = job->scene;
356 int frame = 1, orig_frame;
357 int frames;
358
359 frames = surface->end_frame - surface->start_frame + 1;
360 if (frames <= 0) {
361 STRNCPY_UTF8(canvas->error, N_("No frames to bake"));
362 return;
363 }
364
365 /* Show progress bar. */
366 *(job->do_update) = true;
367
368 /* Set frame to start point (also initializes modifier data). */
369 frame = surface->start_frame;
370 orig_frame = input_scene->r.cfra;
371 input_scene->r.cfra = frame;
373
374 /* Init surface */
375 if (!dynamicPaint_createUVSurface(scene, surface, job->progress, job->do_update)) {
376 job->success = 0;
377 return;
378 }
379
380 /* Loop through selected frames */
381 for (frame = surface->start_frame; frame <= surface->end_frame; frame++) {
382 /* The first 10% are for createUVSurface... */
383 const float progress = 0.1f + 0.9f * (frame - surface->start_frame) / float(frames);
384 surface->current_frame = frame;
385
386 /* If user requested stop, quit baking */
387 if (G.is_break) {
388 job->success = 0;
389 return;
390 }
391
392 /* Update progress bar */
393 *(job->do_update) = true;
394 *(job->progress) = progress;
395
396 /* calculate a frame */
397 input_scene->r.cfra = frame;
399 if (!dynamicPaint_calculateFrame(surface, job->depsgraph, scene, cObject, frame)) {
400 job->success = 0;
401 return;
402 }
403
404 /*
405 * Save output images
406 */
407 {
408 char filepath[FILE_MAX];
409
410 /* primary output layer */
411 if (surface->flags & MOD_DPAINT_OUT1) {
412 /* set filepath */
414 filepath, sizeof(filepath), surface->image_output_path, surface->output_name);
415 BLI_path_frame(filepath, sizeof(filepath), frame, 4);
416
417 /* save image */
418 dynamicPaint_outputSurfaceImage(surface, filepath, 0);
419 }
420 /* secondary output */
421 if (surface->flags & MOD_DPAINT_OUT2 && surface->type == MOD_DPAINT_SURFACE_T_PAINT) {
422 /* set filepath */
424 filepath, sizeof(filepath), surface->image_output_path, surface->output_name2);
425 BLI_path_frame(filepath, sizeof(filepath), frame, 4);
426
427 /* save image */
428 dynamicPaint_outputSurfaceImage(surface, filepath, 1);
429 }
430 }
431 }
432
433 input_scene->r.cfra = orig_frame;
435}
436
437static void dpaint_bake_startjob(void *customdata, wmJobWorkerStatus *worker_status)
438{
439 DynamicPaintBakeJob *job = static_cast<DynamicPaintBakeJob *>(customdata);
440
441 job->stop = &worker_status->stop;
442 job->do_update = &worker_status->do_update;
443 job->progress = &worker_status->progress;
445 job->success = 1;
446
447 G.is_break = false;
448
449 /* XXX annoying hack: needed to prevent data corruption when changing
450 * scene frame in separate threads
451 */
452 G.is_rendering = true;
454
456
457 worker_status->do_update = true;
458 worker_status->stop = false;
459}
460
461/*
462 * Bake Dynamic Paint image sequence surface
463 */
465{
466 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
468 Object *object_eval = DEG_get_evaluated(depsgraph, ob_);
469 Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
470
471 DynamicPaintSurface *surface;
472
473 /*
474 * Get modifier data
475 */
476 DynamicPaintModifierData *pmd = reinterpret_cast<DynamicPaintModifierData *>(
478 if (pmd == nullptr) {
479 BKE_report(op->reports, RPT_ERROR, "Bake failed: no Dynamic Paint modifier found");
480 return OPERATOR_CANCELLED;
481 }
482
483 /* Make sure we're dealing with a canvas */
485 if (canvas == nullptr) {
486 BKE_report(op->reports, RPT_ERROR, "Bake failed: invalid canvas");
487 return OPERATOR_CANCELLED;
488 }
489 surface = get_activeSurface(canvas);
490
491 /* Set state to baking and init surface */
492 canvas->error[0] = '\0';
493 canvas->flags |= MOD_DPAINT_BAKING;
494
496 job->bmain = CTX_data_main(C);
497 job->scene = scene_eval;
498 job->depsgraph = depsgraph;
499 job->ob = object_eval;
500 job->canvas = canvas;
501 job->surface = surface;
502
506 "Baking Dynamic Paint...",
509
513
515
516 /* Bake Dynamic Paint */
518
519 return OPERATOR_FINISHED;
520}
521
523{
524 /* identifiers */
525 ot->name = "Dynamic Paint Bake";
526 ot->description = "Bake dynamic paint image sequence surface";
527 ot->idname = "DPAINT_OT_bake";
528
529 /* API callbacks. */
532}
533
534} // namespace blender
Generic geometry attributes built on CustomData.
support for deformation groups and hooks.
#define G_MAIN
Functions for dealing with objects and deform verts, used by painting and tools.
#define FILE_MAX
#define BLI_path_join(...)
#define STRNCPY_UTF8(dst, src)
Platform independent time functions.
#define BLT_I18NCONTEXT_ID_SIMULATION
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define ND_MODIFIER
Definition WM_types.hh:503
#define NC_OBJECT
Definition WM_types.hh:392
#define output
static AttributeOwner from_id(ID *id)
Object * context_active_object(const bContext *C)
BMesh Iterators.
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void WM_global_reportf(eReportType type, const char *format,...)
void DPAINT_OT_output_toggle(wmOperatorType *ot)
bool BKE_attribute_remove(AttributeOwner &owner, StringRef name, struct ReportList *reports)
void dynamicPaint_freeSurface(const struct DynamicPaintModifierData *pmd, struct DynamicPaintSurface *surface)
int RNA_enum_get(PointerRNA *ptr, const char *name)
void dynamicPaint_outputSurfaceImage(struct DynamicPaintSurface *surface, const char *filepath, short output_layer)
@ RPT_WARNING
Definition BKE_report.hh:40
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
struct DynamicPaintSurface * dynamicPaint_createNewSurface(struct DynamicPaintCanvasSettings *canvas, struct Scene *scene)
@ REGION_DRAW_LOCK_BAKING
bDeformGroup * BKE_object_defgroup_find_name(const Object *ob, StringRef name)
Definition deform.cc:530
void BKE_spacedata_draw_locks(ARegionDrawLockFlags lock_flags)
Definition screen.cc:558
bool dynamicPaint_outputLayerExists(struct DynamicPaintSurface *surface, struct Object *ob, int output)
void dynamicPaint_freeSurfaceData(struct DynamicPaintSurface *surface)
void dynamicPaint_freeBrush(struct DynamicPaintModifierData *pmd)
@ WM_JOB_TYPE_DPAINT_BAKE
Definition WM_api.hh:2035
@ eModifierType_DynamicPaint
void DPAINT_OT_type_toggle(wmOperatorType *ot)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
bool BLI_path_frame(char *path, size_t path_maxncpy, int frame, int digits) ATTR_NONNULL(1)
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
bool ED_operator_object_active_local_editable(bContext *C)
bool dynamicPaint_createType(struct DynamicPaintModifierData *pmd, int type, struct Scene *scene)
void WM_jobs_start(wmWindowManager *wm, wmJob *wm_job)
Definition wm_jobs.cc:498
struct DynamicPaintSurface * get_activeSurface(struct DynamicPaintCanvasSettings *canvas)
Global G
Definition blender.cc:57
static wmOperatorStatus surface_slot_remove_exec(bContext *C, wmOperator *)
void WM_locked_interface_set(wmWindowManager *wm, bool lock)
void WM_locked_interface_set_with_flags(wmWindowManager *wm, short lock_flags)
void DPAINT_OT_bake(wmOperatorType *ot)
void dynamicPaint_freeCanvas(struct DynamicPaintModifierData *pmd)
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
wmWindowManager * CTX_wm_manager(const bContext *C)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
static wmOperatorStatus dynamicpaint_bake_exec(bContext *C, wmOperator *op)
static wmOperatorStatus surface_slot_add_exec(bContext *C, wmOperator *)
struct bDeformGroup * BKE_object_defgroup_add_name(struct Object *ob, const char *name)
void WM_global_report(eReportType type, const char *message)
static void dpaint_bake_free(void *customdata)
std::optional< std::string > ED_mesh_color_add(Mesh *mesh, const char *name, bool active_set, bool do_init, ReportList *reports)
Definition mesh_data.cc:277
static wmOperatorStatus type_toggle_exec(bContext *C, wmOperator *op)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
static void dpaint_bake_endjob(void *customdata)
void RNA_def_property_translation_context(PropertyRNA *prop, const char *context)
ModifierData * BKE_modifiers_findby_type(const Object *ob, ModifierType type)
void DPAINT_OT_surface_slot_remove(wmOperatorType *ot)
void ED_update_for_newframe(Main *bmain, Depsgraph *depsgraph)
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
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
Scene * DEG_get_input_scene(const Depsgraph *graph)
int dynamicPaint_calculateFrame(struct DynamicPaintSurface *surface, struct Depsgraph *depsgraph, struct Scene *scene, struct Object *cObject, int frame)
static void dynamicPaint_bakeImageSequence(DynamicPaintBakeJob *job)
void DPAINT_OT_surface_slot_add(wmOperatorType *ot)
const EnumPropertyItem rna_enum_prop_dynamicpaint_type_items[]
double BLI_time_now_seconds()
Definition time.cc:30
void DEG_relations_tag_update(Main *bmain)
Scene * CTX_data_scene(const bContext *C)
void BKE_object_defgroup_remove(struct Object *ob, struct bDeformGroup *defgroup)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
Scene * DEG_get_evaluated_scene(const Depsgraph *graph)
void WM_jobs_timer(wmJob *wm_job, double time_step, uint note, uint endnote, void(*timer_step)(void *))
Definition wm_jobs.cc:393
int dynamicPaint_createUVSurface(struct Scene *scene, struct DynamicPaintSurface *surface, float *progress, bool *do_update)
static void dpaint_bake_startjob(void *customdata, wmJobWorkerStatus *worker_status)
static wmOperatorStatus output_toggle_exec(bContext *C, wmOperator *op)
DynamicPaintSurface * surface
DynamicPaintCanvasSettings * canvas
struct DynamicPaintBrushSettings * brush
struct DynamicPaintCanvasSettings * canvas
struct DynamicPaintCanvasSettings * canvas
eDynamicPaint_SurfaceFormat format
struct DynamicPaintSurface * prev
eDynamicPaint_SurfaceFlags flags
struct DynamicPaintSurface * next
struct RenderData r
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
struct ReportList * reports
#define N_(msgid)