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
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
|
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* 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.
*
* Contributor(s): Blender Foundation, 2002-2009 full recode.
*
* ***** END GPL LICENSE BLOCK *****
*
* Armature EditMode tools - transforms, chain based editing, and other settings
*/
/** \file blender/editors/armature/armature_edit.c
* \ingroup edarmature
*/
#include <assert.h>
#include "DNA_armature_types.h"
#include "DNA_constraint_types.h"
#include "DNA_object_types.h"
#include "DNA_scene_types.h"
#include "MEM_guardedalloc.h"
#include "BLT_translation.h"
#include "BLI_blenlib.h"
#include "BLI_math.h"
#include "BLI_ghash.h"
#include "BKE_action.h"
#include "BKE_armature.h"
#include "BKE_constraint.h"
#include "BKE_context.h"
#include "BKE_layer.h"
#include "BKE_global.h"
#include "BKE_report.h"
#include "BKE_object.h"
#include "RNA_access.h"
#include "RNA_define.h"
#include "WM_api.h"
#include "WM_types.h"
#include "ED_armature.h"
#include "ED_screen.h"
#include "ED_view3d.h"
#include "armature_intern.h"
/* ************************** Object Tools Exports ******************************* */
/* NOTE: these functions are exported to the Object module to be called from the tools there */
void ED_armature_transform_apply(Object *ob, float mat[4][4], const bool do_props)
{
bArmature *arm = ob->data;
/* Put the armature into editmode */
ED_armature_to_edit(arm);
/* Transform the bones */
ED_armature_transform_bones(arm, mat, do_props);
/* Turn the list into an armature */
ED_armature_from_edit(arm);
ED_armature_edit_free(arm);
}
void ED_armature_transform_bones(struct bArmature *arm, float mat[4][4], const bool do_props)
{
EditBone *ebone;
float scale = mat4_to_scale(mat); /* store the scale of the matrix here to use on envelopes */
float mat3[3][3];
copy_m3_m4(mat3, mat);
normalize_m3(mat3);
/* Do the rotations */
for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
float tmat[3][3];
/* find the current bone's roll matrix */
ED_armature_ebone_to_mat3(ebone, tmat);
/* transform the roll matrix */
mul_m3_m3m3(tmat, mat3, tmat);
/* transform the bone */
mul_m4_v3(mat, ebone->head);
mul_m4_v3(mat, ebone->tail);
/* apply the transformed roll back */
mat3_to_vec_roll(tmat, NULL, &ebone->roll);
if (do_props) {
ebone->rad_head *= scale;
ebone->rad_tail *= scale;
ebone->dist *= scale;
/* we could be smarter and scale by the matrix along the x & z axis */
ebone->xwidth *= scale;
ebone->zwidth *= scale;
}
}
}
void ED_armature_transform(struct bArmature *arm, float mat[4][4], const bool do_props)
{
if (arm->edbo) {
ED_armature_transform_bones(arm, mat, do_props);
}
else {
/* Put the armature into editmode */
ED_armature_to_edit(arm);
/* Transform the bones */
ED_armature_transform_bones(arm, mat, do_props);
/* Go back to object mode*/
ED_armature_from_edit(arm);
ED_armature_edit_free(arm);
}
}
/* exported for use in editors/object/ */
/* 0 == do center, 1 == center new, 2 == center cursor */
void ED_armature_origin_set(Object *ob, float cursor[3], int centermode, int around)
{
const bool is_editmode = BKE_object_is_in_editmode(ob);
EditBone *ebone;
bArmature *arm = ob->data;
float cent[3];
/* Put the armature into editmode */
if (is_editmode == false) {
ED_armature_to_edit(arm);
}
/* Find the centerpoint */
if (centermode == 2) {
copy_v3_v3(cent, cursor);
invert_m4_m4(ob->imat, ob->obmat);
mul_m4_v3(ob->imat, cent);
}
else {
if (around == V3D_AROUND_CENTER_MEAN) {
int total = 0;
zero_v3(cent);
for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
total += 2;
add_v3_v3(cent, ebone->head);
add_v3_v3(cent, ebone->tail);
}
if (total) {
mul_v3_fl(cent, 1.0f / (float)total);
}
}
else {
float min[3], max[3];
INIT_MINMAX(min, max);
for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
minmax_v3v3_v3(min, max, ebone->head);
minmax_v3v3_v3(min, max, ebone->tail);
}
mid_v3_v3v3(cent, min, max);
}
}
/* Do the adjustments */
for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
sub_v3_v3(ebone->head, cent);
sub_v3_v3(ebone->tail, cent);
}
/* Turn the list into an armature */
if (is_editmode == false) {
ED_armature_from_edit(arm);
ED_armature_edit_free(arm);
}
/* Adjust object location for new centerpoint */
if (centermode && (is_editmode == false)) {
mul_mat3_m4_v3(ob->obmat, cent); /* omit translation part */
add_v3_v3(ob->loc, cent);
}
}
/* ********************************* Roll ******************************* */
/* adjust bone roll to align Z axis with vector
* vec is in local space and is normalized
*/
float ED_armature_ebone_roll_to_vector(const EditBone *bone, const float align_axis[3], const bool axis_only)
{
float mat[3][3], nor[3];
float vec[3], align_axis_proj[3], roll = 0.0f;
BLI_ASSERT_UNIT_V3(align_axis);
sub_v3_v3v3(nor, bone->tail, bone->head);
/* If tail == head or the bone is aligned with the axis... */
if (normalize_v3(nor) <= FLT_EPSILON || (fabsf(dot_v3v3(align_axis, nor)) >= (1.0f - FLT_EPSILON))) {
return roll;
}
vec_roll_to_mat3_normalized(nor, 0.0f, mat);
/* project the new_up_axis along the normal */
project_v3_v3v3_normalized(vec, align_axis, nor);
sub_v3_v3v3(align_axis_proj, align_axis, vec);
if (axis_only) {
if (angle_v3v3(align_axis_proj, mat[2]) > (float)(M_PI_2)) {
negate_v3(align_axis_proj);
}
}
roll = angle_v3v3(align_axis_proj, mat[2]);
cross_v3_v3v3(vec, mat[2], align_axis_proj);
if (dot_v3v3(vec, nor) < 0.0f) {
return -roll;
}
return roll;
}
/* note, ranges arithmatic is used below */
typedef enum eCalcRollTypes {
/* pos */
CALC_ROLL_POS_X = 0,
CALC_ROLL_POS_Y,
CALC_ROLL_POS_Z,
CALC_ROLL_TAN_POS_X,
CALC_ROLL_TAN_POS_Z,
/* neg */
CALC_ROLL_NEG_X,
CALC_ROLL_NEG_Y,
CALC_ROLL_NEG_Z,
CALC_ROLL_TAN_NEG_X,
CALC_ROLL_TAN_NEG_Z,
/* no sign */
CALC_ROLL_ACTIVE,
CALC_ROLL_VIEW,
CALC_ROLL_CURSOR,
} eCalcRollTypes;
static const EnumPropertyItem prop_calc_roll_types[] = {
{0, "", 0, N_("Positive"), ""},
{CALC_ROLL_TAN_POS_X, "POS_X", 0, "Local +X Tangent", ""},
{CALC_ROLL_TAN_POS_Z, "POS_Z", 0, "Local +Z Tangent", ""},
{CALC_ROLL_POS_X, "GLOBAL_POS_X", 0, "Global +X Axis", ""},
{CALC_ROLL_POS_Y, "GLOBAL_POS_Y", 0, "Global +Y Axis", ""},
{CALC_ROLL_POS_Z, "GLOBAL_POS_Z", 0, "Global +Z Axis", ""},
{0, "", 0, N_("Negative"), ""},
{CALC_ROLL_TAN_NEG_X, "NEG_X", 0, "Local -X Tangent", ""},
{CALC_ROLL_TAN_NEG_Z, "NEG_Z", 0, "Local -Z Tangent", ""},
{CALC_ROLL_NEG_X, "GLOBAL_NEG_X", 0, "Global -X Axis", ""},
{CALC_ROLL_NEG_Y, "GLOBAL_NEG_Y", 0, "Global -Y Axis", ""},
{CALC_ROLL_NEG_Z, "GLOBAL_NEG_Z", 0, "Global -Z Axis", ""},
{0, "", 0, N_("Other"), ""},
{CALC_ROLL_ACTIVE, "ACTIVE", 0, "Active Bone", ""},
{CALC_ROLL_VIEW, "VIEW", 0, "View Axis", ""},
{CALC_ROLL_CURSOR, "CURSOR", 0, "Cursor", ""},
{0, NULL, 0, NULL, NULL}
};
static int armature_calc_roll_exec(bContext *C, wmOperator *op)
{
Object *ob = CTX_data_edit_object(C);
eCalcRollTypes type = RNA_enum_get(op->ptr, "type");
const bool axis_only = RNA_boolean_get(op->ptr, "axis_only");
/* axis_flip when matching the active bone never makes sense */
bool axis_flip = ((type >= CALC_ROLL_ACTIVE) ? RNA_boolean_get(op->ptr, "axis_flip") :
(type >= CALC_ROLL_TAN_NEG_X) ? true : false);
float imat[3][3];
bArmature *arm = ob->data;
EditBone *ebone;
if ((type >= CALC_ROLL_NEG_X) && (type <= CALC_ROLL_TAN_NEG_Z)) {
type -= (CALC_ROLL_ACTIVE - CALC_ROLL_NEG_X);
axis_flip = true;
}
copy_m3_m4(imat, ob->obmat);
invert_m3(imat);
if (type == CALC_ROLL_CURSOR) { /* Cursor */
Scene *scene = CTX_data_scene(C);
View3D *v3d = CTX_wm_view3d(C); /* can be NULL */
float cursor_local[3];
const View3DCursor *cursor = ED_view3d_cursor3d_get(scene, v3d);
invert_m4_m4(ob->imat, ob->obmat);
copy_v3_v3(cursor_local, cursor->location);
mul_m4_v3(ob->imat, cursor_local);
/* cursor */
for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
if (EBONE_VISIBLE(arm, ebone) && EBONE_EDITABLE(ebone)) {
float cursor_rel[3];
sub_v3_v3v3(cursor_rel, cursor_local, ebone->head);
if (axis_flip) negate_v3(cursor_rel);
if (normalize_v3(cursor_rel) != 0.0f) {
ebone->roll = ED_armature_ebone_roll_to_vector(ebone, cursor_rel, axis_only);
}
}
}
}
else if (ELEM(type, CALC_ROLL_TAN_POS_X, CALC_ROLL_TAN_POS_Z)) {
for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
if (ebone->parent) {
bool is_edit = (EBONE_VISIBLE(arm, ebone) && EBONE_EDITABLE(ebone));
bool is_edit_parent = (EBONE_VISIBLE(arm, ebone->parent) && EBONE_EDITABLE(ebone->parent));
if (is_edit || is_edit_parent) {
EditBone *ebone_other = ebone->parent;
float dir_a[3];
float dir_b[3];
float vec[3];
bool is_vec_zero;
sub_v3_v3v3(dir_a, ebone->tail, ebone->head);
normalize_v3(dir_a);
/* find the first bone in the chane with a different direction */
do {
sub_v3_v3v3(dir_b, ebone_other->head, ebone_other->tail);
normalize_v3(dir_b);
if (type == CALC_ROLL_TAN_POS_Z) {
cross_v3_v3v3(vec, dir_a, dir_b);
}
else {
add_v3_v3v3(vec, dir_a, dir_b);
}
} while ((is_vec_zero = (normalize_v3(vec) < 0.00001f)) &&
(ebone_other = ebone_other->parent));
if (!is_vec_zero) {
if (axis_flip) negate_v3(vec);
if (is_edit) {
ebone->roll = ED_armature_ebone_roll_to_vector(ebone, vec, axis_only);
}
/* parentless bones use cross product with child */
if (is_edit_parent) {
if (ebone->parent->parent == NULL) {
ebone->parent->roll = ED_armature_ebone_roll_to_vector(ebone->parent, vec, axis_only);
}
}
}
}
}
}
}
else {
float vec[3] = {0.0f, 0.0f, 0.0f};
if (type == CALC_ROLL_VIEW) { /* View */
RegionView3D *rv3d = CTX_wm_region_view3d(C);
if (rv3d == NULL) {
BKE_report(op->reports, RPT_ERROR, "No region view3d available");
return OPERATOR_CANCELLED;
}
copy_v3_v3(vec, rv3d->viewinv[2]);
mul_m3_v3(imat, vec);
}
else if (type == CALC_ROLL_ACTIVE) {
float mat[3][3];
ebone = (EditBone *)arm->act_edbone;
if (ebone == NULL) {
BKE_report(op->reports, RPT_ERROR, "No active bone set");
return OPERATOR_CANCELLED;
}
ED_armature_ebone_to_mat3(ebone, mat);
copy_v3_v3(vec, mat[2]);
}
else { /* Axis */
assert(type <= 5);
if (type < 3) vec[type] = 1.0f;
else vec[type - 2] = -1.0f;
mul_m3_v3(imat, vec);
normalize_v3(vec);
}
if (axis_flip) negate_v3(vec);
for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
if (EBONE_VISIBLE(arm, ebone) && EBONE_EDITABLE(ebone)) {
/* roll func is a callback which assumes that all is well */
ebone->roll = ED_armature_ebone_roll_to_vector(ebone, vec, axis_only);
}
}
}
if (arm->flag & ARM_MIRROR_EDIT) {
for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
if ((EBONE_VISIBLE(arm, ebone) && EBONE_EDITABLE(ebone)) == 0) {
EditBone *ebone_mirr = ED_armature_ebone_get_mirrored(arm->edbo, ebone);
if (ebone_mirr && (EBONE_VISIBLE(arm, ebone_mirr) && EBONE_EDITABLE(ebone_mirr))) {
ebone->roll = -ebone_mirr->roll;
}
}
}
}
/* note, notifier might evolve */
WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, ob);
return OPERATOR_FINISHED;
}
void ARMATURE_OT_calculate_roll(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Recalculate Roll";
ot->idname = "ARMATURE_OT_calculate_roll";
ot->description = "Automatically fix alignment of select bones' axes";
/* api callbacks */
ot->invoke = WM_menu_invoke;
ot->exec = armature_calc_roll_exec;
ot->poll = ED_operator_editarmature;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* properties */
ot->prop = RNA_def_enum(ot->srna, "type", prop_calc_roll_types, CALC_ROLL_TAN_POS_X, "Type", "");
RNA_def_boolean(ot->srna, "axis_flip", 0, "Flip Axis", "Negate the alignment axis");
RNA_def_boolean(ot->srna, "axis_only", 0, "Shortest Rotation", "Ignore the axis direction, use the shortest rotation to align");
}
static int armature_roll_clear_exec(bContext *C, wmOperator *op)
{
Object *ob = CTX_data_edit_object(C);
bArmature *arm = ob->data;
EditBone *ebone;
const float roll = RNA_float_get(op->ptr, "roll");
for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
if (EBONE_VISIBLE(arm, ebone) && EBONE_EDITABLE(ebone)) {
/* roll func is a callback which assumes that all is well */
ebone->roll = roll;
}
}
if (arm->flag & ARM_MIRROR_EDIT) {
for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
if ((EBONE_VISIBLE(arm, ebone) && EBONE_EDITABLE(ebone)) == 0) {
EditBone *ebone_mirr = ED_armature_ebone_get_mirrored(arm->edbo, ebone);
if (ebone_mirr && (EBONE_VISIBLE(arm, ebone_mirr) && EBONE_EDITABLE(ebone_mirr))) {
ebone->roll = -ebone_mirr->roll;
}
}
}
}
/* note, notifier might evolve */
WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, ob);
return OPERATOR_FINISHED;
}
void ARMATURE_OT_roll_clear(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Clear Roll";
ot->idname = "ARMATURE_OT_roll_clear";
ot->description = "Clear roll for selected bones";
/* api callbacks */
ot->exec = armature_roll_clear_exec;
ot->poll = ED_operator_editarmature;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
RNA_def_float_rotation(
ot->srna, "roll", 0, NULL, DEG2RADF(-360.0f), DEG2RADF(360.0f),
"Roll", "", DEG2RADF(-360.0f), DEG2RADF(360.0f));
}
/* ******************************** Chain-Based Tools ********************************* */
/* temporary data-structure for merge/fill bones */
typedef struct EditBonePoint {
struct EditBonePoint *next, *prev;
EditBone *head_owner; /* EditBone which uses this point as a 'head' point */
EditBone *tail_owner; /* EditBone which uses this point as a 'tail' point */
float vec[3]; /* the actual location of the point in local/EditMode space */
} EditBonePoint;
/* find chain-tips (i.e. bones without children) */
static void chains_find_tips(ListBase *edbo, ListBase *list)
{
EditBone *curBone, *ebo;
LinkData *ld;
/* note: this is potentially very slow ... there's got to be a better way */
for (curBone = edbo->first; curBone; curBone = curBone->next) {
short stop = 0;
/* is this bone contained within any existing chain? (skip if so) */
for (ld = list->first; ld; ld = ld->next) {
for (ebo = ld->data; ebo; ebo = ebo->parent) {
if (ebo == curBone) {
stop = 1;
break;
}
}
if (stop) break;
}
/* skip current bone if it is part of an existing chain */
if (stop) continue;
/* is any existing chain part of the chain formed by this bone? */
stop = 0;
for (ebo = curBone->parent; ebo; ebo = ebo->parent) {
for (ld = list->first; ld; ld = ld->next) {
if (ld->data == ebo) {
ld->data = curBone;
stop = 1;
break;
}
}
if (stop) break;
}
/* current bone has already been added to a chain? */
if (stop) continue;
/* add current bone to a new chain */
ld = MEM_callocN(sizeof(LinkData), "BoneChain");
ld->data = curBone;
BLI_addtail(list, ld);
}
}
/* --------------------- */
static void fill_add_joint(EditBone *ebo, short eb_tail, ListBase *points)
{
EditBonePoint *ebp;
float vec[3];
short found = 0;
if (eb_tail) {
copy_v3_v3(vec, ebo->tail);
}
else {
copy_v3_v3(vec, ebo->head);
}
for (ebp = points->first; ebp; ebp = ebp->next) {
if (equals_v3v3(ebp->vec, vec)) {
if (eb_tail) {
if ((ebp->head_owner) && (ebp->head_owner->parent == ebo)) {
/* so this bone's tail owner is this bone */
ebp->tail_owner = ebo;
found = 1;
break;
}
}
else {
if ((ebp->tail_owner) && (ebo->parent == ebp->tail_owner)) {
/* so this bone's head owner is this bone */
ebp->head_owner = ebo;
found = 1;
break;
}
}
}
}
/* allocate a new point if no existing point was related */
if (found == 0) {
ebp = MEM_callocN(sizeof(EditBonePoint), "EditBonePoint");
if (eb_tail) {
copy_v3_v3(ebp->vec, ebo->tail);
ebp->tail_owner = ebo;
}
else {
copy_v3_v3(ebp->vec, ebo->head);
ebp->head_owner = ebo;
}
BLI_addtail(points, ebp);
}
}
/* bone adding between selected joints */
static int armature_fill_bones_exec(bContext *C, wmOperator *op)
{
Object *obedit_active = CTX_data_edit_object(C);
Scene *scene = CTX_data_scene(C);
View3D *v3d = CTX_wm_view3d(C);
ListBase points = {NULL, NULL};
EditBone *newbone = NULL;
int count;
bool mixed_object_error = false;
/* sanity checks */
if (ELEM(NULL, obedit_active, obedit_active->data)) {
return OPERATOR_CANCELLED;
}
/* loop over all bones, and only consider if visible */
bArmature *arm = NULL;
CTX_DATA_BEGIN_WITH_ID(C, EditBone *, ebone, visible_bones, bArmature *, arm_iter)
{
bool check = false;
if (!(ebone->flag & BONE_CONNECTED) && (ebone->flag & BONE_ROOTSEL)) {
fill_add_joint(ebone, 0, &points);
check = true;
}
if (ebone->flag & BONE_TIPSEL) {
fill_add_joint(ebone, 1, &points);
check = true;
}
if (check) {
if (arm && (arm != arm_iter)) {
mixed_object_error = true;
}
arm = arm_iter;
}
}
CTX_DATA_END;
/* the number of joints determines how we fill:
* 1) between joint and cursor (joint=head, cursor=tail)
* 2) between the two joints (order is dependent on active-bone/hierarchy)
* 3+) error (a smarter method involving finding chains needs to be worked out
*/
count = BLI_listbase_count(&points);
if (count == 0) {
BKE_report(op->reports, RPT_ERROR, "No joints selected");
return OPERATOR_CANCELLED;
}
else if (mixed_object_error) {
BKE_report(op->reports, RPT_ERROR, "Bones for different objects selected");
BLI_freelistN(&points);
return OPERATOR_CANCELLED;
}
Object *obedit = NULL;
{
ViewLayer *view_layer = CTX_data_view_layer(C);
FOREACH_OBJECT_IN_MODE_BEGIN (view_layer, OB_MODE_EDIT, ob_iter) {
if (ob_iter->data == arm) {
obedit = ob_iter;
}
}
FOREACH_OBJECT_IN_MODE_END;
}
BLI_assert(obedit != NULL);
if (count == 1) {
EditBonePoint *ebp;
float curs[3];
/* Get Points - selected joint */
ebp = points.first;
/* Get points - cursor (tail) */
invert_m4_m4(obedit->imat, obedit->obmat);
mul_v3_m4v3(curs, obedit->imat, ED_view3d_cursor3d_get(scene, v3d)->location);
/* Create a bone */
newbone = add_points_bone(obedit, ebp->vec, curs);
}
else if (count == 2) {
EditBonePoint *ebp_a, *ebp_b;
float head[3], tail[3];
short headtail = 0;
/* check that the points don't belong to the same bone */
ebp_a = (EditBonePoint *)points.first;
ebp_b = ebp_a->next;
if (((ebp_a->head_owner == ebp_b->tail_owner) && (ebp_a->head_owner != NULL)) ||
((ebp_a->tail_owner == ebp_b->head_owner) && (ebp_a->tail_owner != NULL)))
{
BKE_report(op->reports, RPT_ERROR, "Same bone selected...");
BLI_freelistN(&points);
return OPERATOR_CANCELLED;
}
/* find which one should be the 'head' */
if ((ebp_a->head_owner && ebp_b->head_owner) || (ebp_a->tail_owner && ebp_b->tail_owner)) {
/* use active, nice predictable */
if (arm->act_edbone && ELEM(arm->act_edbone, ebp_a->head_owner, ebp_a->tail_owner)) {
headtail = 1;
}
else if (arm->act_edbone && ELEM(arm->act_edbone, ebp_b->head_owner, ebp_b->tail_owner)) {
headtail = 2;
}
else {
/* rule: whichever one is closer to 3d-cursor */
float curs[3];
float dist_sq_a, dist_sq_b;
/* get cursor location */
invert_m4_m4(obedit->imat, obedit->obmat);
mul_v3_m4v3(curs, obedit->imat, ED_view3d_cursor3d_get(scene, v3d)->location);
/* get distances */
dist_sq_a = len_squared_v3v3(ebp_a->vec, curs);
dist_sq_b = len_squared_v3v3(ebp_b->vec, curs);
/* compare distances - closer one therefore acts as direction for bone to go */
headtail = (dist_sq_a < dist_sq_b) ? 2 : 1;
}
}
else if (ebp_a->head_owner) {
headtail = 1;
}
else if (ebp_b->head_owner) {
headtail = 2;
}
/* assign head/tail combinations */
if (headtail == 2) {
copy_v3_v3(head, ebp_a->vec);
copy_v3_v3(tail, ebp_b->vec);
}
else if (headtail == 1) {
copy_v3_v3(head, ebp_b->vec);
copy_v3_v3(tail, ebp_a->vec);
}
/* add new bone and parent it to the appropriate end */
if (headtail) {
newbone = add_points_bone(obedit, head, tail);
/* do parenting (will need to set connected flag too) */
if (headtail == 2) {
/* ebp tail or head - tail gets priority */
if (ebp_a->tail_owner)
newbone->parent = ebp_a->tail_owner;
else
newbone->parent = ebp_a->head_owner;
}
else {
/* ebp_b tail or head - tail gets priority */
if (ebp_b->tail_owner)
newbone->parent = ebp_b->tail_owner;
else
newbone->parent = ebp_b->head_owner;
}
/* don't set for bone connecting two head points of bones */
if (ebp_a->tail_owner || ebp_b->tail_owner) {
newbone->flag |= BONE_CONNECTED;
}
}
}
else {
/* FIXME.. figure out a method for multiple bones */
BKE_reportf(op->reports, RPT_ERROR, "Too many points selected: %d", count);
BLI_freelistN(&points);
return OPERATOR_CANCELLED;
}
if (newbone) {
ED_armature_edit_deselect_all(obedit);
arm->act_edbone = newbone;
newbone->flag |= BONE_TIPSEL;
}
/* updates */
WM_event_add_notifier(C, NC_OBJECT | ND_POSE, obedit);
/* free points */
BLI_freelistN(&points);
return OPERATOR_FINISHED;
}
void ARMATURE_OT_fill(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Fill Between Joints";
ot->idname = "ARMATURE_OT_fill";
ot->description = "Add bone between selected joint(s) and/or 3D-Cursor";
/* callbacks */
ot->exec = armature_fill_bones_exec;
ot->poll = ED_operator_editarmature;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/* --------------------- */
/* this function merges between two bones, removes them and those in-between,
* and adjusts the parent relationships for those in-between
*/
static void bones_merge(Object *obedit, EditBone *start, EditBone *end, EditBone *endchild, ListBase *chains)
{
bArmature *arm = obedit->data;
EditBone *ebo, *ebone, *newbone;
LinkData *chain;
float head[3], tail[3];
/* check if same bone */
if (start == end) {
if (G.debug & G_DEBUG) {
printf("Error: same bone!\n");
printf("\tstart = %s, end = %s\n", start->name, end->name);
}
}
/* step 1: add a new bone
* - head = head/tail of start (default head)
* - tail = head/tail of end (default tail)
* - parent = parent of start
*/
if ((start->flag & BONE_TIPSEL) && (start->flag & BONE_SELECTED) == 0) {
copy_v3_v3(head, start->tail);
}
else {
copy_v3_v3(head, start->head);
}
if ((end->flag & BONE_ROOTSEL) && (end->flag & BONE_SELECTED) == 0) {
copy_v3_v3(tail, end->head);
}
else {
copy_v3_v3(tail, end->tail);
}
newbone = add_points_bone(obedit, head, tail);
newbone->parent = start->parent;
/* TODO, copy more things to the new bone */
newbone->flag = start->flag & (BONE_HINGE | BONE_NO_DEFORM | BONE_NO_SCALE |
BONE_NO_CYCLICOFFSET | BONE_NO_LOCAL_LOCATION | BONE_DONE);
/* step 2a: reparent any side chains which may be parented to any bone in the chain of bones to merge
* - potentially several tips for side chains leading to some tree exist...
*/
for (chain = chains->first; chain; chain = chain->next) {
/* traverse down chain until we hit the bottom or if we run into the tip of the chain of bones we're
* merging (need to stop in this case to avoid corrupting this chain too!)
*/
for (ebone = chain->data; (ebone) && (ebone != end); ebone = ebone->parent) {
short found = 0;
/* check if this bone is parented to one in the merging chain
* ! WATCHIT: must only go check until end of checking chain
*/
for (ebo = end; (ebo) && (ebo != start->parent); ebo = ebo->parent) {
/* side-chain found? --> remap parent to new bone, then we're done with this chain :) */
if (ebone->parent == ebo) {
ebone->parent = newbone;
found = 1;
break;
}
}
/* carry on to the next tip now */
if (found)
break;
}
}
/* step 2b: parent child of end to newbone (child from this chain) */
if (endchild)
endchild->parent = newbone;
/* step 3: delete all bones between and including start and end */
for (ebo = end; ebo; ebo = ebone) {
ebone = (ebo == start) ? (NULL) : (ebo->parent);
bone_free(arm, ebo);
}
newbone->flag |= (BONE_ROOTSEL | BONE_TIPSEL | BONE_SELECTED);
ED_armature_edit_sync_selection(arm->edbo);
}
static int armature_merge_exec(bContext *C, wmOperator *op)
{
Object *obedit = CTX_data_edit_object(C);
bArmature *arm = (obedit) ? obedit->data : NULL;
short type = RNA_enum_get(op->ptr, "type");
/* sanity checks */
if (ELEM(NULL, obedit, arm))
return OPERATOR_CANCELLED;
/* for now, there's only really one type of merging that's performed... */
if (type == 1) {
/* go down chains, merging bones */
ListBase chains = {NULL, NULL};
LinkData *chain, *nchain;
EditBone *ebo;
armature_tag_select_mirrored(arm);
/* get chains (ends on chains) */
chains_find_tips(arm->edbo, &chains);
if (BLI_listbase_is_empty(&chains)) return OPERATOR_CANCELLED;
/* each 'chain' is the last bone in the chain (with no children) */
for (chain = chains.first; chain; chain = nchain) {
EditBone *bstart = NULL, *bend = NULL;
EditBone *bchild = NULL, *child = NULL;
/* temporarily remove chain from list of chains */
nchain = chain->next;
BLI_remlink(&chains, chain);
/* only consider bones that are visible and selected */
for (ebo = chain->data; ebo; child = ebo, ebo = ebo->parent) {
/* check if visible + selected */
if (EBONE_VISIBLE(arm, ebo) &&
((ebo->flag & BONE_CONNECTED) || (ebo->parent == NULL)) &&
(ebo->flag & BONE_SELECTED) )
{
/* set either end or start (end gets priority, unless it is already set) */
if (bend == NULL) {
bend = ebo;
bchild = child;
}
else
bstart = ebo;
}
else {
/* chain is broken... merge any continous segments then clear */
if (bstart && bend)
bones_merge(obedit, bstart, bend, bchild, &chains);
bstart = NULL;
bend = NULL;
bchild = NULL;
}
}
/* merge from bstart to bend if something not merged */
if (bstart && bend)
bones_merge(obedit, bstart, bend, bchild, &chains);
/* put back link */
BLI_insertlinkbefore(&chains, nchain, chain);
}
armature_tag_unselect(arm);
BLI_freelistN(&chains);
}
/* updates */
ED_armature_edit_sync_selection(arm->edbo);
WM_event_add_notifier(C, NC_OBJECT | ND_POSE, obedit);
return OPERATOR_FINISHED;
}
void ARMATURE_OT_merge(wmOperatorType *ot)
{
static const EnumPropertyItem merge_types[] = {
{1, "WITHIN_CHAIN", 0, "Within Chains", ""},
{0, NULL, 0, NULL, NULL}
};
/* identifiers */
ot->name = "Merge Bones";
ot->idname = "ARMATURE_OT_merge";
ot->description = "Merge continuous chains of selected bones";
/* callbacks */
ot->invoke = WM_menu_invoke;
ot->exec = armature_merge_exec;
ot->poll = ED_operator_editarmature;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* properties */
ot->prop = RNA_def_enum(ot->srna, "type", merge_types, 0, "Type", "");
}
/* --------------------- */
/* Switch Direction operator:
* Currently, this does not use context loops, as context loops do not make it
* easy to retrieve any hierarchical/chain relationships which are necessary for
* this to be done easily.
*/
/* helper to clear BONE_TRANSFORM flags */
static void armature_clear_swap_done_flags(bArmature *arm)
{
EditBone *ebone;
for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
ebone->flag &= ~BONE_TRANSFORM;
}
}
static int armature_switch_direction_exec(bContext *C, wmOperator *UNUSED(op))
{
Object *ob = CTX_data_edit_object(C);
bArmature *arm = (bArmature *)ob->data;
ListBase chains = {NULL, NULL};
LinkData *chain;
/* get chains of bones (ends on chains) */
chains_find_tips(arm->edbo, &chains);
if (BLI_listbase_is_empty(&chains)) return OPERATOR_CANCELLED;
/* ensure that mirror bones will also be operated on */
armature_tag_select_mirrored(arm);
/* clear BONE_TRANSFORM flags
* - used to prevent duplicate/canceling operations from occurring [#34123]
* - BONE_DONE cannot be used here as that's already used for mirroring
*/
armature_clear_swap_done_flags(arm);
/* loop over chains, only considering selected and visible bones */
for (chain = chains.first; chain; chain = chain->next) {
EditBone *ebo, *child = NULL, *parent = NULL;
/* loop over bones in chain */
for (ebo = chain->data; ebo; ebo = parent) {
/* parent is this bone's original parent
* - we store this, as the next bone that is checked is this one
* but the value of ebo->parent may change here...
*/
parent = ebo->parent;
/* skip bone if already handled... [#34123] */
if ((ebo->flag & BONE_TRANSFORM) == 0) {
/* only if selected and editable */
if (EBONE_VISIBLE(arm, ebo) && EBONE_EDITABLE(ebo)) {
/* swap head and tail coordinates */
swap_v3_v3(ebo->head, ebo->tail);
/* do parent swapping:
* - use 'child' as new parent
* - connected flag is only set if points are coincidental
*/
ebo->parent = child;
if ((child) && equals_v3v3(ebo->head, child->tail))
ebo->flag |= BONE_CONNECTED;
else
ebo->flag &= ~BONE_CONNECTED;
/* get next bones
* - child will become the new parent of next bone
*/
child = ebo;
}
else {
/* not swapping this bone, however, if its 'parent' got swapped, unparent us from it
* as it will be facing in opposite direction
*/
if ((parent) && (EBONE_VISIBLE(arm, parent) && EBONE_EDITABLE(parent))) {
ebo->parent = NULL;
ebo->flag &= ~BONE_CONNECTED;
}
/* get next bones
* - child will become new parent of next bone (not swapping occurred,
* so set to NULL to prevent infinite-loop)
*/
child = NULL;
}
/* tag as done (to prevent double-swaps) */
ebo->flag |= BONE_TRANSFORM;
}
}
}
/* free chains */
BLI_freelistN(&chains);
/* clear temp flags */
armature_clear_swap_done_flags(arm);
armature_tag_unselect(arm);
/* note, notifier might evolve */
WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, ob);
return OPERATOR_FINISHED;
}
void ARMATURE_OT_switch_direction(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Switch Direction";
ot->idname = "ARMATURE_OT_switch_direction";
ot->description = "Change the direction that a chain of bones points in (head <-> tail swap)";
/* api callbacks */
ot->exec = armature_switch_direction_exec;
ot->poll = ED_operator_editarmature;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/* ********************************* Align ******************************* */
/* helper to fix a ebone position if its parent has moved due to alignment*/
static void fix_connected_bone(EditBone *ebone)
{
float diff[3];
if (!(ebone->parent) || !(ebone->flag & BONE_CONNECTED) || equals_v3v3(ebone->parent->tail, ebone->head))
return;
/* if the parent has moved we translate child's head and tail accordingly */
sub_v3_v3v3(diff, ebone->parent->tail, ebone->head);
add_v3_v3(ebone->head, diff);
add_v3_v3(ebone->tail, diff);
}
/* helper to recursively find chains of connected bones starting at ebone and fix their position */
static void fix_editbone_connected_children(ListBase *edbo, EditBone *ebone)
{
EditBone *selbone;
for (selbone = edbo->first; selbone; selbone = selbone->next) {
if ((selbone->parent) && (selbone->parent == ebone) && (selbone->flag & BONE_CONNECTED)) {
fix_connected_bone(selbone);
fix_editbone_connected_children(edbo, selbone);
}
}
}
static void bone_align_to_bone(ListBase *edbo, EditBone *selbone, EditBone *actbone)
{
float selboneaxis[3], actboneaxis[3], length;
sub_v3_v3v3(actboneaxis, actbone->tail, actbone->head);
normalize_v3(actboneaxis);
sub_v3_v3v3(selboneaxis, selbone->tail, selbone->head);
length = len_v3(selboneaxis);
mul_v3_fl(actboneaxis, length);
add_v3_v3v3(selbone->tail, selbone->head, actboneaxis);
selbone->roll = actbone->roll;
/* if the bone being aligned has connected descendants they must be moved
* according to their parent new position, otherwise they would be left
* in an inconsistent state: connected but away from the parent*/
fix_editbone_connected_children(edbo, selbone);
}
static int armature_align_bones_exec(bContext *C, wmOperator *op)
{
Object *ob = CTX_data_edit_object(C);
bArmature *arm = (bArmature *)ob->data;
EditBone *actbone = CTX_data_active_bone(C);
EditBone *actmirb = NULL;
int num_selected_bones;
/* there must be an active bone */
if (actbone == NULL) {
BKE_report(op->reports, RPT_ERROR, "Operation requires an active bone");
return OPERATOR_CANCELLED;
}
else if (arm->flag & ARM_MIRROR_EDIT) {
/* For X-Axis Mirror Editing option, we may need a mirror copy of actbone
* - if there's a mirrored copy of selbone, try to find a mirrored copy of actbone
* (i.e. selbone="child.L" and actbone="parent.L", find "child.R" and "parent.R").
* This is useful for arm-chains, for example parenting lower arm to upper arm
* - if there's no mirrored copy of actbone (i.e. actbone = "parent.C" or "parent")
* then just use actbone. Useful when doing upper arm to spine.
*/
actmirb = ED_armature_ebone_get_mirrored(arm->edbo, actbone);
if (actmirb == NULL)
actmirb = actbone;
}
/* if there is only 1 selected bone, we assume that that is the active bone,
* since a user will need to have clicked on a bone (thus selecting it) to make it active
*/
num_selected_bones = CTX_DATA_COUNT(C, selected_editable_bones);
if (num_selected_bones <= 1) {
/* When only the active bone is selected, and it has a parent,
* align it to the parent, as that is the only possible outcome.
*/
if (actbone->parent) {
bone_align_to_bone(arm->edbo, actbone, actbone->parent);
if ((arm->flag & ARM_MIRROR_EDIT) && (actmirb->parent))
bone_align_to_bone(arm->edbo, actmirb, actmirb->parent);
BKE_reportf(op->reports, RPT_INFO, "Aligned bone '%s' to parent", actbone->name);
}
}
else {
/* Align 'selected' bones to the active one
* - the context iterator contains both selected bones and their mirrored copies,
* so we assume that unselected bones are mirrored copies of some selected bone
* - since the active one (and/or its mirror) will also be selected, we also need
* to check that we are not trying to operate on them, since such an operation
* would cause errors
*/
/* align selected bones to the active one */
CTX_DATA_BEGIN(C, EditBone *, ebone, selected_editable_bones)
{
if (ELEM(ebone, actbone, actmirb) == 0) {
if (ebone->flag & BONE_SELECTED)
bone_align_to_bone(arm->edbo, ebone, actbone);
else
bone_align_to_bone(arm->edbo, ebone, actmirb);
}
}
CTX_DATA_END;
BKE_reportf(op->reports, RPT_INFO, "%d bones aligned to bone '%s'", num_selected_bones, actbone->name);
}
/* note, notifier might evolve */
WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, ob);
return OPERATOR_FINISHED;
}
void ARMATURE_OT_align(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Align Bones";
ot->idname = "ARMATURE_OT_align";
ot->description = "Align selected bones to the active bone (or to their parent)";
/* api callbacks */
ot->exec = armature_align_bones_exec;
ot->poll = ED_operator_editarmature;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/* ********************************* Split ******************************* */
static int armature_split_exec(bContext *C, wmOperator *UNUSED(op))
{
Object *ob = CTX_data_edit_object(C);
bArmature *arm = (bArmature *)ob->data;
EditBone *bone;
for (bone = arm->edbo->first; bone; bone = bone->next) {
if (bone->parent && (bone->flag & BONE_SELECTED) != (bone->parent->flag & BONE_SELECTED)) {
bone->parent = NULL;
bone->flag &= ~BONE_CONNECTED;
}
}
for (bone = arm->edbo->first; bone; bone = bone->next) {
ED_armature_ebone_select_set(bone, (bone->flag & BONE_SELECTED) != 0);
}
WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, ob);
return OPERATOR_FINISHED;
}
void ARMATURE_OT_split(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Split";
ot->idname = "ARMATURE_OT_split";
ot->description = "Split off selected bones from connected unselected bones";
/* api callbacks */
ot->exec = armature_split_exec;
ot->poll = ED_operator_editarmature;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/* ********************************* Delete ******************************* */
static bool armature_delete_ebone_cb(const char *bone_name, void *arm_p)
{
bArmature *arm = arm_p;
EditBone *ebone;
ebone = ED_armature_ebone_find_name(arm->edbo, bone_name);
return (ebone && (ebone->flag & BONE_SELECTED) && (arm->layer & ebone->layer));
}
/* previously delete_armature */
/* only editmode! */
static int armature_delete_selected_exec(bContext *C, wmOperator *UNUSED(op))
{
EditBone *curBone, *ebone_next;
bool changed_multi = false;
/* cancel if nothing selected */
if (CTX_DATA_COUNT(C, selected_bones) == 0)
return OPERATOR_CANCELLED;
ViewLayer *view_layer = CTX_data_view_layer(C);
uint objects_len = 0;
Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, &objects_len);
for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
Object *obedit = objects[ob_index];
bArmature *arm = obedit->data;
bool changed = false;
armature_select_mirrored(arm);
BKE_pose_channels_remove(obedit, armature_delete_ebone_cb, arm);
for (curBone = arm->edbo->first; curBone; curBone = ebone_next) {
ebone_next = curBone->next;
if (arm->layer & curBone->layer) {
if (curBone->flag & BONE_SELECTED) {
if (curBone == arm->act_edbone) arm->act_edbone = NULL;
ED_armature_ebone_remove(arm, curBone);
changed = true;
}
}
}
if (changed) {
changed_multi = true;
ED_armature_edit_sync_selection(arm->edbo);
BKE_pose_tag_recalc(CTX_data_main(C), obedit->pose);
WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, obedit);
}
}
if (!changed_multi) {
return OPERATOR_CANCELLED;
}
return OPERATOR_FINISHED;
}
void ARMATURE_OT_delete(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Delete Selected Bone(s)";
ot->idname = "ARMATURE_OT_delete";
ot->description = "Remove selected bones from the armature";
/* api callbacks */
ot->invoke = WM_operator_confirm;
ot->exec = armature_delete_selected_exec;
ot->poll = ED_operator_editarmature;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
static bool armature_dissolve_ebone_cb(const char *bone_name, void *arm_p)
{
bArmature *arm = arm_p;
EditBone *ebone;
ebone = ED_armature_ebone_find_name(arm->edbo, bone_name);
return (ebone && (ebone->flag & BONE_DONE));
}
static int armature_dissolve_selected_exec(bContext *C, wmOperator *UNUSED(op))
{
bArmature *arm;
EditBone *ebone, *ebone_next;
Object *obedit = CTX_data_edit_object(C);
bool changed = false;
/* store for mirror */
GHash *ebone_flag_orig = NULL;
int ebone_num = 0;
arm = obedit->data;
for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
ebone->temp.p = NULL;
ebone->flag &= ~BONE_DONE;
ebone_num++;
}
if (arm->flag & ARM_MIRROR_EDIT) {
GHashIterator gh_iter;
ebone_flag_orig = BLI_ghash_ptr_new_ex(__func__, ebone_num);
for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
union { int flag; void *p; } val = {0};
val.flag = ebone->flag;
BLI_ghash_insert(ebone_flag_orig, ebone, val.p);
}
armature_select_mirrored_ex(arm, BONE_SELECTED | BONE_ROOTSEL | BONE_TIPSEL);
GHASH_ITER (gh_iter, ebone_flag_orig) {
union { int flag; void *p; } *val_p = (void *)BLI_ghashIterator_getValue_p(&gh_iter);
ebone = BLI_ghashIterator_getKey(&gh_iter);
val_p->flag = ebone->flag & ~val_p->flag;
}
}
for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
if (ebone->parent && ebone->flag & BONE_CONNECTED) {
if (ebone->parent->temp.ebone == ebone->parent) {
/* ignore */
}
else if (ebone->parent->temp.ebone) {
/* set ignored */
ebone->parent->temp.ebone = ebone->parent;
}
else {
/* set child */
ebone->parent->temp.ebone = ebone;
}
}
}
/* cleanup multiple used bones */
for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
if (ebone->temp.ebone == ebone) {
ebone->temp.ebone = NULL;
}
}
for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
/* break connections for unseen bones */
if (((arm->layer & ebone->layer) &&
((ED_armature_ebone_selectflag_get(ebone) & (BONE_TIPSEL | BONE_SELECTED)))) == 0)
{
ebone->temp.ebone = NULL;
}
if (((arm->layer & ebone->layer) &&
((ED_armature_ebone_selectflag_get(ebone) & (BONE_ROOTSEL | BONE_SELECTED)))) == 0)
{
if (ebone->parent && (ebone->flag & BONE_CONNECTED)) {
ebone->parent->temp.ebone = NULL;
}
}
}
for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
if (ebone->parent &&
(ebone->parent->temp.ebone == ebone))
{
ebone->flag |= BONE_DONE;
}
}
BKE_pose_channels_remove(obedit, armature_dissolve_ebone_cb, arm);
for (ebone = arm->edbo->first; ebone; ebone = ebone_next) {
ebone_next = ebone->next;
if (ebone->flag & BONE_DONE) {
copy_v3_v3(ebone->parent->tail, ebone->tail);
ebone->parent->rad_tail = ebone->rad_tail;
SET_FLAG_FROM_TEST(ebone->parent->flag, ebone->flag & BONE_TIPSEL, BONE_TIPSEL);
ED_armature_ebone_remove_ex(arm, ebone, false);
changed = true;
}
}
if (changed) {
for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
if (ebone->parent &&
ebone->parent->temp.ebone &&
(ebone->flag & BONE_CONNECTED))
{
ebone->rad_head = ebone->parent->rad_tail;
}
}
if (arm->flag & ARM_MIRROR_EDIT) {
for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
union { int flag; void *p; } *val_p = (void *)BLI_ghash_lookup_p(ebone_flag_orig, ebone);
if (val_p && val_p->flag) {
ebone->flag &= ~val_p->flag;
}
}
}
}
if (arm->flag & ARM_MIRROR_EDIT) {
BLI_ghash_free(ebone_flag_orig, NULL, NULL);
}
if (!changed) {
return OPERATOR_CANCELLED;
}
ED_armature_edit_sync_selection(arm->edbo);
WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, obedit);
return OPERATOR_FINISHED;
}
void ARMATURE_OT_dissolve(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Dissolve Selected Bone(s)";
ot->idname = "ARMATURE_OT_dissolve";
ot->description = "Dissolve selected bones from the armature";
/* api callbacks */
ot->exec = armature_dissolve_selected_exec;
ot->poll = ED_operator_editarmature;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/* ********************************* Show/Hide ******************************* */
static int armature_hide_exec(bContext *C, wmOperator *op)
{
Object *obedit = CTX_data_edit_object(C);
bArmature *arm = obedit->data;
EditBone *ebone;
const int invert = RNA_boolean_get(op->ptr, "unselected") ? BONE_SELECTED : 0;
/* cancel if nothing selected */
if (CTX_DATA_COUNT(C, selected_bones) == 0)
return OPERATOR_CANCELLED;
for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
if (EBONE_VISIBLE(arm, ebone)) {
if ((ebone->flag & BONE_SELECTED) != invert) {
ebone->flag &= ~(BONE_TIPSEL | BONE_SELECTED | BONE_ROOTSEL);
ebone->flag |= BONE_HIDDEN_A;
}
}
}
ED_armature_edit_validate_active(arm);
ED_armature_edit_sync_selection(arm->edbo);
WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, obedit);
return OPERATOR_FINISHED;
}
void ARMATURE_OT_hide(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Hide Selected Bones";
ot->idname = "ARMATURE_OT_hide";
ot->description = "Tag selected bones to not be visible in Edit Mode";
/* api callbacks */
ot->exec = armature_hide_exec;
ot->poll = ED_operator_editarmature;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* props */
RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected");
}
static int armature_reveal_exec(bContext *C, wmOperator *op)
{
Object *obedit = CTX_data_edit_object(C);
bArmature *arm = obedit->data;
EditBone *ebone;
const bool select = RNA_boolean_get(op->ptr, "select");
for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
if (arm->layer & ebone->layer) {
if (ebone->flag & BONE_HIDDEN_A) {
if (!(ebone->flag & BONE_UNSELECTABLE)) {
SET_FLAG_FROM_TEST(ebone->flag, select, (BONE_TIPSEL | BONE_SELECTED | BONE_ROOTSEL));
}
ebone->flag &= ~BONE_HIDDEN_A;
}
}
}
ED_armature_edit_validate_active(arm);
ED_armature_edit_sync_selection(arm->edbo);
WM_event_add_notifier(C, NC_OBJECT | ND_BONE_SELECT, obedit);
return OPERATOR_FINISHED;
}
void ARMATURE_OT_reveal(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Reveal Bones";
ot->idname = "ARMATURE_OT_reveal";
ot->description = "Reveal all bones hidden in Edit Mode";
/* api callbacks */
ot->exec = armature_reveal_exec;
ot->poll = ED_operator_editarmature;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
RNA_def_boolean(ot->srna, "select", true, "Select", "");
}
|