Blender V5.3
mask_shapekey.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2012 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdlib>
10
11#include "BLI_listbase.hh"
12
13#include "BKE_context.hh"
14#include "BKE_mask.hh"
15
16#include "DNA_mask_types.h"
17#include "DNA_scene_types.h"
18
19#include "DEG_depsgraph.hh"
21
22#include "RNA_access.hh"
23#include "RNA_define.hh"
24
25#include "WM_api.hh"
26#include "WM_types.hh"
27
28#include "ED_mask.hh" /* own include */
29
30#include "mask_intern.hh" /* own include */
31
32namespace blender {
33
34static void select_only_layer_shape(MaskLayer *mask_layer, MaskLayerShape *mask_layer_shape)
35{
36 for (MaskLayerShape &mask_layer_shape_iter : mask_layer->splines_shapes) {
37 mask_layer_shape_iter.flag &= ~MASK_SHAPE_SELECT;
38 }
39 mask_layer_shape->flag |= MASK_SHAPE_SELECT;
40}
41
43{
44 Scene *scene = CTX_data_scene(C);
45 const int frame = scene->r.cfra;
47 bool changed = false;
48
49 bool mask_was_animated = false;
50 bool mask_now_animated = false;
51
52 for (MaskLayer &mask_layer : mask->masklayers) {
53 MaskLayerShape *mask_layer_shape;
54
55 if (!ED_mask_layer_select_check(&mask_layer)) {
56 continue;
57 }
58
59 mask_was_animated |= BKE_mask_layer_is_animated(mask_layer);
60
61 mask_layer_shape = BKE_mask_layer_shape_verify_frame(&mask_layer, frame);
62 BKE_mask_layer_shape_from_mask(&mask_layer, mask_layer_shape);
63 changed = true;
64
65 mask_now_animated |= BKE_mask_layer_is_animated(mask_layer);
66
67 select_only_layer_shape(&mask_layer, mask_layer_shape);
68 }
69
70 /* The depsgraph relations were built assuming the mask was not animated, so need to update
71 * relations. */
72 if (mask_was_animated != mask_now_animated) {
74 }
75
76 if (changed) {
78 DEG_id_tag_update(&mask->id, 0);
79
80 return OPERATOR_FINISHED;
81 }
82 return OPERATOR_CANCELLED;
83}
84
86{
87 /* identifiers */
88 ot->name = "Insert Shape Key";
89 ot->description = "Insert mask shape keyframe for active mask layer at the current frame";
90 ot->idname = "MASK_OT_shape_key_insert";
91
92 /* API callbacks. */
95
96 /* flags */
98}
99
101{
102 Scene *scene = CTX_data_scene(C);
103 const int frame = scene->r.cfra;
105 bool changed = false;
106
107 bool mask_was_animated = false;
108 bool mask_now_animated = false;
109
110 for (MaskLayer &mask_layer : mask->masklayers) {
111 MaskLayerShape *mask_layer_shape;
112
113 if (!ED_mask_layer_select_check(&mask_layer)) {
114 continue;
115 }
116
117 mask_was_animated |= BKE_mask_layer_is_animated(mask_layer);
118
119 mask_layer_shape = BKE_mask_layer_shape_find_frame(&mask_layer, frame);
120
121 if (mask_layer_shape) {
122 BKE_mask_layer_shape_unlink(&mask_layer, mask_layer_shape);
123 changed = true;
124 }
125
126 mask_now_animated |= BKE_mask_layer_is_animated(mask_layer);
127 }
128
129 /* The depsgraph relations were built assuming the mask was animated, so need to update
130 * relations. */
131 if (mask_was_animated != mask_now_animated) {
133 }
134
135 if (changed) {
138
139 return OPERATOR_FINISHED;
140 }
141 return OPERATOR_CANCELLED;
142}
143
145{
146 /* identifiers */
147 ot->name = "Clear Shape Key";
148 ot->description = "Remove mask shape keyframe for active mask layer at the current frame";
149 ot->idname = "MASK_OT_shape_key_clear";
150
151 /* API callbacks. */
154
155 /* flags */
157}
158
160{
161 Scene *scene = CTX_data_scene(C);
162 const int frame = scene->r.cfra;
164 bool changed = false;
165
166 for (MaskLayer &mask_layer : mask->masklayers) {
167
168 if (mask_layer.visibility_flag & (MASK_HIDE_VIEW | MASK_HIDE_SELECT)) {
169 continue;
170 }
171
172 if (mask_layer.splines_shapes.first_) {
173 MaskLayerShape *mask_layer_shape_reset;
174
175 /* Get the shape-key of the current state. */
176 mask_layer_shape_reset = BKE_mask_layer_shape_alloc(&mask_layer, frame);
177 /* Initialize from mask - as if inserting a keyframe. */
178 BKE_mask_layer_shape_from_mask(&mask_layer, mask_layer_shape_reset);
179
180 for (MaskLayerShape &mask_layer_shape : mask_layer.splines_shapes) {
181 if (mask_layer_shape_reset->tot_vert == mask_layer_shape.tot_vert) {
182 int i_abs = 0;
183 MaskLayerShapeElem *shape_ele_src;
184 MaskLayerShapeElem *shape_ele_dst;
185
186 shape_ele_src = reinterpret_cast<MaskLayerShapeElem *>(mask_layer_shape_reset->data);
187 shape_ele_dst = reinterpret_cast<MaskLayerShapeElem *>(mask_layer_shape.data);
188
189 for (MaskSpline &spline : mask_layer.splines) {
190 for (int i = 0; i < spline.tot_point; i++) {
191 MaskSplinePoint *point = &spline.points[i];
192
193 if (BKE_mask_point_selected(point)) {
194 shape_ele_dst->weight = shape_ele_src->weight;
195 }
196
197 shape_ele_src++;
198 shape_ele_dst++;
199
200 i_abs++;
201 }
202 }
203 (void)i_abs; /* Quiet set-but-unused warning (may be removed). */
204 }
205 else {
206 // printf("%s: skipping\n", __func__);
207 }
208
209 changed = true;
210 }
211
212 BKE_mask_layer_shape_free(mask_layer_shape_reset);
213 }
214 }
215
216 if (changed) {
218 DEG_id_tag_update(&mask->id, 0);
219
220 return OPERATOR_FINISHED;
221 }
222 return OPERATOR_CANCELLED;
223}
224
226{
227 /* identifiers */
228 ot->name = "Feather Reset Animation";
229 ot->description = "Reset feather weights on all selected points animation values";
230 ot->idname = "MASK_OT_shape_key_feather_reset";
231
232 /* API callbacks. */
235
236 /* flags */
238}
239
240/*
241 * - loop over selected shape-keys.
242 * - find first-selected/last-selected pairs.
243 * - move these into a temp list.
244 * - re-key all the original shapes.
245 * - copy unselected values back from the original.
246 * - free the original.
247 */
249{
250 Scene *scene = CTX_data_scene(C);
251 const int frame = scene->r.cfra;
253 bool changed = false;
254
255 const bool do_feather = RNA_boolean_get(op->ptr, "feather");
256 const bool do_location = RNA_boolean_get(op->ptr, "location");
257
258 for (MaskLayer &mask_layer : mask->masklayers) {
259 if (mask_layer.visibility_flag & (MASK_HIDE_VIEW | MASK_HIDE_SELECT)) {
260 continue;
261 }
262
263 /* we need at least one point selected here to bother re-interpolating */
264 if (!ED_mask_layer_select_check(&mask_layer)) {
265 continue;
266 }
267
268 if (mask_layer.splines_shapes.first_) {
269 MaskLayerShape *mask_layer_shape, *mask_layer_shape_next;
270 MaskLayerShape *mask_layer_shape_lastsel = nullptr;
271
272 for (mask_layer_shape = mask_layer.splines_shapes.first(); mask_layer_shape;
273 mask_layer_shape = mask_layer_shape_next)
274 {
275 MaskLayerShape *mask_layer_shape_a = nullptr;
276 MaskLayerShape *mask_layer_shape_b = nullptr;
277
278 mask_layer_shape_next = mask_layer_shape->next;
279
280 /* find contiguous selections */
281 if (mask_layer_shape->flag & MASK_SHAPE_SELECT) {
282 if (mask_layer_shape_lastsel == nullptr) {
283 mask_layer_shape_lastsel = mask_layer_shape;
284 }
285 if ((mask_layer_shape->next == nullptr) ||
286 ((static_cast<MaskLayerShape *>(mask_layer_shape->next))->flag &
287 MASK_SHAPE_SELECT) == 0)
288 {
289 mask_layer_shape_a = mask_layer_shape_lastsel;
290 mask_layer_shape_b = mask_layer_shape;
291 mask_layer_shape_lastsel = nullptr;
292
293 /* this will be freed below, step over selection */
294 mask_layer_shape_next = mask_layer_shape->next;
295 }
296 }
297
298 /* we have a from<>to? - re-interpolate! */
299 if (mask_layer_shape_a && mask_layer_shape_b) {
300 ListBaseT<MaskLayerShape> shapes_tmp = {nullptr, nullptr};
301 MaskLayerShape *mask_layer_shape_tmp;
302 MaskLayerShape *mask_layer_shape_tmp_next;
303 MaskLayerShape *mask_layer_shape_tmp_last = mask_layer_shape_b->next;
304 MaskLayerShape *mask_layer_shape_tmp_rekey;
305
306 /* move keys */
307 for (mask_layer_shape_tmp = mask_layer_shape_a;
308 mask_layer_shape_tmp && (mask_layer_shape_tmp != mask_layer_shape_tmp_last);
309 mask_layer_shape_tmp = mask_layer_shape_tmp_next)
310 {
311 mask_layer_shape_tmp_next = mask_layer_shape_tmp->next;
312 BLI_remlink(&mask_layer.splines_shapes, mask_layer_shape_tmp);
313 BLI_addtail(&shapes_tmp, mask_layer_shape_tmp);
314 }
315
316 /* re-key, NOTE: can't modify the keys here since it messes up. */
317 for (mask_layer_shape_tmp = shapes_tmp.first(); mask_layer_shape_tmp;
318 mask_layer_shape_tmp = mask_layer_shape_tmp->next)
319 {
320 BKE_mask_layer_evaluate(&mask_layer, mask_layer_shape_tmp->frame, true);
321 mask_layer_shape_tmp_rekey = BKE_mask_layer_shape_verify_frame(
322 &mask_layer, mask_layer_shape_tmp->frame);
323 BKE_mask_layer_shape_from_mask(&mask_layer, mask_layer_shape_tmp_rekey);
324 mask_layer_shape_tmp_rekey->flag = mask_layer_shape_tmp->flag & MASK_SHAPE_SELECT;
325 }
326
327 /* restore unselected points and free copies */
328 for (mask_layer_shape_tmp = shapes_tmp.first(); mask_layer_shape_tmp;
329 mask_layer_shape_tmp = mask_layer_shape_tmp_next)
330 {
331 /* restore */
332 int i_abs = 0;
333 MaskLayerShapeElem *shape_ele_src;
334 MaskLayerShapeElem *shape_ele_dst;
335
336 mask_layer_shape_tmp_next = mask_layer_shape_tmp->next;
337
338 /* we know this exists, added above */
339 mask_layer_shape_tmp_rekey = BKE_mask_layer_shape_find_frame(
340 &mask_layer, mask_layer_shape_tmp->frame);
341
342 shape_ele_src = reinterpret_cast<MaskLayerShapeElem *>(mask_layer_shape_tmp->data);
343 shape_ele_dst = reinterpret_cast<MaskLayerShapeElem *>(
344 mask_layer_shape_tmp_rekey->data);
345
346 for (MaskSpline &spline : mask_layer.splines) {
347 for (int i = 0; i < spline.tot_point; i++) {
348 MaskSplinePoint *point = &spline.points[i];
349
350 /* not especially efficient but makes this easier to follow */
351 std::swap(*shape_ele_src, *shape_ele_dst);
352
353 if (BKE_mask_point_selected(point)) {
354 if (do_location) {
355 memcpy(
356 shape_ele_dst->point, shape_ele_src->point, sizeof(shape_ele_src->point));
357 }
358 if (do_feather) {
359 shape_ele_dst->weight = shape_ele_src->weight;
360 }
361 }
362
363 shape_ele_src++;
364 shape_ele_dst++;
365
366 i_abs++;
367 }
368 }
369 (void)i_abs; /* Quiet set-but-unused warning (may be removed). */
370
371 BKE_mask_layer_shape_free(mask_layer_shape_tmp);
372 }
373
374 changed = true;
375 }
376 }
377
378 /* re-evaluate */
379 BKE_mask_layer_evaluate(&mask_layer, frame, true);
380 }
381 }
382
383 if (changed) {
385 DEG_id_tag_update(&mask->id, 0);
386
387 return OPERATOR_FINISHED;
388 }
389 return OPERATOR_CANCELLED;
390}
391
393{
394 /* identifiers */
395 ot->name = "Re-Key Points of Selected Shapes";
396 ot->description =
397 "Recalculate animation data on selected points for frames selected in the dopesheet";
398 ot->idname = "MASK_OT_shape_key_rekey";
399
400 /* API callbacks. */
403
404 /* flags */
406
407 /* properties */
408 RNA_def_boolean(ot->srna, "location", true, "Location", "");
409 RNA_def_boolean(ot->srna, "feather", true, "Feather", "");
410}
411
412/* *** Shape Key Utils *** */
413
414void ED_mask_layer_shape_auto_key(const bContext *C, MaskLayer *mask_layer, const int frame)
415{
416 MaskLayerShape *mask_layer_shape;
417
418 const bool mask_was_animated = BKE_mask_layer_is_animated(*mask_layer);
419
420 mask_layer_shape = BKE_mask_layer_shape_verify_frame(mask_layer, frame);
421 BKE_mask_layer_shape_from_mask(mask_layer, mask_layer_shape);
422
423 const bool mask_now_animated = BKE_mask_layer_is_animated(*mask_layer);
424
425 /* The depsgraph relations were built assuming the mask was not animated, so need to update
426 * relations. */
427 if (mask_was_animated != mask_now_animated) {
429 }
430
431 select_only_layer_shape(mask_layer, mask_layer_shape);
432}
433
434bool ED_mask_layer_shape_auto_key_all(const bContext *C, Mask *mask, const int frame)
435{
436 bool changed = false;
437
438 for (MaskLayer &mask_layer : mask->masklayers) {
439 ED_mask_layer_shape_auto_key(C, &mask_layer, frame);
440 changed = true;
441 }
442
443 return changed;
444}
445
447{
448 bool changed = false;
449
450 for (MaskLayer &mask_layer : mask->masklayers) {
451
452 if (!ED_mask_layer_select_check(&mask_layer)) {
453 continue;
454 }
455
456 ED_mask_layer_shape_auto_key(C, &mask_layer, frame);
457 changed = true;
458 }
459
460 return changed;
461}
462
463} // namespace blender
#define C
Definition RandGen.cpp:29
#define ND_DATA
Definition WM_types.hh:552
#define NC_MASK
Definition WM_types.hh:411
BMesh Iterators.
bool ED_mask_layer_shape_auto_key_select(const bContext *C, Mask *mask, int frame)
MaskLayerShape * BKE_mask_layer_shape_alloc(MaskLayer *masklay, int frame)
void BKE_mask_layer_evaluate(MaskLayer *masklay, float ctime, bool do_newframe)
bool BKE_mask_layer_is_animated(MaskLayer &mask_layer)
void BKE_mask_layer_shape_from_mask(MaskLayer *masklay, MaskLayerShape *masklay_shape)
void BKE_mask_layer_shape_unlink(MaskLayer *masklay, MaskLayerShape *masklay_shape)
bool BKE_mask_point_selected(const MaskSplinePoint *p)
Definition BKE_mask.hh:155
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void DEG_id_tag_update(ID *id, unsigned int flags)
void ED_mask_layer_shape_auto_key(const bContext *C, MaskLayer *mask_layer, int frame)
Mask * CTX_data_edit_mask(const bContext *C)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
bool ED_maskedit_mask_visible_splines_poll(bContext *C)
Definition mask_edit.cc:77
static wmOperatorStatus mask_shape_key_rekey_exec(bContext *C, wmOperator *op)
bool ED_mask_layer_shape_auto_key_all(const bContext *C, Mask *mask, int frame)
void MASK_OT_shape_key_insert(wmOperatorType *ot)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
Main * CTX_data_main(const bContext *C)
MaskLayerShape * BKE_mask_layer_shape_find_frame(MaskLayer *masklay, int frame)
MaskLayerShape * BKE_mask_layer_shape_verify_frame(MaskLayer *masklay, int frame)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
void DEG_relations_tag_update(Main *bmain)
Scene * CTX_data_scene(const bContext *C)
static wmOperatorStatus mask_shape_key_feather_reset_exec(bContext *C, wmOperator *)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
bool ED_mask_layer_select_check(const MaskLayer *mask_layer)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
void MASK_OT_shape_key_clear(wmOperatorType *ot)
void BKE_mask_layer_shape_free(MaskLayerShape *masklay_shape)
static wmOperatorStatus mask_shape_key_insert_exec(bContext *C, wmOperator *)
static void select_only_layer_shape(MaskLayer *mask_layer, MaskLayerShape *mask_layer_shape)
static wmOperatorStatus mask_shape_key_clear_exec(bContext *C, wmOperator *)
void MASK_OT_shape_key_rekey(wmOperatorType *ot)
void MASK_OT_shape_key_feather_reset(wmOperatorType *ot)
MaskLayerShapeFlag flag
struct MaskLayerShape * next
ListBaseT< MaskLayerShape > splines_shapes
MaskLayerVisibility visibility_flag
ListBaseT< MaskSpline > splines
MaskSplinePoint * points
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