Blender V5.3
object_shapekey.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstring>
10
11#ifndef WIN32
12# include <unistd.h>
13#else
14# include <io.h>
15#endif
16
17#include "MEM_guardedalloc.h"
18
19#include "BLI_listbase.hh"
20#include "BLI_math_vector_c.hh"
21#include "BLI_utildefines.hh"
22
23#include "BLT_translation.hh"
24
25#include "DNA_curve_types.h"
26#include "DNA_key_types.h"
27#include "DNA_lattice_types.h"
28#include "DNA_mesh_types.h"
29#include "DNA_object_types.h"
30
31#include "BKE_context.hh"
32#include "BKE_curve.hh"
33#include "BKE_key.hh"
34#include "BKE_lattice.hh"
35#include "BKE_library.hh"
36#include "BKE_object.hh"
37#include "BKE_report.hh"
38
39#include "DEG_depsgraph.hh"
41
42#include "ED_curve.hh"
43#include "ED_lattice.hh"
44#include "ED_mesh.hh"
45#include "ED_object.hh"
46
47#include "RNA_access.hh"
48#include "RNA_define.hh"
49
50#include "WM_api.hh"
51#include "WM_types.hh"
52
53#include "object_intern.hh"
54
55namespace blender::ed::object {
56
57/* -------------------------------------------------------------------- */
60
61bool shape_key_report_if_locked(const Object *obedit, ReportList *reports)
62{
63 KeyBlock *key_block;
64
65 switch (obedit->type) {
66 case OB_MESH:
67 key_block = ED_mesh_get_edit_shape_key(id_cast<Mesh *>(obedit->data));
68 break;
69 case OB_SURF:
71 key_block = ED_curve_get_edit_shape_key(id_cast<Curve *>(obedit->data));
72 break;
73 case OB_LATTICE:
74 key_block = ED_lattice_get_edit_shape_key(id_cast<Lattice *>(obedit->data));
75 break;
76 default:
77 return false;
78 }
79
80 if (key_block && (key_block->flag & KEYBLOCK_LOCKED_SHAPE) != 0) {
81 if (reports) {
82 BKE_reportf(reports, RPT_ERROR, "The active shape key of %s is locked", obedit->id.name + 2);
83 }
84 return true;
85 }
86
87 return false;
88}
89
91{
92 const KeyBlock *kb = BKE_keyblock_from_object(ob);
93
94 if (kb && (kb->flag & KEYBLOCK_LOCKED_SHAPE) != 0) {
95 if (reports) {
96 BKE_reportf(reports, RPT_ERROR, "The active shape key of %s is locked", ob->id.name + 2);
97 }
98 return true;
99 }
100
101 return false;
102}
103
105{
106 const Key *key = BKE_key_from_object(ob);
107
108 if (key) {
109 for (const KeyBlock &kb : key->block) {
110 if (kb.flag & KEYBLOCK_LOCKED_SHAPE) {
111 return true;
112 }
113 }
114 }
115
116 return false;
117}
118
120{
122 if (reports) {
123 BKE_reportf(reports, RPT_ERROR, "The object %s has locked shape keys", ob->id.name + 2);
124 }
125 return true;
126 }
127
128 return false;
129}
130
131bool shape_key_is_selected(const Object &object, const KeyBlock &kb, const int keyblock_index)
132{
133 /* The active shape key is always considered selected. */
134 return (kb.flag & KEYBLOCK_SEL) || keyblock_index == object.shapenr - 1;
135}
136
138
139/* -------------------------------------------------------------------- */
142
143static void object_shape_key_add(bContext *C, Object *ob, const bool from_mix)
144{
145 Main *bmain = CTX_data_main(C);
146 KeyBlock *kb = BKE_object_shapekey_insert(bmain, ob, nullptr, from_mix);
147 if (kb) {
148 /* Shapekeys created via this operator should get default value 1.0. */
149 kb->curval = 1.0f;
150
151 Key *key = BKE_key_from_object(ob);
152 /* for absolute shape keys, new keys may not be added last */
153 ob->shapenr = BLI_findindex(&key->block, kb) + 1;
154
155 /* Explicitly deselect current selection when adding a new key, rather than
156 * relying on the tree view's state. This is problematic when the same data is
157 * shown in more than one view instance, see #161071. */
158 for (KeyBlock &other : key->block) {
159 SET_FLAG_FROM_TEST(other.flag, &other == kb, KEYBLOCK_SEL);
160 }
161
163 }
164}
165
167
168/* -------------------------------------------------------------------- */
171
173 Object *ob, KeyBlock *kb, const bool use_topology, int &totmirr, int &totfail)
174{
175 char *tag_elem = MEM_new_array_zeroed<char>(kb->totelem, "shape_key_mirror");
176
177 if (ob->type == OB_MESH) {
178 Mesh *mesh = id_cast<Mesh *>(ob->data);
179 int i1, i2;
180 float *fp1, *fp2;
181 float tvec[3];
182
184
185 for (i1 = 0; i1 < mesh->verts_num; i1++) {
186 i2 = mesh_get_x_mirror_vert(ob, nullptr, i1, use_topology);
187 if (i2 == i1) {
188 fp1 = (static_cast<float *>(kb->data)) + i1 * 3;
189 fp1[0] = -fp1[0];
190 tag_elem[i1] = 1;
191 totmirr++;
192 }
193 else if (i2 != -1) {
194 if (tag_elem[i1] == 0 && tag_elem[i2] == 0) {
195 fp1 = (static_cast<float *>(kb->data)) + i1 * 3;
196 fp2 = (static_cast<float *>(kb->data)) + i2 * 3;
197
198 copy_v3_v3(tvec, fp1);
199 copy_v3_v3(fp1, fp2);
200 copy_v3_v3(fp2, tvec);
201
202 /* flip x axis */
203 fp1[0] = -fp1[0];
204 fp2[0] = -fp2[0];
205 totmirr++;
206 }
207 tag_elem[i1] = tag_elem[i2] = 1;
208 }
209 else {
210 totfail++;
211 }
212 }
213
215 }
216 else if (ob->type == OB_LATTICE) {
217 const Lattice *lt = id_cast<const Lattice *>(ob->data);
218 int i1, i2;
219 float *fp1, *fp2;
220 int u, v, w;
221 /* half but found up odd value */
222 const int pntsu_half = (lt->pntsu / 2) + (lt->pntsu % 2);
223
224 /* Currently edit-mode isn't supported by mesh so ignore here for now too. */
225#if 0
226 if (lt->editlatt) {
227 lt = lt->editlatt->latt;
228 }
229#endif
230
231 for (w = 0; w < lt->pntsw; w++) {
232 for (v = 0; v < lt->pntsv; v++) {
233 for (u = 0; u < pntsu_half; u++) {
234 int u_inv = (lt->pntsu - 1) - u;
235 float tvec[3];
236 if (u == u_inv) {
237 i1 = BKE_lattice_index_from_uvw(lt, u, v, w);
238 fp1 = (static_cast<float *>(kb->data)) + i1 * 3;
239 fp1[0] = -fp1[0];
240 totmirr++;
241 }
242 else {
243 i1 = BKE_lattice_index_from_uvw(lt, u, v, w);
244 i2 = BKE_lattice_index_from_uvw(lt, u_inv, v, w);
245
246 fp1 = (static_cast<float *>(kb->data)) + i1 * 3;
247 fp2 = (static_cast<float *>(kb->data)) + i2 * 3;
248
249 copy_v3_v3(tvec, fp1);
250 copy_v3_v3(fp1, fp2);
251 copy_v3_v3(fp2, tvec);
252 fp1[0] = -fp1[0];
253 fp2[0] = -fp2[0];
254 totmirr++;
255 }
256 }
257 }
258 }
259 }
260
261 MEM_delete(tag_elem);
262}
263
265 bContext *C, Object *ob, int *r_totmirr, int *r_totfail, bool use_topology)
266{
267 Key *key;
268 int totmirr = 0, totfail = 0;
269
270 *r_totmirr = *r_totfail = 0;
271
272 key = BKE_key_from_object(ob);
273 if (key == nullptr) {
274 return false;
275 }
276
277 if (KeyBlock *kb = static_cast<KeyBlock *>(BLI_findlink(&key->block, ob->shapenr - 1))) {
278 shape_key_mirror(ob, kb, use_topology, totmirr, totfail);
279 }
280
281 *r_totmirr = totmirr;
282 *r_totfail = totfail;
283
286
287 return true;
288}
289
291
292/* -------------------------------------------------------------------- */
295
297{
298 Object *ob = context_object(C);
299 ID *data = (ob) ? ob->data : nullptr;
300
301 return (ob != nullptr && ID_IS_EDITABLE(ob) && !ID_IS_OVERRIDE_LIBRARY(ob) && data != nullptr &&
303}
304
306{
307 Object *ob = context_object(C);
308
309 return (shape_key_poll(C) &&
310 /* check a keyblock exists */
311 (BKE_keyblock_from_object(ob) != nullptr));
312}
313
315{
316 Object *ob = context_object(C);
317
318 return (shape_key_poll(C) && ob->mode != OB_MODE_EDIT);
319}
320
322{
323 Object *ob = context_object(C);
324
325 return (shape_key_mode_poll(C) &&
326 /* check a keyblock exists */
327 (BKE_keyblock_from_object(ob) != nullptr));
328}
329
331{
332 /* Same as shape_key_mode_exists_poll above, but ensure we have at least two shapes! */
333 Object *ob = context_object(C);
334 Key *key = BKE_key_from_object(ob);
335
336 return (shape_key_mode_poll(C) && key != nullptr && key->totkey > 1);
337}
338
340
341/* -------------------------------------------------------------------- */
344
346{
347 Object *ob = context_object(C);
348 const bool from_mix = RNA_boolean_get(op->ptr, "from_mix");
349
350 object_shape_key_add(C, ob, from_mix);
351
354
355 return OPERATOR_FINISHED;
356}
357
358static std::string shape_key_add_get_description(bContext * /*C*/,
359 wmOperatorType * /*ot*/,
360 PointerRNA *ptr)
361{
362 const bool do_from_mix = RNA_boolean_get(ptr, "from_mix");
363 if (do_from_mix) {
364 return TIP_("Create a new shape key from the visual result of existing mix of keys");
365 }
366
367 return "";
368}
369
371{
372 /* identifiers */
373 ot->name = "Add Shape Key";
374 ot->idname = "OBJECT_OT_shape_key_add";
375 ot->description = "Add shape key to the object";
376
377 /* API callbacks. */
381
382 /* flags */
384
385 /* properties */
387 "from_mix",
388 true,
389 "From Mix",
390 "Create a new shape key from the visual result of existing mix of keys");
391}
392
394
395/* -------------------------------------------------------------------- */
398
400{
401 Object *ob = context_object(C);
402 Key *key = BKE_key_from_object(ob);
403
404 /* List selected shape keys. */
405 blender::Vector<KeyBlock *> to_duplicate;
406 for (auto [index, keyblock] : key->block.enumerate()) {
407 const bool is_selected = shape_key_is_selected(*ob, keyblock, index);
408
409 /* Deselect all keys, so that only new ones are selected. */
410 keyblock.flag &= ~KEYBLOCK_SEL;
411
412 if (index == 0) {
413 /* Never duplicate the base key, it's special. */
414 continue;
415 }
416 if (is_selected) {
417 to_duplicate.append(&keyblock);
418 }
419 }
420
421 KeyBlock *kb_new = nullptr;
422 for (KeyBlock *kb_src : to_duplicate) {
423 kb_new = BKE_keyblock_duplicate(key, kb_src);
424 kb_new->flag |= KEYBLOCK_SEL;
425 }
426
427 if (kb_new != nullptr) {
428 ob->shapenr = BLI_findindex(&key->block, kb_new) + 1;
429 }
430
434 return OPERATOR_FINISHED;
435}
436
438{
439 ot->name = "Duplicate Shape Key";
440 ot->idname = "OBJECT_OT_shape_key_copy";
441 ot->description = "Duplicate the active shape key";
442
445
447}
448
450
451/* -------------------------------------------------------------------- */
454
456{
457 Main *bmain = CTX_data_main(C);
458 Object *ob = context_object(C);
459 bool changed = false;
460
461 if (RNA_boolean_get(op->ptr, "all")) {
463 return OPERATOR_CANCELLED;
464 }
465
466 if (RNA_boolean_get(op->ptr, "apply_mix")) {
467 float *arr = BKE_key_evaluate_object_ex(ob, nullptr, nullptr, 0, std::nullopt, ob->data);
468 MEM_delete(arr);
469 }
470 changed = BKE_object_shapekey_free(bmain, ob);
471 }
472 else {
473 int num_selected_but_locked = 0;
474 /* This could be moved into a function of its own at some point. Right now it's only used here,
475 * though, since its inner structure is tailored for allowing shapekey deletion. */
476 Key &key = *BKE_key_from_object(ob);
477 for (KeyBlock &kb : key.block.items_mutable()) {
478 /* Always try to find the keyblock again, as the previous one may have been deleted. For
479 * the same reason, ob->shapenr has to be re-evaluated on every loop iteration. */
480 const int cur_index = BLI_findindex(&key.block, &kb);
481 if (!shape_key_is_selected(*ob, kb, cur_index)) {
482 continue;
483 }
484 if (kb.flag & KEYBLOCK_LOCKED_SHAPE) {
485 num_selected_but_locked++;
486 continue;
487 }
488
489 changed |= BKE_object_shapekey_remove(bmain, ob, &kb);
490
491 /* When `BKE_object_shapekey_remove()` deletes the active shapekey, the active shapekeyindex
492 * is updated as well. It usually decrements, which means that even when the same index is
493 * re-visited, we don't see the active one any more. However, when the basis key (index=0) is
494 * deleted AND there are keys remaining, the active index remains set to 0, and so every
495 * iteration sees "the active shapekey", effectively deleting all of them. */
496 if (cur_index == 0) {
497 ob->shapenr = 0;
498 }
499 }
500
501 if (num_selected_but_locked) {
503 changed ? RPT_WARNING : RPT_ERROR,
504 "Could not delete %d locked shape key(s)",
505 num_selected_but_locked);
506 }
507 }
508
509 /* Ensure that there is still a shapekey active, if there are any. See the comment above. Be
510 * extra careful here, because the deletion of the last shapekey can delete the entire Key ID,
511 * making our `key` reference (from the code above) invalid. */
512 if (ob->shapenr == 0) {
513 Key *key = BKE_key_from_object(ob);
514 if (key && key->totkey > 0) {
515 ob->shapenr = 1;
516 }
517 }
518
519 if (changed) {
523
524 return OPERATOR_FINISHED;
525 }
526 return OPERATOR_CANCELLED;
527}
528
529static bool shape_key_remove_poll_property(const bContext * /*C*/,
530 wmOperator *op,
531 const PropertyRNA *prop)
532{
533 const char *prop_id = RNA_property_identifier(prop);
534 const bool do_all = RNA_enum_get(op->ptr, "all");
535
536 /* Only show seed for randomize action! */
537 if (STREQ(prop_id, "apply_mix") && !do_all) {
538 return false;
539 }
540 return true;
541}
542
544 wmOperatorType * /*ot*/,
545 PointerRNA *ptr)
546{
547 const bool do_apply_mix = RNA_boolean_get(ptr, "apply_mix");
548 if (do_apply_mix) {
549 return TIP_("Apply current visible shape to the object data, and delete all shape keys");
550 }
551
552 const bool do_remove_all = RNA_boolean_get(ptr, "all");
553 if (do_remove_all) {
554 return TIP_("Remove all shape keys from the object");
555 }
556
557 return "";
558}
559
561{
562 /* identifiers */
563 ot->name = "Remove Shape Key";
564 ot->idname = "OBJECT_OT_shape_key_remove";
565 ot->description = "Remove selected shape keys from the object";
566
567 /* API callbacks. */
572
573 /* flags */
575
576 /* properties */
577 RNA_def_boolean(ot->srna, "all", false, "All", "Remove all shape keys");
579 "apply_mix",
580 false,
581 "Apply Mix",
582 "Apply current mix of shape keys to the geometry before removing them");
583}
584
586
587/* -------------------------------------------------------------------- */
590
592{
593 Object *ob = context_object(C);
594 Key *key = BKE_key_from_object(ob);
595
596 if (!key || key->block.is_empty()) {
597 return OPERATOR_CANCELLED;
598 }
599
600 for (KeyBlock &kb : key->block) {
601 kb.curval = clamp_f(0.0f, kb.slidermin, kb.slidermax);
602 }
603
606
607 return OPERATOR_FINISHED;
608}
609
611{
612 /* identifiers */
613 ot->name = "Clear Shape Keys";
614 ot->description =
615 "Reset the weights of all shape keys to 0 or to the closest value respecting the limits";
616 ot->idname = "OBJECT_OT_shape_key_clear";
617
618 /* API callbacks. */
619 ot->poll = shape_key_poll;
621
622 /* flags */
624}
625
626/* starting point and step size could be optional */
628{
629 Object *ob = context_object(C);
630 Key *key = BKE_key_from_object(ob);
631 float cfra = 0.0f;
632
633 if (!key || key->block.is_empty()) {
634 return OPERATOR_CANCELLED;
635 }
636
637 for (KeyBlock &kb : key->block) {
638 kb.pos = cfra;
639 cfra += 0.1f;
640 }
641
644
645 return OPERATOR_FINISHED;
646}
647
649{
650 /* identifiers */
651 ot->name = "Re-Time Shape Keys";
652 ot->description = "Resets the timing for absolute shape keys";
653 ot->idname = "OBJECT_OT_shape_key_retime";
654
655 /* API callbacks. */
656 ot->poll = shape_key_poll;
658
659 /* flags */
661}
662
664
665/* -------------------------------------------------------------------- */
668
670{
671 Object *ob = context_object(C);
672 int totmirr = 0, totfail = 0;
673 bool use_topology = RNA_boolean_get(op->ptr, "use_topology");
674
676 return OPERATOR_CANCELLED;
677 }
678
679 if (!object_shape_key_mirror(C, ob, &totmirr, &totfail, use_topology)) {
680 return OPERATOR_CANCELLED;
681 }
682
683 ED_mesh_report_mirror(*op->reports, totmirr, totfail);
684
685 return OPERATOR_FINISHED;
686}
687
689 wmOperatorType * /*ot*/,
690 PointerRNA *ptr)
691{
692 const bool do_use_topology = RNA_boolean_get(ptr, "use_topology");
693 if (do_use_topology) {
694 return TIP_(
695 "Mirror the active shape key along the local X axis using topology based mirroring\n"
696 "(for when both sides of mesh have matching, unique topology)");
697 }
698
699 return "";
700}
701
703{
704 /* identifiers */
705 ot->name = "Mirror Shape Key";
706 ot->idname = "OBJECT_OT_shape_key_mirror";
707 ot->description = "Mirror the active shape key along the local X axis";
708
709 /* API callbacks. */
713
714 /* flags */
716
717 /* properties */
719 ot->srna,
720 "use_topology",
721 false,
722 "Topology Mirror",
723 "Use topology based mirroring (for when both sides of mesh have matching, unique topology)");
724}
725
727
728/* -------------------------------------------------------------------- */
731
738
740{
741 Object *ob = context_object(C);
742
743 const Key &key = *BKE_key_from_object(ob);
744 const KeyBlockMove type = KeyBlockMove(RNA_enum_get(op->ptr, "type"));
745 const int totkey = key.totkey;
746 int new_index = 0;
747 bool changed = false;
748
749 if (type < 0) { /* Moving upwards. */
750 /* Don't move above the position of the basis key */
751 int top_index = 1;
752 /* Start from index 1 to ignore basis key from being able to move above. */
753 for (int index = 1; index < totkey; index++) {
754 const KeyBlock &kb = *static_cast<KeyBlock *>(BLI_findlink(&key.block, index));
755 if (!shape_key_is_selected(*ob, kb, index)) {
756 continue;
757 }
758 switch (type) {
759 case KB_MOVE_TOP:
760 new_index = top_index;
761 break;
762 case KB_MOVE_UP:
763 new_index = max_ii(index - 1, top_index);
764 break;
765 case KB_MOVE_BOTTOM:
766 case KB_MOVE_DOWN:
768 break;
769 }
770 top_index++;
771 if (new_index < 0) {
772 continue;
773 }
774 changed |= BKE_keyblock_move(ob, index, new_index);
775 }
776 }
777 else { /* Moving downwards. */
778 int bottom_index = totkey - 1;
779 /* Skip basis key to prevent it from moving downwards. */
780 for (int index = totkey - 1; index >= 1; index--) {
781 const KeyBlock &kb = *static_cast<KeyBlock *>(BLI_findlink(&key.block, index));
782 if (!shape_key_is_selected(*ob, kb, index)) {
783 continue;
784 }
785 switch (type) {
786 case KB_MOVE_BOTTOM:
787 new_index = bottom_index;
788 break;
789 case KB_MOVE_DOWN:
790 new_index = min_ii(index + 1, bottom_index);
791 break;
792 case KB_MOVE_TOP:
793 case KB_MOVE_UP:
795 break;
796 }
797 bottom_index--;
798 changed |= BKE_keyblock_move(ob, index, new_index);
799 }
800 }
801
802 if (!changed) {
803 return OPERATOR_CANCELLED;
804 }
805
808
809 return OPERATOR_FINISHED;
810}
811
813{
814 static const EnumPropertyItem slot_move[] = {
815 {KB_MOVE_TOP, "TOP", 0, "Top", "Top of the list"},
816 {KB_MOVE_UP, "UP", 0, "Up", ""},
817 {KB_MOVE_DOWN, "DOWN", 0, "Down", ""},
818 {KB_MOVE_BOTTOM, "BOTTOM", 0, "Bottom", "Bottom of the list"},
819 {0, nullptr, 0, nullptr, nullptr}};
820
821 /* identifiers */
822 ot->name = "Move Shape Key";
823 ot->idname = "OBJECT_OT_shape_key_move";
824 ot->description = "Move selected shape keys up/down in the list";
825
826 /* API callbacks. */
829
830 /* flags */
832
833 RNA_def_enum(ot->srna, "type", slot_move, 0, "Type", "");
834}
835
837
838/* -------------------------------------------------------------------- */
841
842enum {
845};
846
848{
849 Object *ob = context_object(C);
850 const int action = RNA_enum_get(op->ptr, "action");
851 const Key *keys = BKE_key_from_object(ob);
852
853 if (!keys || keys->block.is_empty()) {
854 return OPERATOR_CANCELLED;
855 }
856
857 for (KeyBlock &kb : keys->block) {
858 switch (action) {
859 case SHAPE_KEY_LOCK:
861 break;
862 case SHAPE_KEY_UNLOCK:
864 break;
865 default:
866 BLI_assert(0);
867 }
868 }
869
871
872 return OPERATOR_FINISHED;
873}
874
875static std::string shape_key_lock_get_description(bContext * /*C*/,
876 wmOperatorType * /*op*/,
877 PointerRNA *ptr)
878{
879 const int action = RNA_enum_get(ptr, "action");
880
881 switch (action) {
882 case SHAPE_KEY_LOCK:
883 return TIP_("Lock all shape keys of the active object");
884 break;
885 case SHAPE_KEY_UNLOCK:
886 return TIP_("Unlock all shape keys of the active object");
887 break;
888 default:
889 return "";
890 }
891}
892
894{
895 static const EnumPropertyItem shape_key_lock_actions[] = {
896 {SHAPE_KEY_LOCK, "LOCK", 0, "Lock", "Lock all shape keys"},
897 {SHAPE_KEY_UNLOCK, "UNLOCK", 0, "Unlock", "Unlock all shape keys"},
898 {0, nullptr, 0, nullptr, nullptr},
899 };
900
901 /* identifiers */
902 ot->name = "Change the Lock On Shape Keys";
903 ot->idname = "OBJECT_OT_shape_key_lock";
904 ot->description = "Change the lock state of all shape keys of active object";
905
906 /* API callbacks. */
910
911 /* flags */
913
914 RNA_def_enum(ot->srna,
915 "action",
916 shape_key_lock_actions,
918 "Action",
919 "Lock action to execute on vertex groups");
920}
921
923
924/* -------------------------------------------------------------------- */
927
929{
930 if (!shape_key_exists_poll(C)) {
931 return false;
932 }
933
934 Object *ob = context_object(C);
935 /* 0 = nothing active, 1 = basis key active. */
936 return ob->shapenr > 1;
937}
938
940{
941 Object *ob = context_object(C);
942 Key *key = BKE_key_from_object(ob);
943 KeyBlock *old_basis_key = key->block.first();
944
945 /* Make the new basis by moving the active key to index 0. */
946 const int from_index = -1; /* Interpreted as "the active key". */
947 const int to_index = 0; /* Offset by 1 compared to ob->shapenr. */
948 const bool changed = BKE_keyblock_move(ob, from_index, to_index);
949
950 if (!changed) {
951 /* The poll function should have prevented this operator from being called
952 * on the current basis key. */
954 return OPERATOR_CANCELLED;
955 }
956
957 /* Make the old & new basis keys "Relative to" the new basis key. For the new key it doesn't
958 * matter much, as it's treated as special anyway, but keeping it relative to another key makes
959 * no sense. For the old basis key (which just became a normal key), it would otherwise still be
960 * relative to itself, effectively disabling it. */
961 KeyBlock *new_basis_key = key->block.first();
962 new_basis_key->relative = 0;
963 old_basis_key->relative = 0;
964
965 /* Apply new basis key on original data. This is needed so that creating new shape-keys will
966 * start from the new basis shape. */
967 switch (ob->type) {
968 case OB_MESH: {
969 Mesh *mesh = id_cast<Mesh *>(ob->data);
970 BKE_keyblock_convert_to_mesh(new_basis_key, mesh->vert_positions_for_write());
971 break;
972 }
973 case OB_CURVES_LEGACY:
974 case OB_SURF:
975 BKE_keyblock_convert_to_curve(new_basis_key,
976 id_cast<Curve *>(ob->data),
977 BKE_curve_nurbs_get(id_cast<Curve *>(ob->data)));
978 break;
979 case OB_LATTICE:
980 BKE_keyblock_convert_to_lattice(new_basis_key, id_cast<Lattice *>(ob->data));
981 break;
982 default:
983 break;
984 }
985
988
989 return OPERATOR_FINISHED;
990}
991
993{
994 /* identifiers */
995 ot->name = "Make Shape Key the Basis Key";
996 ot->idname = "OBJECT_OT_shape_key_make_basis";
997 ot->description =
998 "Make the active shape key the new basis key, effectively applying it to the mesh.\n"
999 "Note that this applies the shape key at its 100% value";
1000
1001 /* API callbacks. */
1004
1005 /* flags */
1007}
1008
1010
1011/* -------------------------------------------------------------------- */
1014
1016{
1017 if (!shape_key_exists_poll(C)) {
1018 return false;
1019 }
1020
1021 Object *ob = context_object(C);
1022 if (ob->type != OB_MESH) {
1023 return false;
1024 }
1025 /* 0 = nothing active, 1 = basis key active. */
1026 return ob->shapenr > 1;
1027}
1028
1030{
1031 BLI_assert(a.size() == b.size());
1032 threading::parallel_for(a.index_range(), 2048, [&](const IndexRange range) {
1033 for (const int i : range) {
1034 a[i] += b[i];
1035 }
1036 });
1037}
1038
1040{
1041 Main *bmain = CTX_data_main(C);
1042 Object *ob = context_object(C);
1043 Key *key = BKE_key_from_object(ob);
1044 KeyBlock *basis_key = key->block.first();
1045 MutableSpan<float3> basis_data(static_cast<float3 *>(basis_key->data), basis_key->totelem);
1046 Mesh &mesh = id_cast<Mesh &>(*ob->data);
1047
1048 MutableSpan<float3> positions = mesh.vert_positions_for_write();
1049
1050 int locked_count = 0;
1051 Array<bool> keys_to_process(key->block.count(), false);
1052 for (const auto [i, kb] : key->block.enumerate()) {
1053 if (!shape_key_is_selected(*ob, kb, i)) {
1054 continue;
1055 }
1056 if (kb.flag & KEYBLOCK_LOCKED_SHAPE) {
1057 locked_count++;
1058 continue;
1059 }
1060 keys_to_process[i] = true;
1061 }
1062
1063 if (locked_count != 0) {
1064 BKE_reportf(op->reports, RPT_INFO, "Skipped %d locked shape keys", locked_count);
1065 }
1066
1067 if (!keys_to_process.as_span().contains(true)) {
1068 return OPERATOR_CANCELLED;
1069 }
1070
1071 int totelem_dummy;
1073 &totelem_dummy,
1074 positions.cast<float>().data(),
1075 sizeof(float3) * positions.size(),
1076 keys_to_process,
1077 nullptr);
1078
1079 Array<float3> translations(positions.size());
1080 for (const int i : positions.index_range()) {
1081 translations[i] = positions[i] - basis_data[i];
1082 }
1083
1084 basis_data.copy_from(positions);
1085
1086 Set<KeyBlock *> processed_keys;
1087 for (const auto [i, kb] : key->block.enumerate()) {
1088 if (keys_to_process[i]) {
1089 processed_keys.add_new(&kb);
1090 }
1091 }
1092 for (KeyBlock *kb : processed_keys) {
1093 BKE_object_shapekey_remove(bmain, ob, kb);
1094 }
1095
1096 if (const std::optional<Array<bool>> dependent = BKE_keyblock_get_dependent_keys(key, 0)) {
1097 for (const auto [i, kb] : key->block.enumerate()) {
1098 if (&kb == basis_key) {
1099 continue;
1100 }
1101 if (!(*dependent)[i]) {
1102 continue;
1103 }
1104 MutableSpan<float3> kb_data(static_cast<float3 *>(kb.data), kb.totelem);
1105 add_arrays(kb_data, translations);
1106 }
1107 }
1108
1109 /* Make the basis key active. */
1110 ob->shapenr = 1;
1111
1114
1115 return OPERATOR_FINISHED;
1116}
1117
1119{
1120 ot->name = "Apply to Basis Key";
1121 ot->idname = "OBJECT_OT_shape_key_apply_to_basis";
1122 ot->description = "Apply deformations of selected shape keys to the basis key, removing them";
1123
1126
1128}
1129
1131
1132} // namespace blender::ed::object
General operations, lookup, etc. for blender objects.
#define TIP_(msgid)
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define ND_DRAW
Definition WM_types.hh:502
#define NC_OBJECT
Definition WM_types.hh:392
#define BLI_assert_unreachable()
#define SET_FLAG_FROM_TEST(value, test, flag)
#define STREQ(a, b)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
Span< T > as_span() const
Definition BLI_array.hh:254
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr MutableSpan< NewT > cast() const
Definition BLI_span.hh:755
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
constexpr void copy_from(Span< T > values) const
Definition BLI_span.hh:745
void add_new(const Key &key)
Definition BLI_set.hh:233
void append(const T &value)
void OBJECT_OT_shape_key_clear(wmOperatorType *ot)
static wmOperatorStatus shape_key_clear_exec(bContext *C, wmOperator *)
static bool shape_key_make_basis_poll(bContext *C)
static bool object_shape_key_mirror(bContext *C, Object *ob, int *r_totmirr, int *r_totfail, bool use_topology)
void OBJECT_OT_shape_key_move(wmOperatorType *ot)
static void add_arrays(const MutableSpan< float3 > a, const Span< float3 > b)
static void object_shape_key_add(bContext *C, Object *ob, const bool from_mix)
bool shape_key_report_if_locked(const Object *obedit, ReportList *reports)
static wmOperatorStatus shape_key_retime_exec(bContext *C, wmOperator *)
static wmOperatorStatus shape_key_apply_to_basis_exec(bContext *C, wmOperator *op)
bool shape_key_report_if_any_locked(Object *ob, ReportList *reports)
static std::string shape_key_mirror_get_description(bContext *, wmOperatorType *, PointerRNA *ptr)
static bool shape_key_exists_poll(bContext *C)
Object * context_object(const bContext *C)
static wmOperatorStatus shape_key_copy_exec(bContext *C, wmOperator *)
void OBJECT_OT_shape_key_copy(wmOperatorType *ot)
static bool shape_key_mode_exists_poll(bContext *C)
static std::string shape_key_add_get_description(bContext *, wmOperatorType *, PointerRNA *ptr)
static bool shape_key_move_poll(bContext *C)
static bool shape_key_mode_poll(bContext *C)
static wmOperatorStatus shape_key_remove_exec(bContext *C, wmOperator *op)
static wmOperatorStatus shape_key_add_exec(bContext *C, wmOperator *op)
void shape_key_mirror(Object *ob, KeyBlock *kb, bool use_topology, int &totmirr, int &totfail)
bool shape_key_report_if_active_locked(Object *ob, ReportList *reports)
static bool object_is_any_shape_key_locked(Object *ob)
static wmOperatorStatus shape_key_make_basis_exec(bContext *C, wmOperator *)
void OBJECT_OT_shape_key_lock(wmOperatorType *ot)
static wmOperatorStatus shape_key_lock_exec(bContext *C, wmOperator *op)
static bool shape_key_poll(bContext *C)
void OBJECT_OT_shape_key_make_basis(wmOperatorType *ot)
static bool shape_key_apply_to_basis_poll(bContext *C)
void OBJECT_OT_shape_key_retime(wmOperatorType *ot)
void OBJECT_OT_shape_key_add(wmOperatorType *ot)
static bool shape_key_remove_poll_property(const bContext *, wmOperator *op, const PropertyRNA *prop)
static std::string shape_key_remove_get_description(bContext *, wmOperatorType *, PointerRNA *ptr)
void OBJECT_OT_shape_key_apply_to_basis(wmOperatorType *ot)
void OBJECT_OT_shape_key_remove(wmOperatorType *ot)
static wmOperatorStatus shape_key_move_exec(bContext *C, wmOperator *op)
bool shape_key_is_selected(const Object &object, const KeyBlock &kb, int keyblock_index)
static std::string shape_key_lock_get_description(bContext *, wmOperatorType *, PointerRNA *ptr)
void OBJECT_OT_shape_key_mirror(wmOperatorType *ot)
static wmOperatorStatus shape_key_mirror_exec(bContext *C, wmOperator *op)
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:69
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
KeyBlock * BKE_keyblock_duplicate(Key *key, KeyBlock *kb_src)
Definition key.cc:1367
int RNA_enum_get(PointerRNA *ptr, const char *name)
@ RPT_WARNING
Definition BKE_report.hh:40
MINLINE void copy_v3_v3(float r[3], const float a[3])
KeyBlock * ED_lattice_get_edit_shape_key(const Lattice *latt)
bool BKE_keyblock_move(Object *ob, int org_index, int new_index)
Definition key.cc:1778
ListBaseT< Nurb > * BKE_curve_nurbs_get(Curve *cu)
Definition curve.cc:4992
const char * RNA_property_identifier(const PropertyRNA *prop)
void BKE_keyblock_convert_to_mesh(const KeyBlock *kb, MutableSpan< float3 > vert_positions)
Definition key.cc:1697
void ED_mesh_mirror_spatial_table_end(Object *ob)
KeyBlock * BKE_keyblock_from_object(Object *ob)
Definition key.cc:1407
void BKE_keyblock_convert_to_curve(KeyBlock *kb, Curve *cu, ListBaseT< Nurb > *nurb)
Definition key.cc:1658
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
float * BKE_key_evaluate_object_ex(Object *ob, int *r_totelem, float *arr, size_t arr_size, std::optional< Span< bool > > keys_to_process, ID *obdata)
Definition key.cc:1009
int mesh_get_x_mirror_vert(Object *ob, Mesh *mesh_eval, int index, bool use_topology)
Definition meshtools.cc:297
MINLINE int max_ii(int a, int b)
ATTR_WARN_UNUSED_RESULT const BMVert * v
std::optional< Array< bool > > BKE_keyblock_get_dependent_keys(const Key *key, int index)
Definition key.cc:1875
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
KeyBlock * ED_curve_get_edit_shape_key(const Curve *cu)
Definition editcurve.cc:104
void DEG_id_tag_update(ID *id, unsigned int flags)
BMesh const char void * data
int BKE_lattice_index_from_uvw(const Lattice *lt, int u, int v, int w)
Definition lattice.cc:199
bool BKE_object_shapekey_free(Main *bmain, Object *ob)
Definition object.cc:4802
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
KeyBlock * BKE_object_shapekey_insert(Main *bmain, Object *ob, const char *name, bool from_mix)
Definition object.cc:4770
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
@ KEYBLOCK_LOCKED_SHAPE
bool BKE_object_shapekey_remove(Main *bmain, Object *ob, KeyBlock *kb)
Definition object.cc:4819
void ED_mesh_report_mirror(ReportList &reports, int totmirr, int totfail)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
Main * CTX_data_main(const bContext *C)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
void ED_mesh_mirror_spatial_table_begin(Object *ob, Mesh *mesh_eval)
void DEG_relations_tag_update(Main *bmain)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
VecBase< float, 3 > float3
Key * BKE_key_from_object(Object *ob)
Definition key.cc:1311
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
void BKE_keyblock_convert_to_lattice(const KeyBlock *kb, Lattice *lt)
Definition key.cc:1517
MINLINE int min_ii(int a, int b)
KeyBlock * ED_mesh_get_edit_shape_key(const Mesh *me)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
MINLINE float clamp_f(float value, float min, float max)
struct Lattice * latt
char name[258]
Definition DNA_ID.h:433
KeyBlockFlag flag
ListBaseT< KeyBlock > block
struct EditLatt * editlatt
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
bool(*) poll_property(const bContext *C, wmOperator *op, const PropertyRNA *prop) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1171
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
std::string(*) get_description(bContext *C, wmOperatorType *ot, PointerRNA *ptr)
Definition WM_types.hh:1197
struct ReportList * reports