Blender V5.3
armature_add.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
9
10#include "DNA_anim_types.h"
11#include "DNA_armature_types.h"
13#include "DNA_object_types.h"
14#include "DNA_scene_types.h"
15
16#include "MEM_guardedalloc.h"
17
18#include "BLI_listbase.hh"
19#include "BLI_map.hh"
20#include "BLI_math_matrix_c.hh"
22#include "BLI_math_vector.hh"
23#include "BLI_math_vector_c.hh"
24#include "BLI_string.hh"
25#include "BLI_string_utf8.hh"
26#include "BLI_string_utils.hh"
27
28#include "BLT_translation.hh"
29
30#include "BKE_action.hh"
31#include "BKE_armature.hh"
32#include "BKE_constraint.h"
33#include "BKE_context.hh"
34#include "BKE_fcurve.hh"
35#include "BKE_idprop.hh"
36#include "BKE_layer.hh"
37#include "BKE_lib_id.hh"
38#include "BKE_object_types.hh"
39#include "BKE_report.hh"
40
41#include "ANIM_action.hh"
42
43#include "RNA_access.hh"
44#include "RNA_define.hh"
45
46#include "WM_api.hh"
47#include "WM_types.hh"
48
49#include "ED_armature.hh"
50#include "ED_outliner.hh"
51#include "ED_screen.hh"
52#include "ED_view3d.hh"
53
54#include "ANIM_armature.hh"
56
57#include "DEG_depsgraph.hh"
58
59#include "armature_intern.hh"
60
61namespace blender {
62
63/* *************** Adding stuff in editmode *************** */
64
66{
67 EditBone *bone = MEM_new<EditBone>("eBone");
68
69 STRNCPY_UTF8(bone->name, name);
70 ED_armature_ebone_unique_name(arm->edbo, bone->name, nullptr);
71
72 BLI_addtail(arm->edbo, bone);
73
74 bone->flag |= BONE_TIPSEL;
76 bone->weight = 1.0f;
77 bone->dist = 0.25f;
78 bone->xwidth = 0.1f;
79 bone->zwidth = 0.1f;
80 bone->rad_head = 0.10f;
81 bone->rad_tail = 0.05f;
82 bone->segments = 1;
83
84 /* Bendy-Bone parameters */
85 bone->roll1 = 0.0f;
86 bone->roll2 = 0.0f;
87 bone->curve_in_x = 0.0f;
88 bone->curve_in_z = 0.0f;
89 bone->curve_out_x = 0.0f;
90 bone->curve_out_z = 0.0f;
91 bone->ease1 = 1.0f;
92 bone->ease2 = 1.0f;
93
94 /* Prevent custom bone colors from having alpha zero.
95 * Part of the fix for issue #115434. */
96 bone->color.custom.solid[3] = 255;
97 bone->color.custom.select[3] = 255;
98 bone->color.custom.active[3] = 255;
99
100 copy_v3_fl(bone->scale_in, 1.0f);
101 copy_v3_fl(bone->scale_out, 1.0f);
102
103 return bone;
104}
105
107 const float length,
108 const bool view_aligned)
109{
110 bArmature *arm = id_cast<bArmature *>(obedit_arm->data);
111 EditBone *bone;
112
114
115 /* Create a bone */
117
118 arm->act_edbone = bone;
119
120 zero_v3(bone->head);
121 zero_v3(bone->tail);
122
123 bone->tail[view_aligned ? 1 : 2] = length;
124
125 if (arm->runtime->active_collection) {
127 }
128
129 return bone;
130}
131
141{
142 bArmature *arm;
143 EditBone *ebone, *newbone, *flipbone;
144 float mat[3][3], imat[3][3];
145 int a, to_root = 0;
146 Object *obedit;
147 Scene *scene;
148
149 scene = CTX_data_scene(C);
150 obedit = CTX_data_edit_object(C);
151 arm = id_cast<bArmature *>(obedit->data);
152
153 /* find the active or selected bone */
154 for (ebone = arm->edbo->first(); ebone; ebone = ebone->next) {
155 if (!animrig::bone_is_visible(arm, ebone)) {
156 continue;
157 }
158 if (ebone->flag & BONE_TIPSEL || arm->act_edbone == ebone) {
159 break;
160 }
161 }
162
163 if (ebone == nullptr) {
164 for (ebone = arm->edbo->first(); ebone; ebone = ebone->next) {
165 if (!animrig::bone_is_visible(arm, ebone)) {
166 continue;
167 }
168 if (ebone->flag & BONE_ROOTSEL || arm->act_edbone == ebone) {
169 break;
170 }
171 }
172 if (ebone == nullptr) {
173 return OPERATOR_CANCELLED;
174 }
175
176 to_root = 1;
177 }
178
180
181 /* we re-use code for mirror editing... */
182 flipbone = nullptr;
183 if (arm->flag & ARM_MIRROR_EDIT) {
184 flipbone = ED_armature_ebone_get_mirrored(arm->edbo, ebone);
185 }
186
187 for (a = 0; a < 2; a++) {
188 if (a == 1) {
189 if (flipbone == nullptr) {
190 break;
191 }
192 std::swap(flipbone, ebone);
193 }
194
195 newbone = ED_armature_ebone_add(arm, ebone->name);
196 arm->act_edbone = newbone;
197 newbone->xwidth = ebone->xwidth;
198 newbone->zwidth = ebone->zwidth;
199
200 if (to_root) {
201 copy_v3_v3(newbone->head, ebone->head);
202 newbone->rad_head = ebone->rad_tail;
203 newbone->parent = ebone->parent;
204 }
205 else {
206 copy_v3_v3(newbone->head, ebone->tail);
207 newbone->rad_head = ebone->rad_tail;
208 newbone->parent = ebone;
209 newbone->flag |= BONE_CONNECTED;
210 }
211
212 const View3DCursor *curs = &scene->cursor;
213 copy_v3_v3(newbone->tail, curs->location);
214 sub_v3_v3v3(newbone->tail, newbone->tail, obedit->object_to_world().location());
215
216 if (a == 1) {
217 newbone->tail[0] = -newbone->tail[0];
218 }
219
220 copy_m3_m4(mat, obedit->object_to_world().ptr());
221 invert_m3_m3(imat, mat);
222 mul_m3_v3(imat, newbone->tail);
223
224 newbone->length = len_v3v3(newbone->head, newbone->tail);
225 newbone->rad_tail = newbone->length * 0.05f;
226 newbone->dist = newbone->length * 0.25f;
227
228 /* Calculate the new bone roll:
229 * Ensure the Z-axis of the newly-created bone matches the Z-axis of the parent bone.
230 * The roll_to_vector operator can then take care of the bone roll angle. */
231 float parent_y[3];
232 sub_v3_v3v3(parent_y, ebone->tail, ebone->head);
233 normalize_v3(parent_y);
234 float parent_mat[3][3];
235 vec_roll_to_mat3_normalized(parent_y, ebone->roll, parent_mat);
236 float align_axis[3];
237 copy_v3_v3(align_axis, parent_mat[2]);
238 newbone->roll = ED_armature_ebone_roll_to_vector(newbone, align_axis, false);
239
240 /* Copy bone collection membership. */
242 }
243
245
249
250 return OPERATOR_FINISHED;
251}
252
254 wmOperator *op,
255 const wmEvent *event)
256{
257 /* TODO: most of this code is copied from set3dcursor_invoke,
258 * it would be better to reuse code in set3dcursor_invoke */
259
260 /* temporarily change 3d cursor position */
261 Scene *scene;
262 ARegion *region;
263 View3D *v3d;
264 float tvec[3], oldcurs[3], mval_f[2];
265
266 scene = CTX_data_scene(C);
267 region = CTX_wm_region(C);
268 v3d = CTX_wm_view3d(C);
269
270 View3DCursor *cursor = &scene->cursor;
271
272 copy_v3_v3(oldcurs, cursor->location);
273
274 copy_v2fl_v2i(mval_f, event->mval);
275 ED_view3d_win_to_3d(v3d, region, cursor->location, mval_f, tvec);
276 copy_v3_v3(cursor->location, tvec);
277
278 /* extrude to the where new cursor is and store the operation result */
280
281 /* restore previous 3d cursor position */
282 copy_v3_v3(cursor->location, oldcurs);
283
284 /* Support dragging to move after extrude, see: #114282. */
285 if (retval & OPERATOR_FINISHED) {
286 retval |= OPERATOR_PASS_THROUGH;
287 }
289}
290
292{
293 /* identifiers */
294 ot->name = "Extrude to Cursor";
295 ot->idname = "ARMATURE_OT_click_extrude";
296 ot->description = "Create a new bone going from the last selected joint to the mouse position";
297
298 /* API callbacks. */
302
303 /* flags */
305
306 /* props */
307}
308
309EditBone *add_points_bone(Object *obedit, float head[3], float tail[3])
310{
311 EditBone *ebo;
312
313 ebo = ED_armature_ebone_add(id_cast<bArmature *>(obedit->data),
315
316 copy_v3_v3(ebo->head, head);
317 copy_v3_v3(ebo->tail, tail);
318
319 return ebo;
320}
321
322static void pre_edit_bone_duplicate(ListBaseT<EditBone> *editbones)
323{
324 /* clear temp */
326}
327
333 const Map<StringRefNull, StringRefNull> &name_map,
334 bPoseChannel *pchan_src)
335{
336 bPoseChannel *pchan_dst = nullptr;
337 const char *name_src = pchan_src->name;
338 const StringRefNull name_dst = name_map.lookup_default(name_src, "");
339 if (!name_dst.is_empty()) {
340 pchan_dst = BKE_pose_channel_find_name(pose, name_dst.c_str());
341 }
342
343 if (pchan_dst == nullptr) {
344 pchan_dst = pchan_src;
345 }
346
347 return pchan_dst;
348}
349
350static void post_edit_bone_duplicate(ListBaseT<EditBone> *editbones, Object *ob)
351{
352 if (ob->pose == nullptr) {
353 return;
354 }
355
358
360
361 for (EditBone &ebone_src : *editbones) {
362 EditBone *ebone_dst = ebone_src.temp.ebone;
363 if (!ebone_dst) {
364 ebone_dst = ED_armature_ebone_get_mirrored(editbones, &ebone_src);
365 }
366
367 if (ebone_dst) {
368 name_map.add_as(ebone_src.name, ebone_dst->name);
369 }
370 }
371
372 for (EditBone &ebone_src : *editbones) {
373 EditBone *ebone_dst = ebone_src.temp.ebone;
374 if (!ebone_dst) {
375 continue;
376 }
377
378 bPoseChannel *pchan_src = BKE_pose_channel_find_name(ob->pose, ebone_src.name);
379 if (!pchan_src) {
380 continue;
381 }
382
383 bPoseChannel *pchan_dst = BKE_pose_channel_find_name(ob->pose, ebone_dst->name);
384 if (!pchan_dst) {
385 continue;
386 }
387
388 if (pchan_src->custom_tx) {
389 pchan_dst->custom_tx = pchan_duplicate_map(ob->pose, name_map, pchan_src->custom_tx);
390 }
391 if (pchan_src->bbone_prev) {
392 pchan_dst->bbone_prev = pchan_duplicate_map(ob->pose, name_map, pchan_src->bbone_prev);
393 }
394 if (pchan_src->bbone_next) {
395 pchan_dst->bbone_next = pchan_duplicate_map(ob->pose, name_map, pchan_src->bbone_next);
396 }
397 }
398}
399
401 Object *ob,
402 const bool lookup_mirror_subtarget)
403{
404 /* If an edit bone has been duplicated, lets update its constraints if the
405 * subtarget they point to has also been duplicated.
406 */
407 bPoseChannel *pchan = BKE_pose_channel_ensure(ob->pose, dup_bone->name);
408
409 if (!pchan) {
410 return;
411 }
412
413 EditBone *oldtarget, *newtarget;
414 ListBaseT<bConstraint> *conlist = &pchan->constraints;
415 char name_flipped[MAX_ID_NAME - 2];
416 for (bConstraint &curcon : *conlist) {
417 /* does this constraint have a subtarget in
418 * this armature?
419 */
420 ListBaseT<bConstraintTarget> targets = {nullptr, nullptr};
421
422 if (!BKE_constraint_targets_get(&curcon, &targets)) {
423 continue;
424 }
425 for (bConstraintTarget &ct : targets) {
426 if (!ct.tar || !ct.subtarget[0]) {
427 continue;
428 }
429 Object *target_ob = ct.tar;
430 if (target_ob->type != OB_ARMATURE || !target_ob->data) {
431 /* Can only mirror armature. */
432 continue;
433 }
434 bArmature *target_armature = id_cast<bArmature *>(target_ob->data);
435 /* Was the subtarget bone duplicated too? If
436 * so, update the constraint to point at the
437 * duplicate of the old subtarget.
438 */
439
440 /* TODO: support updating sub-targets for multi-object edit mode.
441 * This requires all objects bones to be duplicated before this runs. */
442 oldtarget = (ob == target_ob) ?
443 ED_armature_ebone_find_name(target_armature->edbo, ct.subtarget) :
444 nullptr;
445 if (oldtarget && oldtarget->temp.ebone) {
446 newtarget = oldtarget->temp.ebone;
447 STRNCPY_UTF8(ct.subtarget, newtarget->name);
448 }
449 else if (lookup_mirror_subtarget) {
450 BLI_string_flip_side_name(name_flipped, ct.subtarget, false, sizeof(name_flipped));
451 if (bPoseChannel *flipped_bone = BKE_pose_channel_find_name(ct.tar->pose, name_flipped)) {
452 STRNCPY_UTF8(ct.subtarget, flipped_bone->name);
453 }
454 }
455 }
456 BKE_constraint_targets_flush(&curcon, &targets, false);
457 }
458}
459
461 EditBone *dup_bone, EditBone *orig_bone, Object *ob, bPoseChannel *pchan, bConstraint *curcon)
462{
463 bActionConstraint *act_con = static_cast<bActionConstraint *>(curcon->data);
464
465 float mat[4][4];
466
467 bConstraintOb cob{};
468 cob.depsgraph = nullptr;
469 cob.scene = nullptr;
470 cob.ob = ob;
471 cob.pchan = pchan;
473
474 unit_m4(mat);
475 bPoseChannel *target_pchan = BKE_pose_channel_find_name(ob->pose, act_con->subtarget);
477 ob, target_pchan, &cob, mat, curcon->tarspace, CONSTRAINT_SPACE_LOCAL, false);
478
479 float max_axis_val = 0;
480 int max_axis = 0;
481 /* Which axis represents X now. IE, which axis defines the mirror plane. */
482 for (int i = 0; i < 3; i++) {
483 float cur_val = fabsf(mat[0][i]);
484 if (cur_val > max_axis_val) {
485 max_axis = i;
486 max_axis_val = cur_val;
487 }
488 }
489
490 /* data->type is mapped as follows for backwards compatibility:
491 * 00,01,02 - rotation (it used to be like this)
492 * 10,11,12 - scaling
493 * 20,21,22 - location
494 */
495 /* Mirror the target range */
496 if (act_con->type < 10 && act_con->type != max_axis) {
497 /* Y or Z rotation */
498 act_con->min = -act_con->min;
499 act_con->max = -act_con->max;
500 }
501 else if (act_con->type == max_axis + 10) {
502 /* X scaling */
503 }
504 else if (act_con->type == max_axis + 20) {
505 /* X location */
506 float imat[4][4];
507
508 invert_m4_m4(imat, mat);
509
510 float min_vec[3], max_vec[3];
511
512 zero_v3(min_vec);
513 zero_v3(max_vec);
514
515 min_vec[0] = act_con->min;
516 max_vec[0] = act_con->max;
517
518 /* convert values into local object space */
519 mul_m4_v3(mat, min_vec);
520 mul_m4_v3(mat, max_vec);
521
522 min_vec[0] *= -1;
523 max_vec[0] *= -1;
524
525 /* convert back to the settings space */
526 mul_m4_v3(imat, min_vec);
527 mul_m4_v3(imat, max_vec);
528
529 act_con->min = min_vec[0];
530 act_con->max = max_vec[0];
531 }
532
533 /* See if there is any channels that uses this bone */
534 bAction *act = static_cast<bAction *>(act_con->act);
535 if (act) {
536 animrig::Action &action = act->wrap();
538 act_con->action_slot_handle);
539 Vector<const FCurve *> fcurves = {};
540 if (cbag) {
541 for (const FCurve *fcurve : cbag->fcurves()) {
542 if (animrig::fcurve_matches_collection_path(*fcurve, "pose.bones[", orig_bone->name)) {
543 fcurves.append(fcurve);
544 }
545 }
546 }
547 for (const FCurve *old_fcurve : fcurves) {
548 const char *new_path = BLI_string_replaceN(
549 old_fcurve->rna_path().c_str(), orig_bone->name, dup_bone->name);
550 FCurve &new_curve = cbag->fcurve_clone(
551 *old_fcurve, new_path, old_fcurve->array_index, dup_bone->name);
552 MEM_delete(new_path);
553 /* Flip the animation */
554 const StringRefNull rna_path = new_curve.rna_path();
555 const size_t slength = rna_path.size();
556 int i;
557 BezTriple *bezt;
558 for (i = 0, bezt = new_curve.bezt; i < new_curve.totvert; i++, bezt++) {
559 bool flip = false;
560 if (BLI_strn_endswith(rna_path.c_str(), "location", slength) && new_curve.array_index == 0)
561 {
562 flip = true;
563 }
564 else if (BLI_strn_endswith(rna_path.c_str(), "rotation_quaternion", slength) &&
565 ELEM(new_curve.array_index, 2, 3))
566 {
567 flip = true;
568 }
569 else if (BLI_strn_endswith(rna_path.c_str(), "rotation_euler", slength) &&
570 ELEM(new_curve.array_index, 1, 2))
571 {
572 flip = true;
573 }
574 else if (BLI_strn_endswith(rna_path.c_str(), "rotation_axis_angle", slength) &&
575 ELEM(new_curve.array_index, 2, 3))
576 {
577 flip = true;
578 }
579
580 if (flip) {
581 bezt->vec[0][1] *= -1;
582 bezt->vec[1][1] *= -1;
583 bezt->vec[2][1] *= -1;
584 }
585 }
586 }
587 }
588
589 /* Make depsgraph aware of our changes. */
591}
592
594{
595 /* IK constraint */
596 bKinematicConstraint *ik = static_cast<bKinematicConstraint *>(curcon->data);
597 ik->poleangle = -M_PI - ik->poleangle;
598 /* Wrap the angle to the +/-180.0f range (default soft limit of the input boxes). */
600}
601
603 bPoseChannel *pchan,
604 bConstraint *curcon)
605{
606 /* This code assumes that bRotLimitConstraint and bLocLimitConstraint have the same fields in
607 * the same memory locations. */
608 bLocLimitConstraint *limit = static_cast<bLocLimitConstraint *>(curcon->data);
609 float local_mat[4][4], imat[4][4];
610
611 float min_vec[3], max_vec[3];
612
613 min_vec[0] = limit->xmin;
614 min_vec[1] = limit->ymin;
615 min_vec[2] = limit->zmin;
616
617 max_vec[0] = limit->xmax;
618 max_vec[1] = limit->ymax;
619 max_vec[2] = limit->zmax;
620
621 unit_m4(local_mat);
622
623 bConstraintOb cob{};
624 cob.depsgraph = nullptr;
625 cob.scene = nullptr;
626 cob.ob = ob;
627 cob.pchan = pchan;
629
631 ob, pchan, &cob, local_mat, curcon->ownspace, CONSTRAINT_SPACE_LOCAL, false);
632
633 if (curcon->type == CONSTRAINT_TYPE_ROTLIMIT) {
634 /* Zero out any location translation */
635 local_mat[3][0] = local_mat[3][1] = local_mat[3][2] = 0;
636 }
637
638 invert_m4_m4(imat, local_mat);
639 /* convert values into local object space */
640 mul_m4_v3(local_mat, min_vec);
641 mul_m4_v3(local_mat, max_vec);
642
643 if (curcon->type == CONSTRAINT_TYPE_ROTLIMIT) {
644 float min_copy[3];
645
646 copy_v3_v3(min_copy, min_vec);
647
648 min_vec[1] = max_vec[1] * -1;
649 min_vec[2] = max_vec[2] * -1;
650
651 max_vec[1] = min_copy[1] * -1;
652 max_vec[2] = min_copy[2] * -1;
653 }
654 else {
655 float min_x_copy = min_vec[0];
656
657 min_vec[0] = max_vec[0] * -1;
658 max_vec[0] = min_x_copy * -1;
659
660 /* Also flip the enabled axis check-boxes accordingly. */
661 if (bool(limit->flag & LIMIT_XMIN) != bool(limit->flag & LIMIT_XMAX)) {
662 limit->flag ^= (LIMIT_XMIN | LIMIT_XMAX);
663 }
664 }
665
666 /* convert back to the settings space */
667 mul_m4_v3(imat, min_vec);
668 mul_m4_v3(imat, max_vec);
669
670 limit->xmin = min_vec[0];
671 limit->ymin = min_vec[1];
672 limit->zmin = min_vec[2];
673
674 limit->xmax = max_vec[0];
675 limit->ymax = max_vec[1];
676 limit->zmax = max_vec[2];
677}
678
680 bPoseChannel *pchan,
681 bConstraint *curcon)
682{
683 bTransformConstraint *trans = static_cast<bTransformConstraint *>(curcon->data);
684
685 float target_mat[4][4], own_mat[4][4], imat[4][4];
686
687 bConstraintOb cob{};
688 cob.depsgraph = nullptr;
689 cob.scene = nullptr;
690 cob.ob = ob;
691 cob.pchan = pchan;
693
694 unit_m4(own_mat);
696 ob, pchan, &cob, own_mat, curcon->ownspace, CONSTRAINT_SPACE_LOCAL, false);
697
698 /* ###Source map mirroring### */
699 float old_min, old_max;
700
701 /* Source location */
702 invert_m4_m4(imat, own_mat);
703
704 /* convert values into local object space */
705 mul_m4_v3(own_mat, trans->from_min);
706 mul_m4_v3(own_mat, trans->from_max);
707
708 old_min = trans->from_min[0];
709 old_max = trans->from_max[0];
710
711 trans->from_min[0] = -old_max;
712 trans->from_max[0] = -old_min;
713
714 /* convert back to the settings space */
715 mul_m4_v3(imat, trans->from_min);
716 mul_m4_v3(imat, trans->from_max);
717
718 /* Source rotation */
719
720 /* Zero out any location translation */
721 own_mat[3][0] = own_mat[3][1] = own_mat[3][2] = 0;
722
723 invert_m4_m4(imat, own_mat);
724
725 /* convert values into local object space */
726 mul_m4_v3(own_mat, trans->from_min_rot);
727 mul_m4_v3(own_mat, trans->from_max_rot);
728
729 old_min = trans->from_min_rot[1];
730 old_max = trans->from_max_rot[1];
731
732 trans->from_min_rot[1] = old_max * -1;
733 trans->from_max_rot[1] = old_min * -1;
734
735 old_min = trans->from_min_rot[2];
736 old_max = trans->from_max_rot[2];
737
738 trans->from_min_rot[2] = old_max * -1;
739 trans->from_max_rot[2] = old_min * -1;
740
741 /* convert back to the settings space */
742 mul_m4_v3(imat, trans->from_min_rot);
743 mul_m4_v3(imat, trans->from_max_rot);
744
745 /* Source scale does not require any mirroring */
746
747 /* ###Destination map mirroring### */
748 float temp_vec[3];
749 float imat_rot[4][4];
750
751 bPoseChannel *target_pchan = BKE_pose_channel_find_name(ob->pose, trans->subtarget);
752 unit_m4(target_mat);
754 ob, target_pchan, &cob, target_mat, curcon->tarspace, CONSTRAINT_SPACE_LOCAL, false);
755
756 invert_m4_m4(imat, target_mat);
757 /* convert values into local object space */
758 mul_m4_v3(target_mat, trans->to_min);
759 mul_m4_v3(target_mat, trans->to_max);
760 mul_m4_v3(target_mat, trans->to_min_scale);
761 mul_m4_v3(target_mat, trans->to_max_scale);
762
763 /* Zero out any location translation */
764 target_mat[3][0] = target_mat[3][1] = target_mat[3][2] = 0;
765 invert_m4_m4(imat_rot, target_mat);
766
767 mul_m4_v3(target_mat, trans->to_min_rot);
768 mul_m4_v3(target_mat, trans->to_max_rot);
769
770 /* TODO(sebpa): This does not support euler order, but doing so will make this way more complex.
771 * For now we have decided to not support all corner cases and advanced setups. */
772
773 /* Helper variables to denote the axis in trans->map */
774 const char X = 0;
775 const char Y = 1;
776 const char Z = 2;
777
778 switch (trans->to) {
779 case TRANS_SCALE:
780 copy_v3_v3(temp_vec, trans->to_max_scale);
781
782 for (int i = 0; i < 3; i++) {
783 if ((trans->from == TRANS_LOCATION && trans->map[i] == X) ||
784 (trans->from == TRANS_ROTATION && trans->map[i] != X))
785 {
786 /* X Loc to X/Y/Z Scale: Min/Max Flipped */
787 /* Y Rot to X/Y/Z Scale: Min/Max Flipped */
788 /* Z Rot to X/Y/Z Scale: Min/Max Flipped */
789 trans->to_max_scale[i] = trans->to_min_scale[i];
790 trans->to_min_scale[i] = temp_vec[i];
791 }
792 }
793 break;
794 case TRANS_LOCATION:
795 /* Invert the X location */
796 trans->to_min[0] *= -1;
797 trans->to_max[0] *= -1;
798
799 copy_v3_v3(temp_vec, trans->to_max);
800
801 for (int i = 0; i < 3; i++) {
802 if ((trans->from == TRANS_LOCATION && trans->map[i] == X) ||
803 (trans->from == TRANS_ROTATION && trans->map[i] != X))
804 {
805 /* X Loc to X/Y/Z Loc: Min/Max Flipped (and Inverted)
806 * Y Rot to X/Y/Z Loc: Min/Max Flipped
807 * Z Rot to X/Y/Z Loc: Min/Max Flipped */
808 trans->to_max[i] = trans->to_min[i];
809 trans->to_min[i] = temp_vec[i];
810 }
811 }
812 break;
813 case TRANS_ROTATION:
814 /* Invert the Z rotation */
815 trans->to_min_rot[2] *= -1;
816 trans->to_max_rot[2] *= -1;
817
818 if ((trans->from == TRANS_LOCATION && trans->map[1] != X) ||
819 (trans->from == TRANS_ROTATION && trans->map[1] != Y) || trans->from == TRANS_SCALE)
820 {
821 /* Invert the Y rotation */
822 trans->to_min_rot[1] *= -1;
823 trans->to_max_rot[1] *= -1;
824 }
825
826 copy_v3_v3(temp_vec, trans->to_max_rot);
827
828 for (int i = 0; i < 3; i++) {
829 if ((trans->from == TRANS_LOCATION && trans->map[i] == X && i != 1) ||
830 (trans->from == TRANS_ROTATION && trans->map[i] == Y && i != 1) ||
831 (trans->from == TRANS_ROTATION && trans->map[i] == Z))
832 {
833 /* X Loc to X/Z Rot: Flipped
834 * Y Rot to X/Z Rot: Flipped
835 * Z Rot to X/Y/Z rot: Flipped */
836 trans->to_max_rot[i] = trans->to_min_rot[i];
837 trans->to_min_rot[i] = temp_vec[i];
838 }
839 }
840
841 if (trans->from == TRANS_ROTATION && trans->map[1] == Y) {
842 /* Y Rot to Y Rot: Flip and invert */
843 trans->to_max_rot[1] = -trans->to_min_rot[1];
844 trans->to_min_rot[1] = -temp_vec[1];
845 }
846
847 break;
848 }
849 /* convert back to the settings space */
850 mul_m4_v3(imat, trans->to_min);
851 mul_m4_v3(imat, trans->to_max);
852 mul_m4_v3(imat_rot, trans->to_min_rot);
853 mul_m4_v3(imat_rot, trans->to_max_rot);
854 mul_m4_v3(imat, trans->to_min_scale);
855 mul_m4_v3(imat, trans->to_max_scale);
856}
857
859{
860 /* Swap track axis X <> -X. */
861 if (value == TRACK_X) {
862 value = TRACK_nX;
863 }
864 else if (value == TRACK_nX) {
865 value = TRACK_X;
866 }
867}
868
869static void track_axis_x_swap(char &value)
870{
871 /* Swap track axis X <> -X. */
872 if (value == TRACK_X) {
873 value = TRACK_nX;
874 }
875 else if (value == TRACK_nX) {
876 value = TRACK_X;
877 }
878}
879
881{
882 bTrackToConstraint *data = static_cast<bTrackToConstraint *>(curcon->data);
883 track_axis_x_swap(data->reserved1);
884}
885
887{
888 bLockTrackConstraint *data = static_cast<bLockTrackConstraint *>(curcon->data);
889 track_axis_x_swap(data->trackflag);
890}
891
893{
894 bDampTrackConstraint *data = static_cast<bDampTrackConstraint *>(curcon->data);
895 track_axis_x_swap(data->trackflag);
896}
897
903
905 EditBone *orig_bone,
906 Object *ob)
907{
908 /* If an edit bone has been duplicated, lets update its constraints if the
909 * subtarget they point to has also been duplicated.
910 */
911 bPoseChannel *pchan;
912 ListBaseT<bConstraint> *conlist;
913
914 if ((pchan = BKE_pose_channel_ensure(ob->pose, dup_bone->name)) == nullptr ||
915 (conlist = &pchan->constraints) == nullptr)
916 {
917 return;
918 }
919
920 for (bConstraint &curcon : *conlist) {
921 switch (curcon.type) {
923 update_duplicate_action_constraint_settings(dup_bone, orig_bone, ob, pchan, &curcon);
924 break;
927 break;
931 break;
934 break;
937 break;
940 break;
943 break;
946 break;
947 default:
948 break;
949 }
950 }
951}
952
954{
955 if (ob->pose == nullptr) {
956 return;
957 }
958 bPoseChannel *pchan;
959 pchan = BKE_pose_channel_ensure(ob->pose, dup_bone->name);
960
961 /* Invert the X location */
962 pchan->custom_translation[0] *= -1;
963 /* Invert the Y rotation */
964 pchan->custom_rotation_euler[1] *= -1;
965 /* Invert the Z rotation */
966 pchan->custom_rotation_euler[2] *= -1;
967
968 bool needs_flip_scale = true;
969
970 if (pchan->custom != nullptr) {
971 Main *bmain = CTX_data_main(C);
972 char name_flip[MAX_ID_NAME - 2];
973
974 /* Skip the first two chars in the object name as those are used to store object type */
975 BLI_string_flip_side_name(name_flip, pchan->custom->id.name + 2, false, sizeof(name_flip));
976 Object *shape_ob = reinterpret_cast<Object *>(BKE_libblock_find_name(bmain, ID_OB, name_flip));
977
978 /* If name_flip doesn't exist, BKE_libblock_find_name() returns pchan->custom (best match) */
979 shape_ob = shape_ob == pchan->custom ? nullptr : shape_ob;
980
981 if (shape_ob != nullptr) {
982 /* A flipped shape object exists, use it! */
983 pchan->custom = shape_ob;
984 needs_flip_scale = false;
985 }
986 }
987
988 /* Invert scale (if no flipped custom shape object is in play). */
989 if (needs_flip_scale) {
990 pchan->custom_scale_xyz[0] *= -1;
991 }
992}
993
994/* Properties should be added on a case by case basis whenever needed to avoid mirroring things
995 * that shouldn't be mirrored. */
996static void mirror_pose_bone(Object &ob, EditBone &ebone)
997{
998 bPoseChannel *pose_bone = BKE_pose_channel_find_name(ob.pose, ebone.name);
999 BLI_assert(pose_bone);
1000 float limit_min = pose_bone->limitmin[2];
1001 pose_bone->limitmin[2] = -pose_bone->limitmax[2];
1002 pose_bone->limitmax[2] = -limit_min;
1003}
1004
1006 EditBone &source_bone,
1007 EditBone &target_bone)
1008{
1009 BLI_assert_msg(armature.edbo != nullptr, "Expecting the armature to be in edit mode");
1010 char name_flip[64];
1011 /* Avoiding modification of the ListBaseT in the iteration. */
1012 Vector<BoneCollection *> unassign_collections;
1013 Vector<BoneCollection *> assign_collections;
1014
1015 /* Find all collections from source_bone that can be flipped. */
1016 for (BoneCollectionReference &collection_reference : source_bone.bone_collections) {
1017 BoneCollection *collection = collection_reference.bcoll;
1018 BLI_string_flip_side_name(name_flip, collection->name, false, sizeof(name_flip));
1019 if (STREQ(name_flip, collection->name)) {
1020 /* Name flipping failed. */
1021 continue;
1022 }
1023 BoneCollection *flipped_collection = ANIM_armature_bonecoll_get_by_name(&armature, name_flip);
1024 if (!flipped_collection) {
1025 const int bcoll_index = animrig::armature_bonecoll_find_index(&armature, collection);
1026 const int parent_index = animrig::armature_bonecoll_find_parent_index(&armature,
1027 bcoll_index);
1028 flipped_collection = ANIM_armature_bonecoll_new(&armature, name_flip, parent_index);
1029 }
1030 BLI_assert(flipped_collection != nullptr);
1031 unassign_collections.append(collection);
1032 assign_collections.append(flipped_collection);
1033 }
1034
1035 /* The target_bone might not be in unassign_collections anymore, or might already be in
1036 * assign_collections. The assign functions will just do nothing in those cases. */
1037 for (BoneCollection *collection : unassign_collections) {
1038 ANIM_armature_bonecoll_unassign_editbone(collection, &target_bone);
1039 }
1040
1041 for (BoneCollection *collection : assign_collections) {
1042 ANIM_armature_bonecoll_assign_editbone(collection, &target_bone);
1043 }
1044}
1045
1046static void copy_pchan(EditBone *src_bone, EditBone *dst_bone, Object *src_ob, Object *dst_ob)
1047{
1048 /* copy the ID property */
1049 if (src_bone->prop) {
1050 dst_bone->prop = IDP_CopyProperty(src_bone->prop);
1051 }
1052 if (src_bone->system_properties) {
1054 }
1055
1056 /* Lets duplicate the list of constraints that the
1057 * current bone has.
1058 */
1059 if (src_ob->pose) {
1060 bPoseChannel *chanold, *channew;
1061
1062 chanold = BKE_pose_channel_ensure(src_ob->pose, src_bone->name);
1063 if (chanold) {
1064 /* WARNING: this creates a new pose-channel, but there will not be an attached bone
1065 * yet as the new bones created here are still 'EditBones' not 'Bones'.
1066 */
1067 channew = BKE_pose_channel_ensure(dst_ob->pose, dst_bone->name);
1068
1069 if (channew) {
1070 BKE_pose_channel_copy_data(channew, chanold);
1071 }
1072 }
1073 }
1074}
1075
1076void ED_armature_ebone_copy(EditBone *dest, const EditBone *source)
1077{
1078 memcpy(dest, source, sizeof(*dest));
1080}
1081
1083 const char *name,
1084 ListBaseT<EditBone> *editbones,
1085 Object *src_ob,
1086 Object *dst_ob)
1087{
1088 EditBone *e_bone = MEM_new<EditBone>("addup_editbone");
1089
1090 /* Copy data from old bone to new bone */
1091 ED_armature_ebone_copy(e_bone, cur_bone);
1092
1093 cur_bone->temp.ebone = e_bone;
1094 e_bone->temp.ebone = cur_bone;
1095
1096 if (name != nullptr) {
1097 STRNCPY_UTF8(e_bone->name, name);
1098 }
1099
1100 ED_armature_ebone_unique_name(editbones, e_bone->name, nullptr);
1101 BLI_addtail(editbones, e_bone);
1102
1103 copy_pchan(cur_bone, e_bone, src_ob, dst_ob);
1104
1105 return e_bone;
1106}
1107
1109 const char *name,
1110 ListBaseT<EditBone> *editbones,
1111 Object *ob)
1112{
1113 return duplicateEditBoneObjects(cur_bone, name, editbones, ob, ob);
1114}
1115
1117 const bool do_flip_names,
1118 StringRefNull search,
1119 StringRefNull replace)
1120{
1121 const Main *bmain = CTX_data_main(C);
1122 const Scene *scene = CTX_data_scene(C);
1123 ViewLayer *view_layer = CTX_data_view_layer(C);
1124
1125 /* cancel if nothing selected */
1126 if (CTX_DATA_COUNT(C, selected_bones) == 0) {
1127 return OPERATOR_CANCELLED;
1128 }
1129
1131 *bmain, scene, view_layer, CTX_wm_view3d(C));
1132 for (Object *ob : objects) {
1133 EditBone *ebone_iter;
1134 /* The beginning of the duplicated bones in the edbo list */
1135 EditBone *ebone_first_dupe = nullptr;
1136
1137 bArmature *arm = id_cast<bArmature *>(ob->data);
1138
1139 ED_armature_edit_sync_selection(arm->edbo); /* XXX why is this needed? */
1140
1142
1143 /* Select mirrored bones */
1144 if (arm->flag & ARM_MIRROR_EDIT) {
1145 for (EditBone &ebone_iter : *arm->edbo) {
1146 if (animrig::bone_is_selected(arm, &ebone_iter)) {
1147 EditBone *ebone;
1148
1149 ebone = ED_armature_ebone_get_mirrored(arm->edbo, &ebone_iter);
1150 if (ebone) {
1151 ebone->flag |= BONE_SELECTED;
1152 }
1153 }
1154 }
1155 }
1156
1157 /* Find the selected bones and duplicate them as needed */
1158 for (ebone_iter = arm->edbo->first(); ebone_iter && ebone_iter != ebone_first_dupe;
1159 ebone_iter = ebone_iter->next)
1160 {
1161 if (animrig::bone_is_selected(arm, ebone_iter)) {
1162 EditBone *ebone;
1163 char new_bone_name_buff[MAXBONENAME];
1164 const char *new_bone_name = ebone_iter->name;
1165
1166 if (do_flip_names) {
1168 new_bone_name_buff, ebone_iter->name, false, sizeof(new_bone_name_buff));
1169
1170 /* Only use flipped name if not yet in use. Otherwise we'd get again inconsistent
1171 * namings (different numbers), better keep default behavior in this case. */
1172 if (ED_armature_ebone_find_name(arm->edbo, new_bone_name_buff) == nullptr) {
1173 new_bone_name = new_bone_name_buff;
1174 }
1175 }
1176 std::string buffer(new_bone_name);
1177 if (!search.is_empty()) {
1178 BLI_string_replace(buffer, search, replace);
1179 new_bone_name = buffer.data();
1180 }
1181
1182 ebone = duplicateEditBone(ebone_iter, new_bone_name, arm->edbo, ob);
1183
1184 if (!ebone_first_dupe) {
1185 ebone_first_dupe = ebone;
1186 }
1187 }
1188 }
1189
1190 /* Run though the list and fix the pointers */
1191 for (ebone_iter = arm->edbo->first(); ebone_iter && ebone_iter != ebone_first_dupe;
1192 ebone_iter = ebone_iter->next)
1193 {
1194 if (animrig::bone_is_selected(arm, ebone_iter)) {
1195 EditBone *ebone = ebone_iter->temp.ebone;
1196
1197 if (!ebone_iter->parent) {
1198 /* If this bone has no parent,
1199 * Set the duplicate->parent to nullptr
1200 */
1201 ebone->parent = nullptr;
1202 }
1203 else if (ebone_iter->parent->temp.ebone) {
1204 /* If this bone has a parent that was duplicated,
1205 * Set the duplicate->parent to the cur_bone->parent->temp
1206 */
1207 ebone->parent = ebone_iter->parent->temp.ebone;
1208 }
1209 else {
1210 /* If this bone has a parent that IS not selected,
1211 * Set the duplicate->parent to the cur_bone->parent
1212 */
1213 ebone->parent = ebone_iter->parent;
1214 ebone->flag &= ~BONE_CONNECTED;
1215 }
1216
1217 /* Update custom handle links. */
1218 if (ebone_iter->bbone_prev && ebone_iter->bbone_prev->temp.ebone) {
1219 ebone->bbone_prev = ebone_iter->bbone_prev->temp.ebone;
1220 }
1221 if (ebone_iter->bbone_next && ebone_iter->bbone_next->temp.ebone) {
1222 ebone->bbone_next = ebone_iter->bbone_next->temp.ebone;
1223 }
1224
1225 /* Lets try to fix any constraint sub-targets that might have been duplicated. */
1226 update_duplicate_subtarget(ebone, ob, false);
1227 }
1228 }
1229
1230 /* correct the active bone */
1231 if (arm->act_edbone && arm->act_edbone->temp.ebone) {
1232 arm->act_edbone = arm->act_edbone->temp.ebone;
1233 }
1234
1235 /* Deselect the old bones and select the new ones */
1236 for (ebone_iter = arm->edbo->first(); ebone_iter && ebone_iter != ebone_first_dupe;
1237 ebone_iter = ebone_iter->next)
1238 {
1239 if (animrig::bone_is_visible(arm, ebone_iter)) {
1240 ebone_iter->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
1241 }
1242 }
1243
1245
1248 }
1249
1251
1252 return OPERATOR_FINISHED;
1253}
1254
1256{
1257 const bool do_flip_names = RNA_boolean_get(op->ptr, "do_flip_names");
1258 return armature_duplicate_selected(C, do_flip_names, "", "");
1259}
1260
1262{
1263 /* identifiers */
1264 ot->name = "Duplicate Selected Bone(s)";
1265 ot->idname = "ARMATURE_OT_duplicate";
1266 ot->description = "Make copies of the selected bones within the same armature";
1267
1268 /* API callbacks. */
1271
1272 /* flags */
1274
1276 ot->srna,
1277 "do_flip_names",
1278 false,
1279 "Flip Names",
1280 "Try to flip names of the bones, if possible, instead of adding a number extension");
1281}
1282
1284{
1285 const bool do_flip_names = RNA_boolean_get(op->ptr, "do_flip_names");
1286 std::string search = RNA_string_get(op->ptr, "search");
1287 std::string replace = RNA_string_get(op->ptr, "replace");
1288 if (search.empty()) {
1289 BKE_report(op->reports, RPT_ERROR, "No search term defined");
1290 return OPERATOR_CANCELLED;
1291 }
1292 return armature_duplicate_selected(C, do_flip_names, search, replace);
1293}
1294
1296 wmOperator *op,
1297 const wmEvent *event)
1298{
1299 return WM_operator_props_popup_confirm(C, op, event);
1300}
1301
1303{
1304 /* identifiers */
1305 ot->name = "Duplicate and Rename";
1306 ot->idname = "ARMATURE_OT_duplicate_rename";
1307 ot->description =
1308 "Make copies of the selected bones within the same armature and replace a part of their "
1309 "name";
1310
1311 /* API callbacks. */
1315
1316 /* flags */
1318
1320 ot->srna,
1321 "do_flip_names",
1322 false,
1323 "Flip Names",
1324 "Try to flip names of the bones, if possible, instead of adding a number extension");
1325
1326 RNA_def_string(ot->srna,
1327 "search",
1328 nullptr,
1330 "Search",
1331 "A part of the current bone name that will be replaced");
1332 RNA_def_string(ot->srna,
1333 "replace",
1334 nullptr,
1336 "Replace",
1337 "The substitute to be inserted into the place of the given search term. If left "
1338 "empty the search term will be removed");
1339}
1340
1341/* Get the duplicated or existing mirrored copy of the bone. */
1343{
1344 if (bone == nullptr) {
1345 return nullptr;
1346 }
1347 if (bone->temp.ebone != nullptr) {
1348 return bone->temp.ebone;
1349 }
1350
1351 EditBone *mirror = ED_armature_ebone_get_mirrored(arm->edbo, bone);
1352 return (mirror != nullptr) ? mirror : bone;
1353}
1354
1360{
1361 const Main *bmain = CTX_data_main(C);
1362 const Scene *scene = CTX_data_scene(C);
1363 ViewLayer *view_layer = CTX_data_view_layer(C);
1364 const int direction = RNA_enum_get(op->ptr, "direction");
1365 const bool copy_bone_colors = RNA_boolean_get(op->ptr, "copy_bone_colors");
1366 const int axis = 0;
1367
1368 /* cancel if nothing selected */
1369 if (CTX_DATA_COUNT(C, selected_bones) == 0) {
1370 return OPERATOR_CANCELLED;
1371 }
1372
1374 *bmain, scene, view_layer, CTX_wm_view3d(C));
1375 for (Object *obedit : objects) {
1376 EditBone *ebone_iter;
1377 /* The beginning of the duplicated mirrored bones in the edbo list */
1378 EditBone *ebone_first_dupe = nullptr;
1379
1380 bArmature *arm = id_cast<bArmature *>(obedit->data);
1381
1382 ED_armature_edit_sync_selection(arm->edbo); /* XXX why is this needed? */
1383
1385
1386 /* Deselect ebones depending on input axis and direction.
1387 * A symmetrizable selection contains selected ebones of the input direction
1388 * and unique selected bones with an unique flippable name.
1389 *
1390 * Storing temp pointers to mirrored unselected ebones. */
1391 for (EditBone &ebone_iter : *arm->edbo) {
1392 if (!(animrig::bone_is_visible(arm, &ebone_iter) && (ebone_iter.flag & BONE_SELECTED))) {
1393 /* Skipping invisible selected bones. */
1394 continue;
1395 }
1396
1397 char name_flip[MAXBONENAME];
1398 BLI_string_flip_side_name(name_flip, ebone_iter.name, false, sizeof(name_flip));
1399
1400 if (STREQ(name_flip, ebone_iter.name)) {
1401 /* Skipping ebones without flippable as they don't have the potential to be mirrored. */
1402 ebone_iter.flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
1403 continue;
1404 }
1405
1406 EditBone *ebone = ED_armature_ebone_find_name(arm->edbo, name_flip);
1407
1408 if (!ebone) {
1409 /* The ebone_iter is unique and mirror-able. */
1410 continue;
1411 }
1412
1413 if (ebone->flag & BONE_SELECTED) {
1414 /* The mirrored ebone and the ebone_iter are selected.
1415 * Deselect based on the input direction and axis. */
1416 float axis_delta;
1417
1418 axis_delta = ebone->head[axis] - ebone_iter.head[axis];
1419 if (axis_delta == 0.0f) {
1420 /* The ebone heads are overlapping. */
1421 axis_delta = ebone->tail[axis] - ebone_iter.tail[axis];
1422
1423 if (axis_delta == 0.0f) {
1424 /* Both mirrored bones point to each other and overlap exactly.
1425 * In this case there's no well defined solution, so de-select both and skip. */
1426 ebone->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
1427 ebone_iter.flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
1428 continue;
1429 }
1430 }
1431
1432 /* Deselect depending on direction. */
1433 if (((axis_delta < 0.0f) ? -1 : 1) == direction) {
1434 /* Don't store temp ptr if the iter_bone gets deselected.
1435 * In this case, the ebone.temp should point to the ebone_iter. */
1436 ebone_iter.flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
1437 continue;
1438 }
1439
1440 ebone->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
1441 }
1442
1443 /* Set temp pointer to mirrored ebones */
1444 ebone_iter.temp.ebone = ebone;
1445 }
1446
1447 /* Find the selected bones and duplicate them as needed, with mirrored name. */
1448 for (ebone_iter = arm->edbo->first(); ebone_iter && ebone_iter != ebone_first_dupe;
1449 ebone_iter = ebone_iter->next)
1450 {
1451 if (animrig::bone_is_selected(arm, ebone_iter)) {
1452 if (ebone_iter->temp.ebone != nullptr) {
1453 /* This will be set if the mirror bone already exists (no need to make a new one)
1454 * but we do need to make sure that the 'pchan' settings (constraints etc)
1455 * is synchronized. */
1456 bPoseChannel *pchan;
1457 /* Make sure we clean up the old data before overwriting it */
1458 pchan = BKE_pose_channel_ensure(obedit->pose, ebone_iter->temp.ebone->name);
1459 BKE_pose_channel_free(pchan);
1460 /* Sync pchan data */
1461 copy_pchan(ebone_iter, ebone_iter->temp.ebone, obedit, obedit);
1462 /* Sync scale mode */
1463 ebone_iter->temp.ebone->inherit_scale_mode = ebone_iter->inherit_scale_mode;
1464 continue;
1465 }
1466
1467 char name_flip[MAXBONENAME];
1468
1469 BLI_string_flip_side_name(name_flip, ebone_iter->name, false, sizeof(name_flip));
1470
1471 /* mirrored bones must have a side-suffix */
1472 if (!STREQ(name_flip, ebone_iter->name)) {
1473 EditBone *ebone;
1474
1475 ebone = duplicateEditBone(ebone_iter, name_flip, arm->edbo, obedit);
1476
1477 if (!ebone_first_dupe) {
1478 ebone_first_dupe = ebone;
1479 }
1480 }
1481 }
1482 }
1483
1484 /* Run through the list and fix the pointers. */
1485 for (ebone_iter = arm->edbo->first(); ebone_iter && ebone_iter != ebone_first_dupe;
1486 ebone_iter = ebone_iter->next)
1487 {
1488 if (ebone_iter->temp.ebone) {
1489 /* copy all flags except for ... */
1490 const eBone_Flag flag_copy = eBone_Flag(~0) &
1492
1493 EditBone *ebone = ebone_iter->temp.ebone;
1494
1495 /* Copy flags in case bone is pre-existing data. */
1496 ebone->flag = (ebone->flag & ~flag_copy) | (ebone_iter->flag & flag_copy);
1497
1498 /* Copy Viewport Display. */
1499 ebone->drawtype = ebone_iter->drawtype;
1500 if (copy_bone_colors) {
1501 ebone->color.palette_index = ebone_iter->color.palette_index;
1502 copy_v4_v4_uchar(ebone->color.custom.active, ebone_iter->color.custom.active);
1503 copy_v4_v4_uchar(ebone->color.custom.select, ebone_iter->color.custom.select);
1504 copy_v4_v4_uchar(ebone->color.custom.solid, ebone_iter->color.custom.solid);
1505 ebone->color.custom.flag = ebone_iter->color.custom.flag;
1506 }
1507
1508 if (ebone_iter->parent == nullptr) {
1509 /* If this bone has no parent,
1510 * Set the duplicate->parent to nullptr
1511 */
1512 ebone->parent = nullptr;
1513 ebone->flag &= ~BONE_CONNECTED;
1514 }
1515 else {
1516 /* the parent may have been duplicated, if not lookup the mirror parent */
1517 EditBone *ebone_parent = get_symmetrized_bone(arm, ebone_iter->parent);
1518
1519 if (ebone_parent == ebone_iter->parent) {
1520 /* If the mirror lookup failed, (but the current bone has a parent)
1521 * then we can assume the parent has no L/R but is a center bone.
1522 * So just use the same parent for both.
1523 */
1524
1525 if (ebone->head[axis] != 0.0f) {
1526 /* The mirrored bone doesn't start on the mirror axis, so assume that this one
1527 * should not be connected to the old parent */
1528 ebone->flag &= ~BONE_CONNECTED;
1529 }
1530 }
1531
1532 ebone->parent = ebone_parent;
1533 }
1534
1535 /* Update custom handle links. */
1536 ebone->bbone_prev = get_symmetrized_bone(arm, ebone_iter->bbone_prev);
1537 ebone->bbone_next = get_symmetrized_bone(arm, ebone_iter->bbone_next);
1538
1539 /* Sync bbone handle types */
1540 ebone->bbone_prev_type = ebone_iter->bbone_prev_type;
1541 ebone->bbone_next_type = ebone_iter->bbone_next_type;
1542
1543 ebone->bbone_mapping_mode = ebone_iter->bbone_mapping_mode;
1544 ebone->bbone_flag = ebone_iter->bbone_flag;
1545 ebone->bbone_prev_flag = ebone_iter->bbone_prev_flag;
1546 ebone->bbone_next_flag = ebone_iter->bbone_next_flag;
1547
1548 /* Lets try to fix any constraint sub-targets that might have been duplicated. */
1549 update_duplicate_subtarget(ebone, obedit, true);
1550 /* Try to update constraint options so that they are mirrored as well
1551 * (need to supply bone_iter as well in case we are working with existing bones) */
1552 update_duplicate_constraint_settings(ebone, ebone_iter, obedit);
1553 /* Mirror bone shapes if possible */
1554 update_duplicate_custom_bone_shapes(C, ebone, obedit);
1555 /* Mirror any settings on the pose bone. */
1556 mirror_pose_bone(*obedit, *ebone);
1557 mirror_bone_collection_assignments(*arm, *ebone_iter, *ebone);
1558 }
1559 }
1560
1562
1563 /* Selected bones now have their 'temp' pointer set,
1564 * so we don't need this anymore */
1565
1566 /* Deselect the old bones and select the new ones */
1567 for (ebone_iter = arm->edbo->first(); ebone_iter && ebone_iter != ebone_first_dupe;
1568 ebone_iter = ebone_iter->next)
1569 {
1570 if (animrig::bone_is_visible(arm, ebone_iter)) {
1571 ebone_iter->flag &= ~(BONE_SELECTED | BONE_TIPSEL | BONE_ROOTSEL);
1572 }
1573 }
1574
1575 /* New bones will be selected, but some of the bones may already exist */
1576 for (ebone_iter = arm->edbo->first(); ebone_iter && ebone_iter != ebone_first_dupe;
1577 ebone_iter = ebone_iter->next)
1578 {
1579 EditBone *ebone = ebone_iter->temp.ebone;
1580 if (ebone && EBONE_SELECTABLE(arm, ebone)) {
1581 ED_armature_ebone_select_set(ebone, true);
1582 }
1583 }
1584
1585 /* correct the active bone */
1586 if (arm->act_edbone && arm->act_edbone->temp.ebone) {
1587 arm->act_edbone = arm->act_edbone->temp.ebone;
1588 }
1589
1590 post_edit_bone_duplicate(arm->edbo, obedit);
1591
1593 DEG_id_tag_update(&obedit->id, ID_RECALC_SELECT);
1594 }
1595
1596 return OPERATOR_FINISHED;
1597}
1598
1600{
1601 /* NOTE: following conventions from #MESH_OT_symmetrize */
1602
1603 /* subset of 'rna_enum_symmetrize_direction_items' */
1604 static const EnumPropertyItem arm_symmetrize_direction_items[] = {
1605 {-1, "NEGATIVE_X", 0, "-X to +X", ""},
1606 {+1, "POSITIVE_X", 0, "+X to -X", ""},
1607 {0, nullptr, 0, nullptr, nullptr},
1608 };
1609
1610 /* identifiers */
1611 ot->name = "Symmetrize";
1612 ot->idname = "ARMATURE_OT_symmetrize";
1613 ot->description = "Enforce symmetry, make copies of the selection or use existing";
1614
1615 /* API callbacks. */
1618
1619 /* flags */
1621
1622 ot->prop = RNA_def_enum(ot->srna,
1623 "direction",
1624 arm_symmetrize_direction_items,
1625 -1,
1626 "Direction",
1627 "Which sides to copy from and to (when both are selected)");
1628 ot->prop = RNA_def_boolean(
1629 ot->srna, "copy_bone_colors", false, "Bone Colors", "Copy colors to existing bones");
1630}
1631
1632/* ------------------------------------------ */
1633
1634/* previously extrude_armature */
1635/* context; editmode armature */
1636/* if forked && mirror-edit: makes two bones with flipped names */
1638{
1639 const Main *bmain = CTX_data_main(C);
1640 const Scene *scene = CTX_data_scene(C);
1641 ViewLayer *view_layer = CTX_data_view_layer(C);
1642 const bool forked = RNA_boolean_get(op->ptr, "forked");
1643 bool changed_multi = false;
1645 *bmain, scene, view_layer, CTX_wm_view3d(C));
1646
1647 enum ExtrudePoint {
1648 SKIP_EXTRUDE,
1649 TIP_EXTRUDE,
1650 ROOT_EXTRUDE,
1651 };
1652
1653 for (Object *ob : objects) {
1654 bArmature *arm = id_cast<bArmature *>(ob->data);
1655 bool forked_iter = forked;
1656
1657 EditBone *newbone = nullptr, *ebone, *flipbone, *first = nullptr;
1658 int a, totbone = 0;
1659 ExtrudePoint do_extrude;
1660
1661 /* since we allow root extrude too, we have to make sure selection is OK */
1662 for (EditBone &ebone : *arm->edbo) {
1663 if (animrig::bone_is_visible(arm, &ebone)) {
1664 if (ebone.flag & BONE_ROOTSEL) {
1665 if (ebone.parent && (ebone.flag & BONE_CONNECTED)) {
1666 if (ebone.parent->flag & BONE_TIPSEL) {
1667 ebone.flag &= ~BONE_ROOTSEL;
1668 }
1669 }
1670 }
1671 }
1672 }
1673
1674 /* Duplicate the necessary bones */
1675 for (ebone = arm->edbo->first(); ((ebone) && (ebone != first)); ebone = ebone->next) {
1676 if (!animrig::bone_is_visible(arm, ebone)) {
1677 continue;
1678 }
1679 /* We extrude per definition the tip. */
1680 do_extrude = SKIP_EXTRUDE;
1681 if (ebone->flag & (BONE_TIPSEL | BONE_SELECTED)) {
1682 do_extrude = TIP_EXTRUDE;
1683 }
1684 else if (ebone->flag & BONE_ROOTSEL) {
1685 /* but, a bone with parent deselected we do the root... */
1686 if (ebone->parent && (ebone->parent->flag & BONE_TIPSEL)) {
1687 /* pass */
1688 }
1689 else {
1690 do_extrude = ROOT_EXTRUDE;
1691 }
1692 }
1693
1694 if (do_extrude) {
1695 /* we re-use code for mirror editing... */
1696 flipbone = nullptr;
1697 if (arm->flag & ARM_MIRROR_EDIT) {
1698 flipbone = ED_armature_ebone_get_mirrored(arm->edbo, ebone);
1699 if (flipbone) {
1700 forked_iter = false; /* we extrude 2 different bones */
1701 if (flipbone->flag & (BONE_TIPSEL | BONE_ROOTSEL | BONE_SELECTED)) {
1702 /* don't want this bone to be selected... */
1703 flipbone->flag &= ~(BONE_TIPSEL | BONE_SELECTED | BONE_ROOTSEL);
1704 }
1705 }
1706 if ((flipbone == nullptr) && (forked_iter)) {
1707 flipbone = ebone;
1708 }
1709 }
1710
1711 for (a = 0; a < 2; a++) {
1712 if (a == 1) {
1713 if (flipbone == nullptr) {
1714 break;
1715 }
1716 std::swap(flipbone, ebone);
1717 }
1718
1719 totbone++;
1720 newbone = MEM_new<EditBone>("extrudebone");
1721
1722 if (do_extrude == TIP_EXTRUDE) {
1723 copy_v3_v3(newbone->head, ebone->tail);
1724 copy_v3_v3(newbone->tail, newbone->head);
1725 newbone->parent = ebone;
1726
1727 /* copies it, in case mirrored bone */
1728 newbone->flag = ebone->flag & (BONE_TIPSEL | BONE_RELATIVE_PARENTING);
1729
1730 if (newbone->parent) {
1731 newbone->flag |= BONE_CONNECTED;
1732 }
1733 }
1734 else if (do_extrude == ROOT_EXTRUDE) {
1735 copy_v3_v3(newbone->head, ebone->head);
1736 copy_v3_v3(newbone->tail, ebone->head);
1737 newbone->parent = ebone->parent;
1738
1739 newbone->flag = BONE_TIPSEL;
1740
1741 if (newbone->parent && (ebone->flag & BONE_CONNECTED)) {
1742 newbone->flag |= BONE_CONNECTED;
1743 }
1744 }
1745
1746 newbone->color = ebone->color;
1747 newbone->drawtype = ebone->drawtype;
1748
1749 newbone->weight = ebone->weight;
1750 newbone->dist = ebone->dist;
1751 newbone->xwidth = ebone->xwidth;
1752 newbone->zwidth = ebone->zwidth;
1753 newbone->rad_head = ebone->rad_tail; /* don't copy entire bone. */
1754 newbone->rad_tail = ebone->rad_tail;
1755 newbone->segments = 1;
1756 newbone->layer = ebone->layer;
1757
1758 /* Bendy-Bone parameters */
1759 newbone->roll1 = ebone->roll1;
1760 newbone->roll2 = ebone->roll2;
1761 newbone->curve_in_x = ebone->curve_in_x;
1762 newbone->curve_in_z = ebone->curve_in_z;
1763 newbone->curve_out_x = ebone->curve_out_x;
1764 newbone->curve_out_z = ebone->curve_out_z;
1765 newbone->ease1 = ebone->ease1;
1766 newbone->ease2 = ebone->ease2;
1767
1768 copy_v3_v3(newbone->scale_in, ebone->scale_in);
1769 copy_v3_v3(newbone->scale_out, ebone->scale_out);
1770
1771 STRNCPY_UTF8(newbone->name, ebone->name);
1772
1773 if (flipbone && forked_iter) { /* only set if mirror edit */
1774 if (strlen(newbone->name) < (MAXBONENAME - 2)) {
1775 BLI_strncat(newbone->name, (a == 0) ? "_L" : "_R", sizeof(newbone->name));
1776 }
1777 }
1778 ED_armature_ebone_unique_name(arm->edbo, newbone->name, nullptr);
1779
1780 /* Copy bone collection membership. */
1782
1783 /* Add the new bone to the list */
1784 BLI_addtail(arm->edbo, newbone);
1785 if (!first) {
1786 first = newbone;
1787 }
1788
1789 /* restore ebone if we were flipping */
1790 if (a == 1 && flipbone) {
1791 std::swap(flipbone, ebone);
1792 }
1793 }
1794 }
1795
1796 /* Deselect the old bone */
1797 ebone->flag &= ~(BONE_TIPSEL | BONE_SELECTED | BONE_ROOTSEL);
1798 }
1799 /* if only one bone, make this one active */
1800 if (totbone == 1 && first) {
1801 arm->act_edbone = first;
1802 }
1803 else {
1804 arm->act_edbone = newbone;
1805 }
1806
1807 if (totbone == 0) {
1808 continue;
1809 }
1810
1811 changed_multi = true;
1812
1813 /* Transform the endpoints */
1815
1818 }
1819
1820 if (!changed_multi) {
1821 return OPERATOR_CANCELLED;
1822 }
1823
1825
1826 return OPERATOR_FINISHED;
1827}
1828
1830{
1831 /* identifiers */
1832 ot->name = "Extrude";
1833 ot->idname = "ARMATURE_OT_extrude";
1834 ot->description = "Create new bones from the selected joints";
1835
1836 /* API callbacks. */
1839
1840 /* flags */
1842
1843 /* props */
1844 RNA_def_boolean(ot->srna, "forked", false, "Forked", "");
1845}
1846
1847/* ********************** Bone Add *************************************/
1848
1849/* Op makes a new bone and returns it with its tip selected. */
1850
1851enum class BoneAlign { UP = 0, AXES = 1, CURSOR_3D = 2, VIEW_3D = 3 };
1852enum class BoneSpace { OBJECT = 0, WORLD = 1 };
1853
1855{
1856 Object *obedit = CTX_data_edit_object(C);
1857
1858 invert_m4_m4(obedit->runtime->world_to_object.ptr(), obedit->object_to_world().ptr());
1859 const float3x3 imat = float3x3(obedit->world_to_object());
1860
1861 float3x3 bone_orient_mat = float3x3::zero();
1862 float3 roll_vector;
1863
1864 /* Apply user pref only for view aligned if the property wasn't set. */
1865 if (!RNA_property_is_set(op->ptr, RNA_struct_find_property(op->ptr, "align")) &&
1866 (U.flag & USER_ADD_VIEWALIGNED))
1867 {
1868 RNA_enum_set(op->ptr, "align", int(BoneAlign::VIEW_3D));
1869 }
1870
1871 const BoneAlign align = BoneAlign(RNA_enum_get(op->ptr, "align"));
1872 const BoneSpace space = BoneSpace(RNA_enum_get(op->ptr, "space"));
1873
1874 switch (align) {
1875 case BoneAlign::VIEW_3D: {
1876 const RegionView3D *rv3d = CTX_wm_region_view3d(C);
1877 const float3x3 view_mat = rv3d ? float3x3(float4x4(rv3d->viewinv)) : float3x3::identity();
1878 bone_orient_mat = imat * view_mat;
1879 roll_vector = bone_orient_mat.z_axis();
1880 break;
1881 }
1882
1883 case BoneAlign::CURSOR_3D: {
1884 const Scene *scene = CTX_data_scene(C);
1885 const View3DCursor &cursor = scene->cursor;
1886 const float3x3 cursor_mat = cursor.matrix<float3x3>();
1887 bone_orient_mat = imat * cursor_mat;
1888 roll_vector = bone_orient_mat.z_axis();
1889 break;
1890 }
1891
1892 case BoneAlign::AXES: {
1893 switch (space) {
1894 case BoneSpace::WORLD:
1895 bone_orient_mat = imat;
1896 roll_vector = imat.z_axis();
1897 break;
1898 case BoneSpace::OBJECT:
1899 /* Assumes Z=up for objects and Y=up for bones. */
1900 bone_orient_mat = float3x3({1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 1.0f, 0.0f});
1901 break;
1902 }
1903 break;
1904 }
1905
1906 case BoneAlign::UP: {
1907 switch (space) {
1908 case BoneSpace::WORLD:
1909 /* Construct a matrix that points Y up, Z Forward and X left-right. */
1910 bone_orient_mat = imat *
1911 float3x3({1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f}, {0.0f, -1.0f, 0.0f});
1912
1913 /* Set roll reference for ED_armature_ebone_roll_to_vector. */
1914 roll_vector = -imat.y_axis();
1915 break;
1916 case BoneSpace::OBJECT:
1917 bone_orient_mat = float3x3::identity();
1918 break;
1919 }
1920 break;
1921 }
1922 }
1923
1924 char name[MAXBONENAME];
1925 RNA_string_get(op->ptr, "name", name);
1926
1927 /* Get inverse point for head and orientation for tail. */
1928 const float3 curs_worldspace = CTX_data_scene(C)->cursor.location;
1929 const float3 curs_objectspace =
1930 (obedit->world_to_object() * float4(curs_worldspace, 1.0f)).xyz();
1931
1933
1934 /* Create a bone. */
1935 EditBone *bone = ED_armature_ebone_add(id_cast<bArmature *>(obedit->data), name);
1936 ANIM_armature_bonecoll_assign_active(id_cast<bArmature *>(obedit->data), bone);
1937
1938 /* Scale B-Bone display width and Bone Envelope based on length. */
1939 const float length = RNA_float_get(op->ptr, "length");
1940 BLI_assert(length > 0.0f);
1941
1942 bone->xwidth = 0.1f * length;
1943 bone->zwidth = 0.1f * length;
1944 bone->rad_head = 0.1f * length;
1945 bone->rad_tail = 0.05f * length;
1946 bone->dist = 0.25f * length;
1947
1948 bArmature *arm = id_cast<bArmature *>(obedit->data);
1949 if (bone->bone_collections.is_empty() && (arm->flag & ARM_BCOLL_SOLO_ACTIVE)) {
1950 BKE_report(op->reports,
1952 "Bone not added to a collection and hidden because solo bone collection(s) exist.");
1953 }
1954 else if (!ANIM_bonecoll_is_visible_editbone(arm, bone)) {
1955 const BoneCollectionReference *bcoll_ref = static_cast<const BoneCollectionReference *>(
1956 bone->bone_collections.first_);
1957 BLI_assert_msg(bcoll_ref,
1958 "Bone that is not visible due to its bone collections MUST be assigned to at "
1959 "least one of them.");
1960 BKE_reportf(op->reports,
1962 "Bone was added to a hidden collection '%s'",
1963 bcoll_ref->bcoll->name);
1964 }
1965
1966 /* Bone head to cursor position. */
1967 copy_v3_v3(bone->head, curs_objectspace);
1968
1969 const float3 tail_vector = bone_orient_mat * float3(0.0f, 0.0f, length);
1970 add_v3_v3v3(bone->tail, bone->head, tail_vector);
1971
1972 const bool needs_bone_roll = (ELEM(align, BoneAlign::CURSOR_3D, BoneAlign::VIEW_3D) ||
1973 space == BoneSpace::WORLD);
1974
1975 if (needs_bone_roll) {
1976 const float3 tail_vector_normalized = math::normalize(bone_orient_mat[1]) * length;
1977 add_v3_v3v3(bone->tail, bone->head, tail_vector_normalized);
1978
1979 /* Compute bone roll so its local Z aligns with desired Z axis. */
1980 bone->roll = ED_armature_ebone_roll_to_vector(bone, roll_vector, false);
1981 }
1982
1983 /* Disable Deform if applicable. */
1984 const bool use_deform = RNA_boolean_get(op->ptr, "use_deform");
1985 if (!use_deform) {
1986 bone->flag |= BONE_NO_DEFORM;
1987 }
1988
1989 /* NOTE: notifier might evolve. */
1993
1994 return OPERATOR_FINISHED;
1995}
1996
1998{
1999 /* identifiers */
2000 ot->name = "Add Bone";
2001 ot->idname = "ARMATURE_OT_bone_primitive_add";
2002 ot->description = "Add a new bone located at the 3D cursor";
2003
2004 /* API callbacks. */
2007
2008 /* flags */
2010
2011 RNA_def_string(ot->srna,
2012 "name",
2015 "Name",
2016 "Name of the newly created bone");
2017
2018 static const EnumPropertyItem space_items[] = {
2019 {int(BoneSpace::OBJECT),
2020 "OBJECT",
2021 0,
2022 "Object",
2023 "The newly created bone will use Object Space co-ordinate system"},
2024 {int(BoneSpace::WORLD),
2025 "WORLD",
2026 0,
2027 "World",
2028 "The newly created bone will use World Space co-ordinate system"},
2029 {0, nullptr, 0, nullptr, nullptr}};
2030
2031 RNA_def_enum(ot->srna,
2032 "space",
2033 space_items,
2034 int(BoneSpace::OBJECT),
2035 "Space",
2036 "Co-ordinate system the new bone will be created in");
2037
2038 static const EnumPropertyItem align_items[] = {
2039 {int(BoneAlign::UP),
2040 "UP",
2041 0,
2042 "Up",
2043 "Make the bone visually point upwards so the long axis is aligned with the World/Object "
2044 "positive Z axis (depending on the choice above)"},
2045 {int(BoneAlign::AXES),
2046 "AXES",
2047 0,
2048 "Axes",
2049 "Align the new bone to match the axes of the World/Object (depending on the choice above)"},
2051 "3D_CURSOR",
2052 0,
2053 "3D Cursor",
2054 "Align new bone to match the axes of the 3D cursor"},
2055 {int(BoneAlign::VIEW_3D),
2056 "3D_VIEW",
2057 0,
2058 "Viewport",
2059 "Align new bone to match the axes of the 3D viewport"},
2060 {0, nullptr, 0, nullptr, nullptr}};
2061
2062 RNA_def_enum(ot->srna,
2063 "align",
2064 align_items,
2065 int(BoneAlign::UP),
2066 "Align",
2067 "Initial orientation of the new bone");
2068
2069 RNA_def_float(ot->srna,
2070 "length",
2071 1.0f,
2072 0.001f,
2073 FLT_MAX,
2074 "Length",
2075 "Length of the new bone",
2076 0.001f,
2077 100.0f);
2078 RNA_def_boolean(ot->srna, "use_deform", true, "Deform", "Enable bone to deform geometry");
2079}
2080
2081/* ********************** Subdivide *******************************/
2082
2083/* Subdivide Operators:
2084 * This group of operators all use the same 'exec' callback, but they are called
2085 * through several different operators - a combined menu (which just calls the exec in the
2086 * appropriate ways), and two separate ones.
2087 */
2088
2090{
2091 Object *obedit = CTX_data_edit_object(C);
2092 EditBone *newbone;
2093 int cuts, i;
2094
2095 /* there may not be a number_cuts property defined (for 'simple' subdivide) */
2096 cuts = RNA_int_get(op->ptr, "number_cuts");
2097
2098 /* loop over all editable bones */
2099 CTX_DATA_BEGIN_WITH_ID (C, EditBone *, ebone, selected_editable_bones, bArmature *, arm) {
2100 /* Keep track of the last bone in the editbone list. The newly created ones
2101 * will be appended after this one. */
2102 EditBone *last_bone_before_cutting = arm->edbo->last();
2103 BLI_assert_msg(last_bone_before_cutting,
2104 "If there is no bone before subdividing, which bone is being subdivided here?");
2105
2106 for (i = cuts + 1; i > 1; i--) {
2107 /* compute cut ratio first */
2108 float cutratio = 1.0f / float(i);
2109 float cutratioI = 1.0f - cutratio;
2110
2111 float val1[3];
2112 float val2[3];
2113 float val3[3];
2114
2115 newbone = MEM_new<EditBone>("ebone subdiv", *ebone);
2116 BLI_addtail(arm->edbo, newbone);
2117
2118 /* calculate location of newbone->head */
2119 copy_v3_v3(val1, ebone->head);
2120 copy_v3_v3(val2, ebone->tail);
2121 copy_v3_v3(val3, newbone->head);
2122
2123 val3[0] = val1[0] * cutratio + val2[0] * cutratioI;
2124 val3[1] = val1[1] * cutratio + val2[1] * cutratioI;
2125 val3[2] = val1[2] * cutratio + val2[2] * cutratioI;
2126
2127 copy_v3_v3(newbone->head, val3);
2128 copy_v3_v3(newbone->tail, ebone->tail);
2129 copy_v3_v3(ebone->tail, newbone->head);
2130
2131 newbone->rad_head = ((ebone->rad_head * cutratio) + (ebone->rad_tail * cutratioI));
2132 ebone->rad_tail = newbone->rad_head;
2133
2134 newbone->flag |= BONE_CONNECTED;
2135 newbone->prop = nullptr;
2136 newbone->system_properties = nullptr;
2137
2138 /* correct parent bones */
2139 for (EditBone &tbone : *arm->edbo) {
2140 if (tbone.parent == ebone) {
2141 tbone.parent = newbone;
2142 }
2143 }
2144 newbone->parent = ebone;
2145
2146 /* Copy bone collection membership. */
2147 BLI_duplicatelist(&newbone->bone_collections, &ebone->bone_collections);
2148 }
2149
2150 /* Ensure the bones are uniquely named, in the right order to ensure "Bone" is subdivided into
2151 * "Bone", "Bone.001", "Bone.002", etc. This has to be in the opposite order as the cuts in the
2152 * code above.
2153 *
2154 * The code above cuts into fractions (for cuts=3 it cuts into 1/4, then 1/3, then 1/2), which
2155 * means that it MUST be run in that order. Since the loop below also must run in the order it
2156 * is now in, and that's the opposite order of the loop above, they cannot be combined.
2157 *
2158 * If the code above were refactored, it could just calculate the final bone length and create
2159 * (N-1) bones of that length, which can then be done in any order. Then the code below can be
2160 * integrated into the code above.
2161 */
2162 ListBaseT<EditBone> new_bones;
2163 new_bones.first_ = last_bone_before_cutting->next;
2164 new_bones.last_ = arm->edbo->last();
2165 for (EditBone &newbone : new_bones.items_reversed()) {
2166 ED_armature_ebone_unique_name(arm->edbo, newbone.name, &newbone);
2167 }
2168 }
2170
2171 /* NOTE: notifier might evolve. */
2175
2176 return OPERATOR_FINISHED;
2177}
2178
2180{
2181 PropertyRNA *prop;
2182
2183 /* identifiers */
2184 ot->name = "Subdivide";
2185 ot->idname = "ARMATURE_OT_subdivide";
2186 ot->description = "Break selected bones into chains of smaller bones";
2187
2188 /* API callbacks. */
2191
2192 /* flags */
2194
2195 /* Properties */
2196 prop = RNA_def_int(ot->srna, "number_cuts", 1, 1, 1000, "Number of Cuts", "", 1, 10);
2197 /* Avoid re-using last var because it can cause
2198 * _very_ high poly meshes and annoy users (or worse crash) */
2200}
2201
2202} // namespace blender
Functions and classes to work with Actions.
Functions to deal with Armatures.
C++ functions to deal with Armature collections (i.e. the successor of bone layers).
Blender kernel action and pose functionality.
#define CTX_DATA_BEGIN_WITH_ID(C, Type, instance, member, Type_id, instance_id)
#define CTX_DATA_COUNT(C, member)
#define CTX_DATA_END
#define M_PI
#define STRNCPY_UTF8(dst, src)
#define DATA_(msgid)
#define MAX_ID_NAME
Definition DNA_ID.h:367
#define MAXBONENAME
Object is a sort of wrapper for general info.
#define EBONE_SELECTABLE(arm, ebone)
#define Z
#define Y
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define ND_BONE_SELECT
Definition WM_types.hh:491
#define NC_OBJECT
Definition WM_types.hh:392
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define STREQ(a, b)
Value lookup_default(const Key &key, const Value &default_value) const
Definition BLI_map.hh:570
bool add_as(ForwardKey &&key, ForwardValue &&...value)
Definition BLI_map.hh:312
constexpr const char * c_str() const
void append(const T &value)
FCurve & fcurve_clone(const FCurve &old_fcurve, StringRefNull new_path, int new_array_index, StringRef new_group_name)
Span< const FCurve * > fcurves() const
constexpr bool is_empty() const
#define FLT_MAX
float length(VecOp< float, D >) RET
bool fcurve_matches_collection_path(const FCurve &fcurve, StringRefNull collection_rna_path, StringRefNull data_name)
int armature_bonecoll_find_index(const bArmature *armature, const BoneCollection *bcoll)
const animrig::Channelbag * channelbag_for_action_slot(const Action &action, slot_handle_t slot_handle)
bool bone_is_visible(const bArmature *armature, const Bone *bone)
bool bone_is_selected(const bArmature *armature, const Bone *bone)
int armature_bonecoll_find_parent_index(const bArmature *armature, int bcoll_index)
constexpr char bone_default_name[]
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
BMesh Iterators.
bPoseChannel * BKE_pose_channel_ensure(bPose *pose, const char *name) ATTR_NONNULL(2)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void mul_m3_v3(const float M[3][3], float r[3])
int RNA_enum_get(PointerRNA *ptr, const char *name)
bool ANIM_armature_bonecoll_assign_editbone(BoneCollection *bcoll, EditBone *ebone)
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
ID * BKE_libblock_find_name(Main *bmain, short type, const char *name, const std::optional< Library * > lib=std::nullopt) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition lib_id.cc:1808
@ RPT_WARNING
Definition BKE_report.hh:40
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
wmOperatorStatus WM_operator_flag_only_pass_through_on_press(wmOperatorStatus retval, const wmEvent *event)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void BKE_constraint_targets_flush(struct bConstraint *con, ListBaseT< bConstraintTarget > *targets, bool no_copy)
void copy_m3_m4(float m1[3][3], const float m2[4][4])
static wmOperatorStatus armature_duplicate_rename_invoke(bContext *C, wmOperator *op, const wmEvent *event)
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
void ED_armature_edit_transform_mirror_update(Object *obedit)
void void void void void void BLI_duplicatelist(ListBase *dst, const ListBase *src) ATTR_NONNULL(1
Definition listbase.cc:834
bool invert_m3_m3(float inverse[3][3], const float mat[3][3])
void BKE_pose_channels_hash_free(bPose *pose) ATTR_NONNULL(1)
void ARMATURE_OT_extrude(wmOperatorType *ot)
IDProperty * IDP_CopyProperty(const IDProperty *prop) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:986
BoneCollection * ANIM_armature_bonecoll_new(bArmature *armature, const char *name, int parent_index=-1)
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
static wmOperatorStatus armature_click_extrude_exec(bContext *C, wmOperator *)
void ARMATURE_OT_duplicate(wmOperatorType *ot)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
int bool bool bool BLI_strn_endswith(const char *__restrict str, const char *__restrict end, size_t str_len) ATTR_NONNULL(1
int RNA_int_get(PointerRNA *ptr, const char *name)
static void pre_edit_bone_duplicate(ListBaseT< EditBone > *editbones)
static wmOperatorStatus armature_duplicate_selected_exec(bContext *C, wmOperator *op)
static void update_duplicate_action_constraint_settings(EditBone *dup_bone, EditBone *orig_bone, Object *ob, bPoseChannel *pchan, bConstraint *curcon)
void ED_armature_ebone_unique_name(ListBaseT< EditBone > *ebones, char *name, EditBone *bone)
void BKE_pose_channels_hash_ensure(bPose *pose) ATTR_NONNULL(1)
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
bool ED_operator_editarmature(bContext *C)
void ARMATURE_OT_duplicate_rename(wmOperatorType *ot)
MatBase< float, 4, 4 > float4x4
static void copy_pchan(EditBone *src_bone, EditBone *dst_bone, Object *src_ob, Object *dst_ob)
wmOperatorStatus WM_operator_props_popup_confirm(bContext *C, wmOperator *op, const wmEvent *)
EditBone * duplicateEditBone(EditBone *cur_bone, const char *name, ListBaseT< EditBone > *editbones, Object *ob)
static EditBone * get_symmetrized_bone(bArmature *arm, EditBone *bone)
void ARMATURE_OT_subdivide(wmOperatorType *ot)
ViewLayer * CTX_data_view_layer(const bContext *C)
void ED_armature_ebone_listbase_temp_clear(ListBaseT< EditBone > *lb)
static void mirror_bone_collection_assignments(bArmature &armature, EditBone &source_bone, EditBone &target_bone)
VecBase< float, 4 > float4
void mul_m4_v3(const float M[4][4], float r[3])
static void update_duplicate_constraint_lock_track_settings(bConstraint *curcon)
static wmOperatorStatus armature_duplicate_rename_exec(bContext *C, wmOperator *op)
void BKE_constraint_mat_convertspace(struct Object *ob, struct bPoseChannel *pchan, struct bConstraintOb *cob, float mat[4][4], short from, short to, bool keep_scale)
void BKE_pose_channel_free(bPoseChannel *pchan) ATTR_NONNULL(1)
EditBone * ED_armature_ebone_get_mirrored(const ListBaseT< EditBone > *edbo, EditBone *ebo)
void BKE_constraint_custom_object_space_init(struct bConstraintOb *cob, struct bConstraint *con)
static wmOperatorStatus armature_duplicate_selected(bContext *C, const bool do_flip_names, StringRefNull search, StringRefNull replace)
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
BMesh const char void * data
void ED_armature_edit_sync_selection(ListBaseT< EditBone > *edbo)
static bPoseChannel * pchan_duplicate_map(const bPose *pose, const Map< StringRefNull, StringRefNull > &name_map, bPoseChannel *pchan_src)
static void post_edit_bone_duplicate(ListBaseT< EditBone > *editbones, Object *ob)
Object * CTX_data_edit_object(const bContext *C)
bool ANIM_armature_bonecoll_unassign_editbone(BoneCollection *bcoll, EditBone *ebone)
bool ANIM_bonecoll_is_visible_editbone(const bArmature *armature, const EditBone *ebone)
static void update_duplicate_subtarget(EditBone *dup_bone, Object *ob, const bool lookup_mirror_subtarget)
static wmOperatorStatus armature_bone_primitive_add_exec(bContext *C, wmOperator *op)
BoneCollection * ANIM_armature_bonecoll_get_by_name(bArmature *armature, const char *name) ATTR_WARN_UNUSED_RESULT
@ ARM_DRAW_TYPE_ARMATURE_DEFINED
int BKE_constraint_targets_get(struct bConstraint *con, ListBaseT< bConstraintTarget > *r_targets)
void unit_m4(float m[4][4])
RegionView3D * CTX_wm_region_view3d(const bContext *C)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
MINLINE void copy_v4_v4_uchar(unsigned char r[4], const unsigned char a[4])
MINLINE void zero_v3(float r[3])
MatBase< float, 3, 3 > float3x3
static wmOperatorStatus armature_click_extrude_invoke(bContext *C, wmOperator *op, const wmEvent *event)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void BKE_pose_channel_copy_data(bPoseChannel *pchan, const bPoseChannel *pchan_from)
static void update_duplicate_constraint_settings(EditBone *dup_bone, EditBone *orig_bone, Object *ob)
static void update_duplicate_loc_rot_constraint_settings(Object *ob, bPoseChannel *pchan, bConstraint *curcon)
void ED_armature_ebone_copy(EditBone *dest, const EditBone *source)
MINLINE float normalize_v3(float n[3])
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
static void update_duplicate_transform_constraint_settings(Object *ob, bPoseChannel *pchan, bConstraint *curcon)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
static void update_duplicate_custom_bone_shapes(bContext *C, EditBone *dup_bone, Object *ob)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
Main * CTX_data_main(const bContext *C)
MINLINE void copy_v3_fl(float r[3], float f)
EditBone * ED_armature_ebone_add(bArmature *arm, const char *name)
void ARMATURE_OT_click_extrude(wmOperatorType *ot)
char * BLI_string_replaceN(const char *__restrict str, const char *__restrict substr_old, const char *__restrict substr_new) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
void ANIM_armature_bonecoll_assign_active(const bArmature *armature, EditBone *ebone)
static void mirror_pose_bone(Object &ob, EditBone &ebone)
static void update_duplicate_constraint_damp_track_settings(bConstraint *curcon)
@ ID_RECALC_SELECT
Definition DNA_ID.h:1101
@ ID_RECALC_ANIMATION_NO_FLUSH
Definition DNA_ID.h:1178
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
Vector< Object * > BKE_view_layer_array_from_objects_in_edit_mode_unique_data(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d)
EditBone * ED_armature_ebone_add_primitive(Object *obedit_arm, const float length, const bool view_aligned)
void ED_outliner_select_sync_from_edit_bone_tag(bContext *C)
EditBone * add_points_bone(Object *obedit, float head[3], float tail[3])
static void update_duplicate_kinematics_constraint_settings(bConstraint *curcon)
static wmOperatorStatus armature_symmetrize_exec(bContext *C, wmOperator *op)
Scene * CTX_data_scene(const bContext *C)
void ARMATURE_OT_bone_primitive_add(wmOperatorType *ot)
void vec_roll_to_mat3_normalized(const float nor[3], float roll, float r_mat[3][3])
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ OPTYPE_DEPENDS_ON_CURSOR
Definition WM_types.hh:228
std::string RNA_string_get(PointerRNA *ptr, const char *name)
VecBase< float, 3 > float3
float ED_armature_ebone_roll_to_vector(const EditBone *bone, const float align_axis[3], const bool axis_only)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void ARMATURE_OT_symmetrize(wmOperatorType *ot)
static wmOperatorStatus armature_extrude_exec(bContext *C, wmOperator *op)
MINLINE void copy_v2fl_v2i(float r[2], const int a[2])
EditBone * duplicateEditBoneObjects(EditBone *cur_bone, const char *name, ListBaseT< EditBone > *editbones, Object *src_ob, Object *dst_ob)
void BLI_string_replace(std::string &haystack, StringRef needle, StringRef other)
void ED_view3d_win_to_3d(const View3D *v3d, const ARegion *region, const float depth_pt[3], const float mval[2], float r_out[3])
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
ARegion * CTX_wm_region(const bContext *C)
void ED_armature_ebone_select_set(EditBone *ebone, bool select)
static wmOperatorStatus armature_subdivide_exec(bContext *C, wmOperator *op)
static void track_axis_x_swap(eTrackToAxis_Modes &value)
float RNA_float_get(PointerRNA *ptr, const char *name)
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
bool RNA_property_is_set(PointerRNA *ptr, PropertyRNA *prop)
View3D * CTX_wm_view3d(const bContext *C)
static void update_duplicate_constraint_track_to_settings(bConstraint *curcon)
char char size_t char * BLI_strncat(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
EditBone * ED_armature_ebone_find_name(const ListBaseT< EditBone > *edbo, const char *name)
static void update_duplicate_constraint_shrinkwrap_settings(bConstraint *curcon)
bool ED_armature_edit_deselect_all(Object *obedit)
size_t BLI_string_flip_side_name(char *name_dst, const char *name_src, bool strip_number, size_t name_dst_maxncpy) ATTR_NONNULL(1
float angle_wrap_rad(float angle)
#define X(TYPE)
#define fabsf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
unsigned char select[4]
unsigned char solid[4]
unsigned char active[4]
union blender::EditBone::@252003325204232231327014100071366025164067232113 temp
ListBaseT< BoneCollectionReference > bone_collections
eBone_BBoneHandleFlag bbone_next_flag
eBone_BBoneMappingMode bbone_mapping_mode
eBone_BBoneFlag bbone_flag
IDProperty * system_properties
eBone_InheritScaleMode inherit_scale_mode
eBone_BBoneHandleType bbone_next_type
EditBone * bbone_prev
EditBone * bbone_next
eBone_BBoneHandleFlag bbone_prev_flag
eBone_BBoneHandleType bbone_prev_type
IDProperty * prop
unsigned int totvert
char name[258]
Definition DNA_ID.h:433
const c_style_mat & ptr() const
bke::ObjectRuntime * runtime
struct bPose * pose
View3DCursor cursor
ListBaseT< EditBone > * edbo
struct EditBone * act_edbone
bke::bArmature_Runtime * runtime
struct bPoseChannel * pchan
struct Depsgraph * depsgraph
eBConstraint_SpaceTypes ownspace
eBConstraint_SpaceTypes tarspace
ListBaseT< struct bConstraint > constraints
struct bPoseChannel * custom_tx
struct bPoseChannel * bbone_next
struct bPoseChannel * bbone_prev
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
struct ReportList * reports