1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
* Pose Mode API's and Operators for Pose Mode armatures
*/
/** \file
* \ingroup edarmature
*/
#include "MEM_guardedalloc.h"
#include "BLI_blenlib.h"
#include "BLI_math.h"
#include "DNA_anim_types.h"
#include "DNA_armature_types.h"
#include "DNA_object_types.h"
#include "DNA_scene_types.h"
#include "BKE_action.h"
#include "BKE_anim_visualization.h"
#include "BKE_armature.h"
#include "BKE_context.h"
#include "BKE_deform.h"
#include "BKE_global.h"
#include "BKE_layer.h"
#include "BKE_main.h"
#include "BKE_object.h"
#include "BKE_report.h"
#include "BKE_scene.h"
#include "DEG_depsgraph.h"
#include "DEG_depsgraph_query.h"
#include "RNA_access.h"
#include "RNA_define.h"
#include "RNA_enum_types.h"
#include "WM_api.h"
#include "WM_types.h"
#include "ED_anim_api.h"
#include "ED_armature.h"
#include "ED_keyframing.h"
#include "ED_object.h"
#include "ED_screen.h"
#include "ED_view3d.h"
#include "UI_interface.h"
#include "armature_intern.h"
#undef DEBUG_TIME
#include "PIL_time.h"
#ifdef DEBUG_TIME
# include "PIL_time_utildefines.h"
#endif
/* matches logic with ED_operator_posemode_context() */
Object *ED_pose_object_from_context(bContext *C)
{
ScrArea *area = CTX_wm_area(C);
Object *ob;
/* Since this call may also be used from the buttons window,
* we need to check for where to get the object. */
if (area && area->spacetype == SPACE_PROPERTIES) {
ob = ED_object_context(C);
}
else {
ob = BKE_object_pose_armature_get(CTX_data_active_object(C));
}
return ob;
}
/* This function is used to process the necessary updates for */
bool ED_object_posemode_enter_ex(struct Main *bmain, Object *ob)
{
BLI_assert(!ID_IS_LINKED(ob));
bool ok = false;
switch (ob->type) {
case OB_ARMATURE:
ob->restore_mode = ob->mode;
ob->mode |= OB_MODE_POSE;
/* Inform all CoW versions that we changed the mode. */
DEG_id_tag_update_ex(bmain, &ob->id, ID_RECALC_COPY_ON_WRITE);
ok = true;
break;
default:
break;
}
return ok;
}
bool ED_object_posemode_enter(bContext *C, Object *ob)
{
ReportList *reports = CTX_wm_reports(C);
if (ID_IS_LINKED(ob)) {
BKE_report(reports, RPT_WARNING, "Cannot pose libdata");
return false;
}
struct Main *bmain = CTX_data_main(C);
bool ok = ED_object_posemode_enter_ex(bmain, ob);
if (ok) {
WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_MODE_POSE, NULL);
}
return ok;
}
bool ED_object_posemode_exit_ex(struct Main *bmain, Object *ob)
{
bool ok = false;
if (ob) {
ob->restore_mode = ob->mode;
ob->mode &= ~OB_MODE_POSE;
/* Inform all CoW versions that we changed the mode. */
DEG_id_tag_update_ex(bmain, &ob->id, ID_RECALC_COPY_ON_WRITE);
ok = true;
}
return ok;
}
bool ED_object_posemode_exit(bContext *C, Object *ob)
{
struct Main *bmain = CTX_data_main(C);
bool ok = ED_object_posemode_exit_ex(bmain, ob);
if (ok) {
WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_MODE_OBJECT, NULL);
}
return ok;
}
/* if a selected or active bone is protected, throw error (oonly if warn == 1) and return 1 */
/* only_selected == 1: the active bone is allowed to be protected */
#if 0 /* UNUSED 2.5 */
static bool pose_has_protected_selected(Object *ob, short warn)
{
/* check protection */
if (ob->proxy) {
bPoseChannel *pchan;
bArmature *arm = ob->data;
for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
if (pchan->bone && (pchan->bone->layer & arm->layer)) {
if (pchan->bone->layer & arm->layer_protected) {
if (pchan->bone->flag & BONE_SELECTED) {
break;
}
}
}
}
if (pchan) {
if (warn) {
error("Cannot change Proxy protected bones");
}
return 1;
}
}
return 0;
}
#endif
/* ********************************************** */
/* Motion Paths */
static eAnimvizCalcRange pose_path_convert_range(ePosePathCalcRange range)
{
switch (range) {
case POSE_PATH_CALC_RANGE_CURRENT_FRAME:
return ANIMVIZ_CALC_RANGE_CURRENT_FRAME;
case POSE_PATH_CALC_RANGE_CHANGED:
return ANIMVIZ_CALC_RANGE_CHANGED;
case POSE_PATH_CALC_RANGE_FULL:
return ANIMVIZ_CALC_RANGE_FULL;
}
return ANIMVIZ_CALC_RANGE_FULL;
}
/* For the object with pose/action: update paths for those that have got them
* This should selectively update paths that exist...
*
* To be called from various tools that do incremental updates
*/
void ED_pose_recalculate_paths(bContext *C, Scene *scene, Object *ob, ePosePathCalcRange range)
{
/* Transform doesn't always have context available to do update. */
if (C == NULL) {
return;
}
Main *bmain = CTX_data_main(C);
ViewLayer *view_layer = CTX_data_view_layer(C);
Depsgraph *depsgraph;
bool free_depsgraph = false;
ListBase targets = {NULL, NULL};
/* set flag to force recalc, then grab the relevant bones to target */
ob->pose->avs.recalc |= ANIMVIZ_RECALC_PATHS;
animviz_get_object_motionpaths(ob, &targets);
/* recalculate paths, then free */
#ifdef DEBUG_TIME
TIMEIT_START(pose_path_calc);
#endif
/* For a single frame update it's faster to re-use existing dependency graph and avoid overhead
* of building all the relations and so on for a temporary one. */
if (range == POSE_PATH_CALC_RANGE_CURRENT_FRAME) {
/* NOTE: Dependency graph will be evaluated at all the frames, but we first need to access some
* nested pointers, like animation data. */
depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
free_depsgraph = false;
}
else {
depsgraph = animviz_depsgraph_build(bmain, scene, view_layer, &targets);
free_depsgraph = true;
}
animviz_calc_motionpaths(
depsgraph, bmain, scene, &targets, pose_path_convert_range(range), !free_depsgraph);
#ifdef DEBUG_TIME
TIMEIT_END(pose_path_calc);
#endif
BLI_freelistN(&targets);
if (range != POSE_PATH_CALC_RANGE_CURRENT_FRAME) {
/* Tag armature object for copy on write - so paths will draw/redraw.
* For currently frame only we update evaluated object directly. */
DEG_id_tag_update(&ob->id, ID_RECALC_COPY_ON_WRITE);
}
/* Free temporary depsgraph. */
if (free_depsgraph) {
DEG_graph_free(depsgraph);
}
}
/* show popup to determine settings */
static int pose_calculate_paths_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
{
Object *ob = BKE_object_pose_armature_get(CTX_data_active_object(C));
if (ELEM(NULL, ob, ob->pose)) {
return OPERATOR_CANCELLED;
}
/* set default settings from existing/stored settings */
{
bAnimVizSettings *avs = &ob->pose->avs;
PointerRNA avs_ptr;
RNA_int_set(op->ptr, "start_frame", avs->path_sf);
RNA_int_set(op->ptr, "end_frame", avs->path_ef);
RNA_pointer_create(NULL, &RNA_AnimVizMotionPaths, avs, &avs_ptr);
RNA_enum_set(op->ptr, "bake_location", RNA_enum_get(&avs_ptr, "bake_location"));
}
/* show popup dialog to allow editing of range... */
/* FIXME: hard-coded dimensions here are just arbitrary. */
return WM_operator_props_dialog_popup(C, op, 200);
}
/* For the object with pose/action: create path curves for selected bones
* This recalculates the WHOLE path within the pchan->pathsf and pchan->pathef range
*/
static int pose_calculate_paths_exec(bContext *C, wmOperator *op)
{
Object *ob = BKE_object_pose_armature_get(CTX_data_active_object(C));
Scene *scene = CTX_data_scene(C);
if (ELEM(NULL, ob, ob->pose)) {
return OPERATOR_CANCELLED;
}
/* grab baking settings from operator settings */
{
bAnimVizSettings *avs = &ob->pose->avs;
PointerRNA avs_ptr;
avs->path_sf = RNA_int_get(op->ptr, "start_frame");
avs->path_ef = RNA_int_get(op->ptr, "end_frame");
RNA_pointer_create(NULL, &RNA_AnimVizMotionPaths, avs, &avs_ptr);
RNA_enum_set(&avs_ptr, "bake_location", RNA_enum_get(op->ptr, "bake_location"));
}
/* set up path data for bones being calculated */
CTX_DATA_BEGIN (C, bPoseChannel *, pchan, selected_pose_bones_from_active_object) {
/* verify makes sure that the selected bone has a bone with the appropriate settings */
animviz_verify_motionpaths(op->reports, scene, ob, pchan);
}
CTX_DATA_END;
#ifdef DEBUG_TIME
TIMEIT_START(recalc_pose_paths);
#endif
/* calculate the bones that now have motionpaths... */
/* TODO: only make for the selected bones? */
ED_pose_recalculate_paths(C, scene, ob, POSE_PATH_CALC_RANGE_FULL);
#ifdef DEBUG_TIME
TIMEIT_END(recalc_pose_paths);
#endif
/* notifiers for updates */
WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
return OPERATOR_FINISHED;
}
void POSE_OT_paths_calculate(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Calculate Bone Paths";
ot->idname = "POSE_OT_paths_calculate";
ot->description = "Calculate paths for the selected bones";
/* api callbacks */
ot->invoke = pose_calculate_paths_invoke;
ot->exec = pose_calculate_paths_exec;
ot->poll = ED_operator_posemode_exclusive;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* properties */
RNA_def_int(ot->srna,
"start_frame",
1,
MINAFRAME,
MAXFRAME,
"Start",
"First frame to calculate bone paths on",
MINFRAME,
MAXFRAME / 2.0);
RNA_def_int(ot->srna,
"end_frame",
250,
MINAFRAME,
MAXFRAME,
"End",
"Last frame to calculate bone paths on",
MINFRAME,
MAXFRAME / 2.0);
RNA_def_enum(ot->srna,
"bake_location",
rna_enum_motionpath_bake_location_items,
MOTIONPATH_BAKE_HEADS,
"Bake Location",
"Which point on the bones is used when calculating paths");
}
/* --------- */
static bool pose_update_paths_poll(bContext *C)
{
if (ED_operator_posemode_exclusive(C)) {
Object *ob = CTX_data_active_object(C);
return (ob->pose->avs.path_bakeflag & MOTIONPATH_BAKE_HAS_PATHS) != 0;
}
return false;
}
static int pose_update_paths_exec(bContext *C, wmOperator *UNUSED(op))
{
Object *ob = BKE_object_pose_armature_get(CTX_data_active_object(C));
Scene *scene = CTX_data_scene(C);
if (ELEM(NULL, ob, scene)) {
return OPERATOR_CANCELLED;
}
/* calculate the bones that now have motionpaths... */
/* TODO: only make for the selected bones? */
ED_pose_recalculate_paths(C, scene, ob, POSE_PATH_CALC_RANGE_FULL);
/* notifiers for updates */
WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
return OPERATOR_FINISHED;
}
void POSE_OT_paths_update(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Update Bone Paths";
ot->idname = "POSE_OT_paths_update";
ot->description = "Recalculate paths for bones that already have them";
/* api callbacks */
ot->exec = pose_update_paths_exec;
ot->poll = pose_update_paths_poll;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/* --------- */
/* for the object with pose/action: clear path curves for selected bones only */
static void ED_pose_clear_paths(Object *ob, bool only_selected)
{
bPoseChannel *pchan;
bool skipped = false;
if (ELEM(NULL, ob, ob->pose)) {
return;
}
/* free the motionpath blocks for all bones - This is easier for users to quickly clear all */
for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
if (pchan->mpath) {
if ((only_selected == false) || ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED))) {
animviz_free_motionpath(pchan->mpath);
pchan->mpath = NULL;
}
else {
skipped = true;
}
}
}
/* if nothing was skipped, there should be no paths left! */
if (skipped == false) {
ob->pose->avs.path_bakeflag &= ~MOTIONPATH_BAKE_HAS_PATHS;
}
/* tag armature object for copy on write - so removed paths don't still show */
DEG_id_tag_update(&ob->id, ID_RECALC_COPY_ON_WRITE);
}
/* operator callback - wrapper for the backend function */
static int pose_clear_paths_exec(bContext *C, wmOperator *op)
{
Object *ob = BKE_object_pose_armature_get(CTX_data_active_object(C));
bool only_selected = RNA_boolean_get(op->ptr, "only_selected");
/* only continue if there's an object */
if (ELEM(NULL, ob, ob->pose)) {
return OPERATOR_CANCELLED;
}
/* use the backend function for this */
ED_pose_clear_paths(ob, only_selected);
/* notifiers for updates */
WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
return OPERATOR_FINISHED;
}
/* operator callback/wrapper */
static int pose_clear_paths_invoke(bContext *C, wmOperator *op, const wmEvent *evt)
{
if ((evt->shift) && !RNA_struct_property_is_set(op->ptr, "only_selected")) {
RNA_boolean_set(op->ptr, "only_selected", true);
}
return pose_clear_paths_exec(C, op);
}
void POSE_OT_paths_clear(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Clear Bone Paths";
ot->idname = "POSE_OT_paths_clear";
ot->description = "Clear path caches for all bones, hold Shift key for selected bones only";
/* api callbacks */
ot->invoke = pose_clear_paths_invoke;
ot->exec = pose_clear_paths_exec;
ot->poll = ED_operator_posemode_exclusive;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* properties */
ot->prop = RNA_def_boolean(
ot->srna, "only_selected", false, "Only Selected", "Only clear paths from selected bones");
RNA_def_property_flag(ot->prop, PROP_SKIP_SAVE);
}
/* --------- */
static int pose_update_paths_range_exec(bContext *C, wmOperator *UNUSED(op))
{
Scene *scene = CTX_data_scene(C);
Object *ob = BKE_object_pose_armature_get(CTX_data_active_object(C));
if (ELEM(NULL, scene, ob, ob->pose)) {
return OPERATOR_CANCELLED;
}
/* use Preview Range or Full Frame Range - whichever is in use */
ob->pose->avs.path_sf = PSFRA;
ob->pose->avs.path_ef = PEFRA;
/* tag for updates */
DEG_id_tag_update(&ob->id, ID_RECALC_COPY_ON_WRITE);
WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
return OPERATOR_FINISHED;
}
void POSE_OT_paths_range_update(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Update Range from Scene";
ot->idname = "POSE_OT_paths_range_update";
ot->description = "Update frame range for motion paths from the Scene's current frame range";
/* callbacks */
ot->exec = pose_update_paths_range_exec;
ot->poll = ED_operator_posemode_exclusive;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/* ********************************************** */
static int pose_flip_names_exec(bContext *C, wmOperator *op)
{
Main *bmain = CTX_data_main(C);
ViewLayer *view_layer = CTX_data_view_layer(C);
View3D *v3d = CTX_wm_view3d(C);
const bool do_strip_numbers = RNA_boolean_get(op->ptr, "do_strip_numbers");
FOREACH_OBJECT_IN_MODE_BEGIN (view_layer, v3d, OB_ARMATURE, OB_MODE_POSE, ob) {
bArmature *arm = ob->data;
ListBase bones_names = {NULL};
FOREACH_PCHAN_SELECTED_IN_OBJECT_BEGIN (ob, pchan) {
BLI_addtail(&bones_names, BLI_genericNodeN(pchan->name));
}
FOREACH_PCHAN_SELECTED_IN_OBJECT_END;
ED_armature_bones_flip_names(bmain, arm, &bones_names, do_strip_numbers);
BLI_freelistN(&bones_names);
/* since we renamed stuff... */
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
/* note, notifier might evolve */
WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
}
FOREACH_OBJECT_IN_MODE_END;
return OPERATOR_FINISHED;
}
void POSE_OT_flip_names(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Flip Names";
ot->idname = "POSE_OT_flip_names";
ot->description = "Flips (and corrects) the axis suffixes of the names of selected bones";
/* api callbacks */
ot->exec = pose_flip_names_exec;
ot->poll = ED_operator_posemode_local;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
RNA_def_boolean(ot->srna,
"do_strip_numbers",
false,
"Strip Numbers",
"Try to remove right-most dot-number from flipped names "
"(WARNING: may result in incoherent naming in some cases)");
}
/* ------------------ */
static int pose_autoside_names_exec(bContext *C, wmOperator *op)
{
Main *bmain = CTX_data_main(C);
char newname[MAXBONENAME];
short axis = RNA_enum_get(op->ptr, "axis");
Object *ob_prev = NULL;
/* loop through selected bones, auto-naming them */
CTX_DATA_BEGIN_WITH_ID (C, bPoseChannel *, pchan, selected_pose_bones, Object *, ob) {
bArmature *arm = ob->data;
BLI_strncpy(newname, pchan->name, sizeof(newname));
if (bone_autoside_name(newname, 1, axis, pchan->bone->head[axis], pchan->bone->tail[axis])) {
ED_armature_bone_rename(bmain, arm, pchan->name, newname);
}
if (ob_prev != ob) {
/* since we renamed stuff... */
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
/* note, notifier might evolve */
WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
ob_prev = ob;
}
}
CTX_DATA_END;
return OPERATOR_FINISHED;
}
void POSE_OT_autoside_names(wmOperatorType *ot)
{
static const EnumPropertyItem axis_items[] = {
{0, "XAXIS", 0, "X-Axis", "Left/Right"},
{1, "YAXIS", 0, "Y-Axis", "Front/Back"},
{2, "ZAXIS", 0, "Z-Axis", "Top/Bottom"},
{0, NULL, 0, NULL, NULL},
};
/* identifiers */
ot->name = "AutoName by Axis";
ot->idname = "POSE_OT_autoside_names";
ot->description =
"Automatically renames the selected bones according to which side of the target axis they "
"fall on";
/* api callbacks */
ot->invoke = WM_menu_invoke;
ot->exec = pose_autoside_names_exec;
ot->poll = ED_operator_posemode;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* settings */
ot->prop = RNA_def_enum(ot->srna, "axis", axis_items, 0, "Axis", "Axis tag names with");
}
/* ********************************************** */
static int pose_bone_rotmode_exec(bContext *C, wmOperator *op)
{
const int mode = RNA_enum_get(op->ptr, "type");
Object *prev_ob = NULL;
/* set rotation mode of selected bones */
CTX_DATA_BEGIN_WITH_ID (C, bPoseChannel *, pchan, selected_pose_bones, Object *, ob) {
/* use API Method for conversions... */
BKE_rotMode_change_values(
pchan->quat, pchan->eul, pchan->rotAxis, &pchan->rotAngle, pchan->rotmode, (short)mode);
/* finally, set the new rotation type */
pchan->rotmode = mode;
if (prev_ob != ob) {
/* Notifiers and updates. */
DEG_id_tag_update((ID *)ob, ID_RECALC_GEOMETRY);
WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, ob);
WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, ob);
prev_ob = ob;
}
}
CTX_DATA_END;
return OPERATOR_FINISHED;
}
void POSE_OT_rotation_mode_set(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Set Rotation Mode";
ot->idname = "POSE_OT_rotation_mode_set";
ot->description = "Set the rotation representation used by selected bones";
/* callbacks */
ot->invoke = WM_menu_invoke;
ot->exec = pose_bone_rotmode_exec;
ot->poll = ED_operator_posemode;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* properties */
ot->prop = RNA_def_enum(
ot->srna, "type", rna_enum_object_rotation_mode_items, 0, "Rotation Mode", "");
}
/* ********************************************** */
static bool armature_layers_poll(bContext *C)
{
/* Armature layers operators can be used in posemode OR editmode for armatures */
return ED_operator_posemode(C) || ED_operator_editarmature(C);
}
static bArmature *armature_layers_get_data(Object **ob)
{
bArmature *arm = NULL;
/* Sanity checking and handling of posemode. */
if (*ob) {
Object *tob = BKE_object_pose_armature_get(*ob);
if (tob) {
*ob = tob;
arm = (*ob)->data;
}
else if ((*ob)->type == OB_ARMATURE) {
arm = (*ob)->data;
}
}
return arm;
}
/* Show all armature layers */
static int pose_armature_layers_showall_exec(bContext *C, wmOperator *op)
{
Object *ob = CTX_data_active_object(C);
bArmature *arm = armature_layers_get_data(&ob);
PointerRNA ptr;
int maxLayers = (RNA_boolean_get(op->ptr, "all")) ? 32 : 16;
/* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
bool layers[32] = {false};
/* sanity checking */
if (arm == NULL) {
return OPERATOR_CANCELLED;
}
/* use RNA to set the layers
* although it would be faster to just set directly using bitflags, we still
* need to setup a RNA pointer so that we get the "update" callbacks for free...
*/
RNA_id_pointer_create(&arm->id, &ptr);
for (int i = 0; i < maxLayers; i++) {
layers[i] = 1;
}
RNA_boolean_set_array(&ptr, "layers", layers);
/* note, notifier might evolve */
WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
DEG_id_tag_update(&arm->id, ID_RECALC_COPY_ON_WRITE);
/* done */
return OPERATOR_FINISHED;
}
void ARMATURE_OT_layers_show_all(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Show All Layers";
ot->idname = "ARMATURE_OT_layers_show_all";
ot->description = "Make all armature layers visible";
/* callbacks */
ot->exec = pose_armature_layers_showall_exec;
ot->poll = armature_layers_poll;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* properties */
ot->prop = RNA_def_boolean(
ot->srna, "all", 1, "All Layers", "Enable all layers or just the first 16 (top row)");
}
/* ------------------- */
/* Present a popup to get the layers that should be used */
static int armature_layers_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
Object *ob = CTX_data_active_object(C);
bArmature *arm = armature_layers_get_data(&ob);
PointerRNA ptr;
/* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
bool layers[32];
/* sanity checking */
if (arm == NULL) {
return OPERATOR_CANCELLED;
}
/* Get RNA pointer to armature data to use that to retrieve the layers as ints
* to init the operator. */
RNA_id_pointer_create((ID *)arm, &ptr);
RNA_boolean_get_array(&ptr, "layers", layers);
RNA_boolean_set_array(op->ptr, "layers", layers);
/* part to sync with other similar operators... */
return WM_operator_props_popup(C, op, event);
}
/* Set the visible layers for the active armature (edit and pose modes) */
static int armature_layers_exec(bContext *C, wmOperator *op)
{
Object *ob = CTX_data_active_object(C);
bArmature *arm = armature_layers_get_data(&ob);
PointerRNA ptr;
/* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
bool layers[32];
if (arm == NULL) {
return OPERATOR_CANCELLED;
}
/* get the values set in the operator properties */
RNA_boolean_get_array(op->ptr, "layers", layers);
/* get pointer for armature, and write data there... */
RNA_id_pointer_create((ID *)arm, &ptr);
RNA_boolean_set_array(&ptr, "layers", layers);
/* note, notifier might evolve */
WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
DEG_id_tag_update(&arm->id, ID_RECALC_COPY_ON_WRITE);
return OPERATOR_FINISHED;
}
void ARMATURE_OT_armature_layers(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Change Armature Layers";
ot->idname = "ARMATURE_OT_armature_layers";
ot->description = "Change the visible armature layers";
/* callbacks */
ot->invoke = armature_layers_invoke;
ot->exec = armature_layers_exec;
ot->poll = armature_layers_poll;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* properties */
RNA_def_boolean_layer_member(
ot->srna, "layers", 32, NULL, "Layer", "Armature layers to make visible");
}
/* ------------------- */
/* Present a popup to get the layers that should be used */
static int pose_bone_layers_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
/* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
bool layers[32] = {0};
/* get layers that are active already */
CTX_DATA_BEGIN (C, bPoseChannel *, pchan, selected_pose_bones) {
short bit;
/* loop over the bits for this pchan's layers, adding layers where they're needed */
for (bit = 0; bit < 32; bit++) {
layers[bit] = (pchan->bone->layer & (1u << bit)) != 0;
}
}
CTX_DATA_END;
/* copy layers to operator */
RNA_boolean_set_array(op->ptr, "layers", layers);
/* part to sync with other similar operators... */
return WM_operator_props_popup(C, op, event);
}
/* Set the visible layers for the active armature (edit and pose modes) */
static int pose_bone_layers_exec(bContext *C, wmOperator *op)
{
PointerRNA ptr;
/* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
bool layers[32];
/* get the values set in the operator properties */
RNA_boolean_get_array(op->ptr, "layers", layers);
Object *prev_ob = NULL;
/* Make sure that the pose bone data is up to date.
* (May not always be the case after undo/redo e.g.).
*/
struct Main *bmain = CTX_data_main(C);
wmWindow *win = CTX_wm_window(C);
View3D *v3d = CTX_wm_view3d(C); /* This may be NULL in a lot of cases. */
ViewLayer *view_layer = WM_window_get_active_view_layer(win);
FOREACH_OBJECT_IN_MODE_BEGIN (view_layer, v3d, OB_ARMATURE, OB_MODE_POSE, ob_iter) {
bArmature *arm = ob_iter->data;
BKE_pose_ensure(bmain, ob_iter, arm, true);
}
FOREACH_OBJECT_IN_MODE_END;
/* set layers of pchans based on the values set in the operator props */
CTX_DATA_BEGIN_WITH_ID (C, bPoseChannel *, pchan, selected_pose_bones, Object *, ob) {
/* get pointer for pchan, and write flags this way */
RNA_pointer_create((ID *)ob->data, &RNA_Bone, pchan->bone, &ptr);
RNA_boolean_set_array(&ptr, "layers", layers);
if (prev_ob != ob) {
/* Note, notifier might evolve. */
WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
DEG_id_tag_update((ID *)ob->data, ID_RECALC_COPY_ON_WRITE);
prev_ob = ob;
}
}
CTX_DATA_END;
return OPERATOR_FINISHED;
}
void POSE_OT_bone_layers(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Change Bone Layers";
ot->idname = "POSE_OT_bone_layers";
ot->description = "Change the layers that the selected bones belong to";
/* callbacks */
ot->invoke = pose_bone_layers_invoke;
ot->exec = pose_bone_layers_exec;
ot->poll = ED_operator_posemode_exclusive;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* properties */
RNA_def_boolean_layer_member(
ot->srna, "layers", 32, NULL, "Layer", "Armature layers that bone belongs to");
}
/* ------------------- */
/* Present a popup to get the layers that should be used */
static int armature_bone_layers_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
/* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
bool layers[32] = {0};
/* get layers that are active already */
CTX_DATA_BEGIN (C, EditBone *, ebone, selected_editable_bones) {
short bit;
/* loop over the bits for this pchan's layers, adding layers where they're needed */
for (bit = 0; bit < 32; bit++) {
if (ebone->layer & (1u << bit)) {
layers[bit] = 1;
}
}
}
CTX_DATA_END;
/* copy layers to operator */
RNA_boolean_set_array(op->ptr, "layers", layers);
/* part to sync with other similar operators... */
return WM_operator_props_popup(C, op, event);
}
/* Set the visible layers for the active armature (edit and pose modes) */
static int armature_bone_layers_exec(bContext *C, wmOperator *op)
{
Object *ob = CTX_data_edit_object(C);
PointerRNA ptr;
/* hardcoded for now - we can only have 32 armature layers, so this should be fine... */
bool layers[32];
/* get the values set in the operator properties */
RNA_boolean_get_array(op->ptr, "layers", layers);
/* set layers of pchans based on the values set in the operator props */
CTX_DATA_BEGIN_WITH_ID (C, EditBone *, ebone, selected_editable_bones, bArmature *, arm) {
/* get pointer for pchan, and write flags this way */
RNA_pointer_create((ID *)arm, &RNA_EditBone, ebone, &ptr);
RNA_boolean_set_array(&ptr, "layers", layers);
}
CTX_DATA_END;
ED_armature_edit_refresh_layer_used(ob->data);
/* note, notifier might evolve */
WM_event_add_notifier(C, NC_OBJECT | ND_POSE, ob);
return OPERATOR_FINISHED;
}
void ARMATURE_OT_bone_layers(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Change Bone Layers";
ot->idname = "ARMATURE_OT_bone_layers";
ot->description = "Change the layers that the selected bones belong to";
/* callbacks */
ot->invoke = armature_bone_layers_invoke;
ot->exec = armature_bone_layers_exec;
ot->poll = ED_operator_editarmature;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* properties */
RNA_def_boolean_layer_member(
ot->srna, "layers", 32, NULL, "Layer", "Armature layers that bone belongs to");
}
/* ********************************************** */
/* Show/Hide Bones */
static int hide_pose_bone_fn(Object *ob, Bone *bone, void *ptr)
{
bArmature *arm = ob->data;
const bool hide_select = (bool)POINTER_AS_INT(ptr);
int count = 0;
if (arm->layer & bone->layer) {
if (((bone->flag & BONE_SELECTED) != 0) == hide_select) {
bone->flag |= BONE_HIDDEN_P;
/* only needed when 'hide_select' is true, but harmless. */
bone->flag &= ~BONE_SELECTED;
if (arm->act_bone == bone) {
arm->act_bone = NULL;
}
count += 1;
}
}
return count;
}
/* active object is armature in posemode, poll checked */
static int pose_hide_exec(bContext *C, wmOperator *op)
{
ViewLayer *view_layer = CTX_data_view_layer(C);
uint objects_len;
Object **objects = BKE_object_pose_array_get_unique(view_layer, CTX_wm_view3d(C), &objects_len);
bool changed_multi = false;
const int hide_select = !RNA_boolean_get(op->ptr, "unselected");
void *hide_select_p = POINTER_FROM_INT(hide_select);
for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
Object *ob_iter = objects[ob_index];
bArmature *arm = ob_iter->data;
if (ob_iter->proxy != NULL) {
BKE_report(op->reports, RPT_INFO, "Undo of hiding can only be done with Reveal Selected");
}
bool changed = bone_looper(ob_iter, arm->bonebase.first, hide_select_p, hide_pose_bone_fn) !=
0;
if (changed) {
changed_multi = true;
WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, ob_iter);
DEG_id_tag_update(&arm->id, ID_RECALC_COPY_ON_WRITE);
}
}
MEM_freeN(objects);
return changed_multi ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
}
void POSE_OT_hide(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Hide Selected";
ot->idname = "POSE_OT_hide";
ot->description = "Tag selected bones to not be visible in Pose Mode";
/* api callbacks */
ot->exec = pose_hide_exec;
ot->poll = ED_operator_posemode;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* props */
RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "");
}
static int show_pose_bone_cb(Object *ob, Bone *bone, void *data)
{
const bool select = POINTER_AS_INT(data);
bArmature *arm = ob->data;
int count = 0;
if (arm->layer & bone->layer) {
if (bone->flag & BONE_HIDDEN_P) {
if (!(bone->flag & BONE_UNSELECTABLE)) {
SET_FLAG_FROM_TEST(bone->flag, select, BONE_SELECTED);
}
bone->flag &= ~BONE_HIDDEN_P;
count += 1;
}
}
return count;
}
/* active object is armature in posemode, poll checked */
static int pose_reveal_exec(bContext *C, wmOperator *op)
{
ViewLayer *view_layer = CTX_data_view_layer(C);
uint objects_len;
Object **objects = BKE_object_pose_array_get_unique(view_layer, CTX_wm_view3d(C), &objects_len);
bool changed_multi = false;
const bool select = RNA_boolean_get(op->ptr, "select");
void *select_p = POINTER_FROM_INT(select);
for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
Object *ob_iter = objects[ob_index];
bArmature *arm = ob_iter->data;
bool changed = bone_looper(ob_iter, arm->bonebase.first, select_p, show_pose_bone_cb);
if (changed) {
changed_multi = true;
WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, ob_iter);
DEG_id_tag_update(&arm->id, ID_RECALC_COPY_ON_WRITE);
}
}
MEM_freeN(objects);
return changed_multi ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
}
void POSE_OT_reveal(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Reveal Selected";
ot->idname = "POSE_OT_reveal";
ot->description = "Reveal all bones hidden in Pose Mode";
/* api callbacks */
ot->exec = pose_reveal_exec;
ot->poll = ED_operator_posemode;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
RNA_def_boolean(ot->srna, "select", true, "Select", "");
}
/* ********************************************** */
/* Flip Quats */
static int pose_flip_quats_exec(bContext *C, wmOperator *UNUSED(op))
{
Scene *scene = CTX_data_scene(C);
KeyingSet *ks = ANIM_builtin_keyingset_get_named(NULL, ANIM_KS_LOC_ROT_SCALE_ID);
bool changed_multi = false;
ViewLayer *view_layer = CTX_data_view_layer(C);
View3D *v3d = CTX_wm_view3d(C);
FOREACH_OBJECT_IN_MODE_BEGIN (view_layer, v3d, OB_ARMATURE, OB_MODE_POSE, ob_iter) {
bool changed = false;
/* loop through all selected pchans, flipping and keying (as needed) */
FOREACH_PCHAN_SELECTED_IN_OBJECT_BEGIN (ob_iter, pchan) {
/* only if bone is using quaternion rotation */
if (pchan->rotmode == ROT_MODE_QUAT) {
changed = true;
/* quaternions have 720 degree range */
negate_v4(pchan->quat);
ED_autokeyframe_pchan(C, scene, ob_iter, pchan, ks);
}
}
FOREACH_PCHAN_SELECTED_IN_OBJECT_END;
if (changed) {
changed_multi = true;
/* notifiers and updates */
DEG_id_tag_update(&ob_iter->id, ID_RECALC_GEOMETRY);
WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, ob_iter);
}
}
FOREACH_OBJECT_IN_MODE_END;
return changed_multi ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
}
void POSE_OT_quaternions_flip(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Flip Quats";
ot->idname = "POSE_OT_quaternions_flip";
ot->description =
"Flip quaternion values to achieve desired rotations, while maintaining the same "
"orientations";
/* callbacks */
ot->exec = pose_flip_quats_exec;
ot->poll = ED_operator_posemode;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
|