Blender  V2.93
armature_relations.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  * Operators for relations between bones and for transferring bones between armature objects
19  */
20 
25 #include "MEM_guardedalloc.h"
26 
27 #include "DNA_anim_types.h"
28 #include "DNA_armature_types.h"
29 #include "DNA_constraint_types.h"
30 #include "DNA_object_types.h"
31 #include "DNA_scene_types.h"
32 
33 #include "BLI_blenlib.h"
34 #include "BLI_ghash.h"
35 #include "BLI_math.h"
36 
37 #include "BLT_translation.h"
38 
39 #include "BKE_action.h"
40 #include "BKE_anim_data.h"
41 #include "BKE_animsys.h"
42 #include "BKE_armature.h"
43 #include "BKE_constraint.h"
44 #include "BKE_context.h"
45 #include "BKE_fcurve_driver.h"
46 #include "BKE_layer.h"
47 #include "BKE_main.h"
48 #include "BKE_report.h"
49 
50 #include "DEG_depsgraph.h"
51 #include "DEG_depsgraph_build.h"
52 
53 #include "RNA_access.h"
54 #include "RNA_define.h"
55 
56 #include "WM_api.h"
57 #include "WM_types.h"
58 
59 #include "ED_armature.h"
60 #include "ED_object.h"
61 #include "ED_outliner.h"
62 #include "ED_screen.h"
63 
64 #include "UI_interface.h"
65 #include "UI_resources.h"
66 
67 #include "armature_intern.h"
68 
69 /* -------------------------------------------------------------------- */
76  Object *ob,
77  Object *tarArm,
78  Object *srcArm,
79  bPoseChannel *pchan,
80  EditBone *curbone,
81  ListBase *lb)
82 {
83  bConstraint *con;
84  bool changed = false;
85 
86  for (con = lb->first; con; con = con->next) {
88  ListBase targets = {NULL, NULL};
90 
91  /* constraint targets */
92  if (cti && cti->get_constraint_targets) {
93  cti->get_constraint_targets(con, &targets);
94 
95  for (ct = targets.first; ct; ct = ct->next) {
96  if (ct->tar == srcArm) {
97  if (ct->subtarget[0] == '\0') {
98  ct->tar = tarArm;
99  changed = true;
100  }
101  else if (STREQ(ct->subtarget, pchan->name)) {
102  ct->tar = tarArm;
103  BLI_strncpy(ct->subtarget, curbone->name, sizeof(ct->subtarget));
104  changed = true;
105  }
106  }
107  }
108 
109  if (cti->flush_constraint_targets) {
110  cti->flush_constraint_targets(con, &targets, 0);
111  }
112  }
113 
114  /* action constraint? (pose constraints only) */
115  if (con->type == CONSTRAINT_TYPE_ACTION) {
116  bActionConstraint *data = con->data;
117 
118  if (data->act) {
120  &tarArm->id, data->act, "pose.bones[", pchan->name, curbone->name, 0, 0, false);
121 
123  }
124  }
125  }
126 
127  if (changed) {
129  }
130 }
131 
132 /* userdata for joined_armature_fix_animdata_cb() */
133 typedef struct tJoinArmature_AdtFixData {
135 
138 
141 
142 /* Callback to pass to BKE_animdata_main_cb() for fixing driver ID's to point to the new ID. */
143 /* FIXME: For now, we only care about drivers here.
144  * When editing rigs, it's very rare to have animation on the rigs being edited already,
145  * so it should be safe to skip these.
146  */
147 static void joined_armature_fix_animdata_cb(ID *id, FCurve *fcu, void *user_data)
148 {
150  ID *src_id = &afd->srcArm->id;
151  ID *dst_id = &afd->tarArm->id;
152 
153  GHashIterator gh_iter;
154  bool changed = false;
155 
156  /* Fix paths - If this is the target object, it will have some "dirty" paths */
157  if ((id == src_id) && strstr(fcu->rna_path, "pose.bones[")) {
158  GHASH_ITER (gh_iter, afd->names_map) {
159  const char *old_name = BLI_ghashIterator_getKey(&gh_iter);
160  const char *new_name = BLI_ghashIterator_getValue(&gh_iter);
161 
162  /* only remap if changed; this still means there will be some
163  * waste if there aren't many drivers/keys */
164  if (!STREQ(old_name, new_name) && strstr(fcu->rna_path, old_name)) {
166  id, fcu->rna_path, "pose.bones", old_name, new_name, 0, 0, false);
167 
168  changed = true;
169 
170  /* we don't want to apply a second remapping on this driver now,
171  * so stop trying names, but keep fixing drivers
172  */
173  break;
174  }
175  }
176  }
177 
178  /* Driver targets */
179  if (fcu->driver) {
180  ChannelDriver *driver = fcu->driver;
181  DriverVar *dvar;
182 
183  /* Fix driver references to invalid ID's */
184  for (dvar = driver->variables.first; dvar; dvar = dvar->next) {
185  /* only change the used targets, since the others will need fixing manually anyway */
187  /* change the ID's used... */
188  if (dtar->id == src_id) {
189  dtar->id = dst_id;
190 
191  changed = true;
192 
193  /* also check on the subtarget...
194  * XXX: We duplicate the logic from drivers_path_rename_fix() here, with our own
195  * little twists so that we know that it isn't going to clobber the wrong data
196  */
197  if ((dtar->rna_path && strstr(dtar->rna_path, "pose.bones[")) || (dtar->pchan_name[0])) {
198  GHASH_ITER (gh_iter, afd->names_map) {
199  const char *old_name = BLI_ghashIterator_getKey(&gh_iter);
200  const char *new_name = BLI_ghashIterator_getValue(&gh_iter);
201 
202  /* only remap if changed */
203  if (!STREQ(old_name, new_name)) {
204  if ((dtar->rna_path) && strstr(dtar->rna_path, old_name)) {
205  /* Fix up path */
206  dtar->rna_path = BKE_animsys_fix_rna_path_rename(
207  id, dtar->rna_path, "pose.bones", old_name, new_name, 0, 0, false);
208  break; /* no need to try any more names for bone path */
209  }
210  if (STREQ(dtar->pchan_name, old_name)) {
211  /* Change target bone name */
212  BLI_strncpy(dtar->pchan_name, new_name, sizeof(dtar->pchan_name));
213  break; /* no need to try any more names for bone subtarget */
214  }
215  }
216  }
217  }
218  }
219  }
221  }
222  }
223 
224  if (changed) {
226  }
227 }
228 
229 /* Helper function for armature joining - link fixing */
231  Main *bmain, Object *tarArm, Object *srcArm, bPoseChannel *pchan, EditBone *curbone)
232 {
233  Object *ob;
234  bPose *pose;
235  bPoseChannel *pchant;
236 
237  /* let's go through all objects in database */
238  for (ob = bmain->objects.first; ob; ob = ob->id.next) {
239  /* do some object-type specific things */
240  if (ob->type == OB_ARMATURE) {
241  pose = ob->pose;
242  for (pchant = pose->chanbase.first; pchant; pchant = pchant->next) {
244  bmain, ob, tarArm, srcArm, pchan, curbone, &pchant->constraints);
245  }
246  }
247 
248  /* fix object-level constraints */
249  if (ob != srcArm) {
251  bmain, ob, tarArm, srcArm, pchan, curbone, &ob->constraints);
252  }
253 
254  /* See if an object is parented to this armature */
255  if (ob->parent && (ob->parent == srcArm)) {
256  /* Is object parented to a bone of this src armature? */
257  if (ob->partype == PARBONE) {
258  /* bone name in object */
259  if (STREQ(ob->parsubstr, pchan->name)) {
260  BLI_strncpy(ob->parsubstr, curbone->name, sizeof(ob->parsubstr));
261  }
262  }
263 
264  /* make tar armature be new parent */
265  ob->parent = tarArm;
266 
268  }
269  }
270 }
271 
272 /* join armature exec is exported for use in object->join objects operator... */
274 {
275  Main *bmain = CTX_data_main(C);
277  Object *ob_active = CTX_data_active_object(C);
278  bArmature *arm = (ob_active) ? ob_active->data : NULL;
279  bPose *pose, *opose;
280  bPoseChannel *pchan, *pchann;
281  EditBone *curbone;
282  float mat[4][4], oimat[4][4];
283  bool ok = false;
284 
285  /* Ensure we're not in edit-mode and that the active object is an armature. */
286  if (!ob_active || ob_active->type != OB_ARMATURE) {
287  return OPERATOR_CANCELLED;
288  }
289  if (!arm || arm->edbo) {
290  return OPERATOR_CANCELLED;
291  }
292 
293  CTX_DATA_BEGIN (C, Object *, ob_iter, selected_editable_objects) {
294  if (ob_iter == ob_active) {
295  ok = true;
296  break;
297  }
298  }
299  CTX_DATA_END;
300 
301  /* that way the active object is always selected */
302  if (ok == false) {
303  BKE_report(op->reports, RPT_WARNING, "Active object is not a selected armature");
304  return OPERATOR_CANCELLED;
305  }
306 
307  /* Inverse transform for all selected armatures in this object,
308  * See #object_join_exec for detailed comment on why the safe version is used. */
309  invert_m4_m4_safe_ortho(oimat, ob_active->obmat);
310 
311  /* Get edit-bones of active armature to add edit-bones to */
312  ED_armature_to_edit(arm);
313 
314  /* Get pose of active object and move it out of pose-mode */
315  pose = ob_active->pose;
316  ob_active->mode &= ~OB_MODE_POSE;
317 
318  CTX_DATA_BEGIN (C, Object *, ob_iter, selected_editable_objects) {
319  if ((ob_iter->type == OB_ARMATURE) && (ob_iter != ob_active)) {
321  bArmature *curarm = ob_iter->data;
322 
323  /* we assume that each armature datablock is only used in a single place */
324  BLI_assert(ob_active->data != ob_iter->data);
325 
326  /* init callback data for fixing up AnimData links later */
327  afd.bmain = bmain;
328  afd.srcArm = ob_iter;
329  afd.tarArm = ob_active;
330  afd.names_map = BLI_ghash_str_new("join_armature_adt_fix");
331 
332  /* Make a list of edit-bones in current armature */
333  ED_armature_to_edit(ob_iter->data);
334 
335  /* Get Pose of current armature */
336  opose = ob_iter->pose;
337  ob_iter->mode &= ~OB_MODE_POSE;
338  // BASACT->flag &= ~OB_MODE_POSE;
339 
340  /* Find the difference matrix */
341  mul_m4_m4m4(mat, oimat, ob_iter->obmat);
342 
343  /* Copy bones and posechannels from the object to the edit armature */
344  for (pchan = opose->chanbase.first; pchan; pchan = pchann) {
345  pchann = pchan->next;
346  curbone = ED_armature_ebone_find_name(curarm->edbo, pchan->name);
347 
348  /* Get new name */
349  ED_armature_ebone_unique_name(arm->edbo, curbone->name, NULL);
350  BLI_ghash_insert(afd.names_map, BLI_strdup(pchan->name), curbone->name);
351 
352  /* Transform the bone */
353  {
354  float premat[4][4];
355  float postmat[4][4];
356  float difmat[4][4];
357  float imat[4][4];
358  float temp[3][3];
359 
360  /* Get the premat */
361  ED_armature_ebone_to_mat3(curbone, temp);
362 
363  unit_m4(premat); /* mul_m4_m3m4 only sets 3x3 part */
364  mul_m4_m3m4(premat, temp, mat);
365 
366  mul_m4_v3(mat, curbone->head);
367  mul_m4_v3(mat, curbone->tail);
368 
369  /* Get the postmat */
370  ED_armature_ebone_to_mat3(curbone, temp);
371  copy_m4_m3(postmat, temp);
372 
373  /* Find the roll */
374  invert_m4_m4(imat, premat);
375  mul_m4_m4m4(difmat, imat, postmat);
376 
377  curbone->roll -= atan2f(difmat[2][0], difmat[2][2]);
378  }
379 
380  /* Fix Constraints and Other Links to this Bone and Armature */
381  joined_armature_fix_links(bmain, ob_active, ob_iter, pchan, curbone);
382 
383  /* Rename pchan */
384  BLI_strncpy(pchan->name, curbone->name, sizeof(pchan->name));
385 
386  /* Jump Ship! */
387  BLI_remlink(curarm->edbo, curbone);
388  BLI_addtail(arm->edbo, curbone);
389 
390  /* Pose channel is moved from one storage to another, its UUID is still unique. */
391  BLI_remlink(&opose->chanbase, pchan);
392  BLI_addtail(&pose->chanbase, pchan);
395  }
396 
397  /* Fix all the drivers (and animation data) */
400 
401  /* Only copy over animdata now, after all the remapping has been done,
402  * so that we don't have to worry about ambiguities re which armature
403  * a bone came from!
404  */
405  if (ob_iter->adt) {
406  if (ob_active->adt == NULL) {
407  /* no animdata, so just use a copy of the whole thing */
408  ob_active->adt = BKE_animdata_copy(bmain, ob_iter->adt, 0);
409  }
410  else {
411  /* merge in data - we'll fix the drivers manually */
413  bmain, &ob_active->id, &ob_iter->id, ADT_MERGECOPY_KEEP_DST, false);
414  }
415  }
416 
417  if (curarm->adt) {
418  if (arm->adt == NULL) {
419  /* no animdata, so just use a copy of the whole thing */
420  arm->adt = BKE_animdata_copy(bmain, curarm->adt, 0);
421  }
422  else {
423  /* merge in data - we'll fix the drivers manually */
424  BKE_animdata_merge_copy(bmain, &arm->id, &curarm->id, ADT_MERGECOPY_KEEP_DST, false);
425  }
426  }
427 
428  /* Free the old object data */
429  ED_object_base_free_and_unlink(bmain, scene, ob_iter);
430  }
431  }
432  CTX_DATA_END;
433 
434  DEG_relations_tag_update(bmain); /* because we removed object(s) */
435 
436  ED_armature_from_edit(bmain, arm);
438 
442 
443  return OPERATOR_FINISHED;
444 }
445 
448 /* -------------------------------------------------------------------- */
452 /* Helper function for armature separating - link fixing */
453 static void separated_armature_fix_links(Main *bmain, Object *origArm, Object *newArm)
454 {
455  Object *ob;
456  bPoseChannel *pchan;
457  bConstraint *con;
458  ListBase *opchans, *npchans;
459 
460  /* get reference to list of bones in original and new armatures */
461  opchans = &origArm->pose->chanbase;
462  npchans = &newArm->pose->chanbase;
463 
464  /* let's go through all objects in database */
465  for (ob = bmain->objects.first; ob; ob = ob->id.next) {
466  /* do some object-type specific things */
467  if (ob->type == OB_ARMATURE) {
468  for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
469  for (con = pchan->constraints.first; con; con = con->next) {
471  ListBase targets = {NULL, NULL};
472  bConstraintTarget *ct;
473 
474  /* constraint targets */
475  if (cti && cti->get_constraint_targets) {
476  cti->get_constraint_targets(con, &targets);
477 
478  for (ct = targets.first; ct; ct = ct->next) {
479  /* Any targets which point to original armature
480  * are redirected to the new one only if:
481  * - The target isn't origArm/newArm itself.
482  * - The target is one that can be found in newArm/origArm.
483  */
484  if (ct->subtarget[0] != 0) {
485  if (ct->tar == origArm) {
486  if (BLI_findstring(npchans, ct->subtarget, offsetof(bPoseChannel, name))) {
487  ct->tar = newArm;
488  }
489  }
490  else if (ct->tar == newArm) {
491  if (BLI_findstring(opchans, ct->subtarget, offsetof(bPoseChannel, name))) {
492  ct->tar = origArm;
493  }
494  }
495  }
496  }
497 
498  if (cti->flush_constraint_targets) {
499  cti->flush_constraint_targets(con, &targets, 0);
500  }
501  }
502  }
503  }
504  }
505 
506  /* fix object-level constraints */
507  if (ob != origArm) {
508  for (con = ob->constraints.first; con; con = con->next) {
510  ListBase targets = {NULL, NULL};
511  bConstraintTarget *ct;
512 
513  /* constraint targets */
514  if (cti && cti->get_constraint_targets) {
515  cti->get_constraint_targets(con, &targets);
516 
517  for (ct = targets.first; ct; ct = ct->next) {
518  /* any targets which point to original armature are redirected to the new one only if:
519  * - the target isn't origArm/newArm itself
520  * - the target is one that can be found in newArm/origArm
521  */
522  if (ct->subtarget[0] != '\0') {
523  if (ct->tar == origArm) {
524  if (BLI_findstring(npchans, ct->subtarget, offsetof(bPoseChannel, name))) {
525  ct->tar = newArm;
526  }
527  }
528  else if (ct->tar == newArm) {
529  if (BLI_findstring(opchans, ct->subtarget, offsetof(bPoseChannel, name))) {
530  ct->tar = origArm;
531  }
532  }
533  }
534  }
535 
536  if (cti->flush_constraint_targets) {
537  cti->flush_constraint_targets(con, &targets, 0);
538  }
539  }
540  }
541  }
542 
543  /* See if an object is parented to this armature */
544  if (ob->parent && (ob->parent == origArm)) {
545  /* Is object parented to a bone of this src armature? */
546  if ((ob->partype == PARBONE) && (ob->parsubstr[0] != '\0')) {
547  if (BLI_findstring(npchans, ob->parsubstr, offsetof(bPoseChannel, name))) {
548  ob->parent = newArm;
549  }
550  }
551  }
552  }
553 }
554 
562 static void separate_armature_bones(Main *bmain, Object *ob, const bool is_select)
563 {
564  bArmature *arm = (bArmature *)ob->data;
565  bPoseChannel *pchan, *pchann;
566  EditBone *curbone;
567 
568  /* make local set of edit-bones to manipulate here */
569  ED_armature_to_edit(arm);
570 
571  /* go through pose-channels, checking if a bone should be removed */
572  for (pchan = ob->pose->chanbase.first; pchan; pchan = pchann) {
573  pchann = pchan->next;
574  curbone = ED_armature_ebone_find_name(arm->edbo, pchan->name);
575 
576  /* check if bone needs to be removed */
577  if (is_select == (EBONE_VISIBLE(arm, curbone) && (curbone->flag & BONE_SELECTED))) {
578 
579  /* clear the bone->parent var of any bone that had this as its parent */
580  LISTBASE_FOREACH (EditBone *, ebo, arm->edbo) {
581  if (ebo->parent == curbone) {
582  ebo->parent = NULL;
583  /* this is needed to prevent random crashes with in ED_armature_from_edit */
584  ebo->temp.p = NULL;
585  ebo->flag &= ~BONE_CONNECTED;
586  }
587  }
588 
589  /* clear the pchan->parent var of any pchan that had this as its parent */
590  LISTBASE_FOREACH (bPoseChannel *, pchn, &ob->pose->chanbase) {
591  if (pchn->parent == pchan) {
592  pchn->parent = NULL;
593  }
594  if (pchn->bbone_next == pchan) {
595  pchn->bbone_next = NULL;
596  }
597  if (pchn->bbone_prev == pchan) {
598  pchn->bbone_prev = NULL;
599  }
600  }
601 
602  /* Free any of the extra-data this pchan might have. */
603  BKE_pose_channel_free(pchan);
605 
606  /* get rid of unneeded bone */
607  bone_free(arm, curbone);
608  BLI_freelinkN(&ob->pose->chanbase, pchan);
609  }
610  }
611 
612  /* Exit edit-mode (recalculates pose-channels too). */
614  ED_armature_from_edit(bmain, ob->data);
616 }
617 
618 /* separate selected bones into their armature */
620 {
621  Main *bmain = CTX_data_main(C);
623  ViewLayer *view_layer = CTX_data_view_layer(C);
624  bool ok = false;
625 
626  /* set wait cursor in case this takes a while */
627  WM_cursor_wait(true);
628 
629  uint bases_len = 0;
631  view_layer, CTX_wm_view3d(C), &bases_len);
632 
633  for (uint base_index = 0; base_index < bases_len; base_index++) {
634  Base *base_old = bases[base_index];
635  Object *ob_old = base_old->object;
636 
637  {
638  bArmature *arm_old = ob_old->data;
639  bool has_selected_bone = false;
640  bool has_selected_any = false;
641  LISTBASE_FOREACH (EditBone *, ebone, arm_old->edbo) {
642  if (EBONE_VISIBLE(arm_old, ebone)) {
643  if (ebone->flag & BONE_SELECTED) {
644  has_selected_bone = true;
645  break;
646  }
647  if (ebone->flag & (BONE_TIPSEL | BONE_ROOTSEL)) {
648  has_selected_any = true;
649  }
650  }
651  }
652  if (has_selected_bone == false) {
653  if (has_selected_any) {
654  /* Without this, we may leave head/tail selected
655  * which isn't expected after separating. */
657  }
658  continue;
659  }
660  }
661 
662  /* We are going to do this as follows (unlike every other instance of separate):
663  * 1. Exit edit-mode & pose-mode for active armature/base. Take note of what this is.
664  * 2. Duplicate base - BASACT is the new one now
665  * 3. For each of the two armatures,
666  * enter edit-mode -> remove appropriate bones -> exit edit-mode + recalculate.
667  * 4. Fix constraint links
668  * 5. Make original armature active and enter edit-mode
669  */
670 
671  /* 1) store starting settings and exit edit-mode */
672  ob_old->mode &= ~OB_MODE_POSE;
673 
674  ED_armature_from_edit(bmain, ob_old->data);
675  ED_armature_edit_free(ob_old->data);
676 
677  /* 2) duplicate base */
678 
679  /* Only duplicate linked armature but take into account
680  * user preferences for duplicating actions. */
681  short dupflag = USER_DUP_ARM | (U.dupflag & USER_DUP_ACT);
682  Base *base_new = ED_object_add_duplicate(bmain, scene, view_layer, base_old, dupflag);
683  Object *ob_new = base_new->object;
684 
686 
687  /* 3) remove bones that shouldn't still be around on both armatures */
688  separate_armature_bones(bmain, ob_old, true);
689  separate_armature_bones(bmain, ob_new, false);
690 
691  /* 4) fix links before depsgraph flushes, err... or after? */
692  separated_armature_fix_links(bmain, ob_old, ob_new);
693 
694  DEG_id_tag_update(&ob_old->id, ID_RECALC_GEOMETRY); /* this is the original one */
695  DEG_id_tag_update(&ob_new->id, ID_RECALC_GEOMETRY); /* this is the separated one */
696 
697  /* 5) restore original conditions */
698  ED_armature_to_edit(ob_old->data);
700 
701  /* parents tips remain selected when connected children are removed. */
703 
704  ok = true;
705 
706  /* note, notifier might evolve */
708  }
709  MEM_freeN(bases);
710 
711  /* Recalculate/redraw + cleanup */
712  WM_cursor_wait(false);
713 
714  if (ok) {
715  BKE_report(op->reports, RPT_INFO, "Separated bones");
717  }
718 
719  return OPERATOR_FINISHED;
720 }
721 
723 {
724  /* identifiers */
725  ot->name = "Separate Bones";
726  ot->idname = "ARMATURE_OT_separate";
727  ot->description = "Isolate selected bones into a separate armature";
728 
729  /* callbacks */
733 
734  /* flags */
736 }
737 
740 /* -------------------------------------------------------------------- */
744 /* armature parenting options */
745 #define ARM_PAR_CONNECT 1
746 #define ARM_PAR_OFFSET 2
747 
748 /* check for null, before calling! */
750 {
751  bone->flag |= BONE_CONNECTED;
752  copy_v3_v3(bone->head, bone->parent->tail);
753  bone->rad_head = bone->parent->rad_tail;
754 }
755 
757  EditBone *selbone,
758  EditBone *actbone,
759  short mode)
760 {
761  EditBone *ebone;
762  float offset[3];
763 
764  if ((selbone->parent) && (selbone->flag & BONE_CONNECTED)) {
765  selbone->parent->flag &= ~BONE_TIPSEL;
766  }
767 
768  /* make actbone the parent of selbone */
769  selbone->parent = actbone;
770 
771  /* in actbone tree we cannot have a loop */
772  for (ebone = actbone->parent; ebone; ebone = ebone->parent) {
773  if (ebone->parent == selbone) {
774  ebone->parent = NULL;
775  ebone->flag &= ~BONE_CONNECTED;
776  }
777  }
778 
779  if (mode == ARM_PAR_CONNECT) {
780  /* Connected: Child bones will be moved to the parent tip */
781  selbone->flag |= BONE_CONNECTED;
782  sub_v3_v3v3(offset, actbone->tail, selbone->head);
783 
784  copy_v3_v3(selbone->head, actbone->tail);
785  selbone->rad_head = actbone->rad_tail;
786 
787  add_v3_v3(selbone->tail, offset);
788 
789  /* offset for all its children */
790  for (ebone = edbo->first; ebone; ebone = ebone->next) {
791  EditBone *par;
792 
793  for (par = ebone->parent; par; par = par->parent) {
794  if (par == selbone) {
795  add_v3_v3(ebone->head, offset);
796  add_v3_v3(ebone->tail, offset);
797  break;
798  }
799  }
800  }
801  }
802  else {
803  /* Offset: Child bones will retain their distance from the parent tip */
804  selbone->flag &= ~BONE_CONNECTED;
805  }
806 }
807 
809  {ARM_PAR_CONNECT, "CONNECTED", 0, "Connected", ""},
810  {ARM_PAR_OFFSET, "OFFSET", 0, "Keep Offset", ""},
811  {0, NULL, 0, NULL, NULL},
812 };
813 
815 {
817  bArmature *arm = (bArmature *)ob->data;
818  EditBone *actbone = CTX_data_active_bone(C);
819  EditBone *actmirb = NULL;
820  short val = RNA_enum_get(op->ptr, "type");
821 
822  /* there must be an active bone */
823  if (actbone == NULL) {
824  BKE_report(op->reports, RPT_ERROR, "Operation requires an active bone");
825  return OPERATOR_CANCELLED;
826  }
827  if (arm->flag & ARM_MIRROR_EDIT) {
828  /* For X-Axis Mirror Editing option, we may need a mirror copy of actbone:
829  * - If there's a mirrored copy of selbone, try to find a mirrored copy of actbone
830  * (i.e. selbone="child.L" and actbone="parent.L", find "child.R" and "parent.R").
831  * This is useful for arm-chains, for example parenting lower arm to upper arm.
832  * - If there's no mirrored copy of actbone (i.e. actbone = "parent.C" or "parent")
833  * then just use actbone. Useful when doing upper arm to spine.
834  */
835  actmirb = ED_armature_ebone_get_mirrored(arm->edbo, actbone);
836  if (actmirb == NULL) {
837  actmirb = actbone;
838  }
839  }
840 
841  /* If there is only 1 selected bone, we assume that that is the active bone,
842  * since a user will need to have clicked on a bone (thus selecting it) to make it active. */
843  bool is_active_only_selected = false;
844  if (actbone->flag & BONE_SELECTED) {
845  is_active_only_selected = true;
846  LISTBASE_FOREACH (EditBone *, ebone, arm->edbo) {
847  if (EBONE_EDITABLE(ebone) && (ebone->flag & BONE_SELECTED)) {
848  if (ebone != actbone) {
849  is_active_only_selected = false;
850  break;
851  }
852  }
853  }
854  }
855 
856  if (is_active_only_selected) {
857  /* When only the active bone is selected, and it has a parent,
858  * connect it to the parent, as that is the only possible outcome.
859  */
860  if (actbone->parent) {
862 
863  if ((arm->flag & ARM_MIRROR_EDIT) && (actmirb->parent)) {
865  }
866  }
867  }
868  else {
869  /* Parent 'selected' bones to the active one:
870  * - The context iterator contains both selected bones and their mirrored copies,
871  * so we assume that unselected bones are mirrored copies of some selected bone.
872  * - Since the active one (and/or its mirror) will also be selected, we also need
873  * to check that we are not trying to operate on them, since such an operation
874  * would cause errors.
875  */
876 
877  /* Parent selected bones to the active one. */
878  LISTBASE_FOREACH (EditBone *, ebone, arm->edbo) {
879  if (EBONE_EDITABLE(ebone) && (ebone->flag & BONE_SELECTED)) {
880  if (ebone != actbone) {
881  bone_connect_to_new_parent(arm->edbo, ebone, actbone, val);
882  }
883 
884  if (arm->flag & ARM_MIRROR_EDIT) {
885  EditBone *ebone_mirror = ED_armature_ebone_get_mirrored(arm->edbo, ebone);
886  if (ebone_mirror && (ebone_mirror->flag & BONE_SELECTED) == 0) {
887  if (ebone_mirror != actmirb) {
888  bone_connect_to_new_parent(arm->edbo, ebone_mirror, actmirb, val);
889  }
890  }
891  }
892  }
893  }
894  }
895 
896  /* note, notifier might evolve */
899 
900  return OPERATOR_FINISHED;
901 }
902 
904  wmOperator *UNUSED(op),
905  const wmEvent *UNUSED(event))
906 {
907  bool all_childbones = false;
908  {
910  bArmature *arm = ob->data;
911  EditBone *actbone = arm->act_edbone;
912  LISTBASE_FOREACH (EditBone *, ebone, arm->edbo) {
913  if (EBONE_EDITABLE(ebone) && (ebone->flag & BONE_SELECTED)) {
914  if (ebone != actbone) {
915  if (ebone->parent != actbone) {
916  all_childbones = true;
917  break;
918  }
919  }
920  }
921  }
922  }
923 
925  C, CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Make Parent"), ICON_NONE);
926  uiLayout *layout = UI_popup_menu_layout(pup);
927  uiItemEnumO(layout, "ARMATURE_OT_parent_set", NULL, 0, "type", ARM_PAR_CONNECT);
928  if (all_childbones) {
929  /* Object becomes parent, make the associated menus. */
930  uiItemEnumO(layout, "ARMATURE_OT_parent_set", NULL, 0, "type", ARM_PAR_OFFSET);
931  }
932 
933  UI_popup_menu_end(C, pup);
934 
935  return OPERATOR_INTERFACE;
936 }
937 
939 {
940  /* identifiers */
941  ot->name = "Make Parent";
942  ot->idname = "ARMATURE_OT_parent_set";
943  ot->description = "Set the active bone as the parent of the selected bones";
944 
945  /* api callbacks */
949 
950  /* flags */
952 
953  RNA_def_enum(
954  ot->srna, "type", prop_editarm_make_parent_types, 0, "Parent Type", "Type of parenting");
955 }
956 
958  {1, "CLEAR", 0, "Clear Parent", ""},
959  {2, "DISCONNECT", 0, "Disconnect Bone", ""},
960  {0, NULL, 0, NULL, NULL},
961 };
962 
963 static void editbone_clear_parent(EditBone *ebone, int mode)
964 {
965  if (ebone->parent) {
966  /* for nice selection */
967  ebone->parent->flag &= ~BONE_TIPSEL;
968  }
969 
970  if (mode == 1) {
971  ebone->parent = NULL;
972  }
973  ebone->flag &= ~BONE_CONNECTED;
974 }
975 
977 {
978  ViewLayer *view_layer = CTX_data_view_layer(C);
979  const int val = RNA_enum_get(op->ptr, "type");
980 
981  CTX_DATA_BEGIN (C, EditBone *, ebone, selected_editable_bones) {
982  editbone_clear_parent(ebone, val);
983  }
984  CTX_DATA_END;
985 
986  uint objects_len = 0;
988  view_layer, CTX_wm_view3d(C), &objects_len);
989  for (uint ob_index = 0; ob_index < objects_len; ob_index++) {
990  Object *ob = objects[ob_index];
991  bArmature *arm = ob->data;
992  bool changed = false;
993 
994  LISTBASE_FOREACH (EditBone *, ebone, arm->edbo) {
995  if (EBONE_EDITABLE(ebone)) {
996  changed = true;
997  break;
998  }
999  }
1000 
1001  if (!changed) {
1002  continue;
1003  }
1004 
1006 
1007  /* Note, notifier might evolve. */
1009  }
1010  MEM_freeN(objects);
1011 
1012  return OPERATOR_FINISHED;
1013 }
1014 
1016 {
1017  /* identifiers */
1018  ot->name = "Clear Parent";
1019  ot->idname = "ARMATURE_OT_parent_clear";
1020  ot->description =
1021  "Remove the parent-child relationship between selected bones and their parents";
1022 
1023  /* api callbacks */
1027 
1028  /* flags */
1030 
1031  ot->prop = RNA_def_enum(ot->srna,
1032  "type",
1034  0,
1035  "Clear Type",
1036  "What way to clear parenting");
1037 }
1038 
Blender kernel action and pose functionality.
void BKE_pose_channels_hash_free(struct bPose *pose)
Definition: action.c:960
void BKE_pose_channel_free(struct bPoseChannel *pchan)
Definition: action.c:1117
@ ADT_MERGECOPY_KEEP_DST
Definition: BKE_anim_data.h:92
void BKE_animdata_merge_copy(struct Main *bmain, struct ID *dst_id, struct ID *src_id, eAnimData_MergeCopy_Modes action_mode, bool fix_drivers)
Definition: anim_data.c:436
struct AnimData * BKE_animdata_copy(struct Main *bmain, struct AnimData *adt, const int flag)
Definition: anim_data.c:318
void BKE_fcurves_main_cb(struct Main *bmain, ID_FCurve_Edit_Callback func, void *user_data)
Definition: anim_data.c:1288
char * BKE_animsys_fix_rna_path_rename(struct ID *owner_id, char *old_path, const char *prefix, const char *oldName, const char *newName, int oldSubscript, int newSubscript, bool verify_paths)
Definition: anim_data.c:969
void BKE_action_fix_paths_rename(struct ID *owner_id, struct bAction *act, const char *prefix, const char *oldName, const char *newName, int oldSubscript, int newSubscript, bool verify_paths)
Definition: anim_data.c:1032
const bConstraintTypeInfo * BKE_constraint_typeinfo_get(struct bConstraint *con)
Definition: constraint.c:5435
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1034
struct Object * CTX_data_edit_object(const bContext *C)
Definition: context.c:1296
#define CTX_DATA_BEGIN(C, Type, instance, member)
Definition: BKE_context.h:252
struct ViewLayer * CTX_data_view_layer(const bContext *C)
Definition: context.c:1044
struct Object * CTX_data_active_object(const bContext *C)
Definition: context.c:1279
struct View3D * CTX_wm_view3d(const bContext *C)
Definition: context.c:760
struct EditBone * CTX_data_active_bone(const bContext *C)
Definition: context.c:1321
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1018
#define CTX_DATA_END
Definition: BKE_context.h:260
#define DRIVER_TARGETS_USED_LOOPER_BEGIN(dvar)
#define DRIVER_TARGETS_LOOPER_END
#define BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, v3d, r_len)
Definition: BKE_layer.h:426
#define BKE_view_layer_array_from_bases_in_edit_mode_unique_data(view_layer, v3d, r_len)
Definition: BKE_layer.h:430
void BKE_report(ReportList *reports, ReportType type, const char *message)
Definition: report.c:104
#define BLI_assert(a)
Definition: BLI_assert.h:58
BLI_INLINE void * BLI_ghashIterator_getKey(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.h:146
BLI_INLINE void * BLI_ghashIterator_getValue(GHashIterator *ghi) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.h:150
GHash * BLI_ghash_str_new(const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
#define GHASH_ITER(gh_iter_, ghash_)
Definition: BLI_ghash.h:169
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition: BLI_ghash.c:756
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition: BLI_ghash.c:1008
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
void BLI_freelinkN(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:281
void * BLI_findstring(const struct ListBase *listbase, const char *id, const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:133
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
Definition: math_matrix.c:262
void invert_m4_m4_safe_ortho(float Ainv[4][4], const float A[4][4])
Definition: math_matrix.c:3287
void unit_m4(float m[4][4])
Definition: rct.c:1140
void copy_m4_m3(float m1[4][4], const float m2[3][3])
Definition: math_matrix.c:120
bool invert_m4_m4(float R[4][4], const float A[4][4])
Definition: math_matrix.c:1278
void mul_m4_v3(const float M[4][4], float r[3])
Definition: math_matrix.c:732
void mul_m4_m3m4(float R[4][4], const float A[3][3], const float B[4][4])
Definition: math_matrix.c:505
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL() ATTR_MALLOC
Definition: string.c:70
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
unsigned int uint
Definition: BLI_sys_types.h:83
#define UNUSED(x)
#define STREQ(a, b)
#define CTX_IFACE_(context, msgid)
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
void DEG_id_tag_update_ex(struct Main *bmain, struct ID *id, int flag)
void DEG_id_tag_update(struct ID *id, int flag)
void DEG_relations_tag_update(struct Main *bmain)
@ ID_RECALC_COPY_ON_WRITE
Definition: DNA_ID.h:654
@ ID_RECALC_SELECT
Definition: DNA_ID.h:638
@ ID_RECALC_GEOMETRY
Definition: DNA_ID.h:611
@ BONE_ROOTSEL
@ BONE_SELECTED
@ BONE_TIPSEL
@ BONE_CONNECTED
@ ARM_MIRROR_EDIT
@ CONSTRAINT_TYPE_ACTION
@ OB_MODE_POSE
Object is a sort of wrapper for general info.
@ PARBONE
@ OB_ARMATURE
@ USER_DUP_ARM
@ USER_DUP_ACT
@ OPERATOR_CANCELLED
@ OPERATOR_INTERFACE
@ OPERATOR_FINISHED
#define EBONE_VISIBLE(arm, ebone)
Definition: ED_armature.h:56
#define EBONE_EDITABLE(ebone)
Definition: ED_armature.h:64
struct Base * ED_object_add_duplicate(struct Main *bmain, struct Scene *scene, struct ViewLayer *view_layer, struct Base *base, const eDupli_ID_Flags dupflag)
Definition: object_add.c:3313
void ED_object_base_free_and_unlink(struct Main *bmain, struct Scene *scene, struct Object *ob)
Definition: object_add.c:1958
void ED_outliner_select_sync_from_object_tag(struct bContext *C)
Definition: outliner_sync.c:56
bool ED_operator_editarmature(struct bContext *C)
Definition: screen_ops.c:437
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:39
struct uiLayout * UI_popup_menu_layout(uiPopupMenu *pup)
void uiItemEnumO(uiLayout *layout, const char *opname, const char *name, int icon, const char *propname, int value)
void UI_popup_menu_end(struct bContext *C, struct uiPopupMenu *pup)
uiPopupMenu * UI_popup_menu_begin(struct bContext *C, const char *title, int icon) ATTR_NONNULL()
#define ND_OB_ACTIVE
Definition: WM_types.h:340
@ OPTYPE_UNDO
Definition: WM_types.h:155
@ OPTYPE_REGISTER
Definition: WM_types.h:153
#define NC_SCENE
Definition: WM_types.h:279
#define ND_LAYER_CONTENT
Definition: WM_types.h:354
#define ND_POSE
Definition: WM_types.h:359
#define ND_BONE_SELECT
Definition: WM_types.h:361
#define NC_OBJECT
Definition: WM_types.h:280
void bone_free(struct bArmature *arm, struct EditBone *bone)
void ED_armature_ebone_unique_name(ListBase *ebones, char *name, EditBone *bone)
static void separate_armature_bones(Main *bmain, Object *ob, const bool is_select)
static void joined_armature_fix_links(Main *bmain, Object *tarArm, Object *srcArm, bPoseChannel *pchan, EditBone *curbone)
static const EnumPropertyItem prop_editarm_clear_parent_types[]
void ARMATURE_OT_parent_clear(wmOperatorType *ot)
static void joined_armature_fix_animdata_cb(ID *id, FCurve *fcu, void *user_data)
static int armature_parent_set_exec(bContext *C, wmOperator *op)
static void joined_armature_fix_links_constraints(Main *bmain, Object *ob, Object *tarArm, Object *srcArm, bPoseChannel *pchan, EditBone *curbone, ListBase *lb)
static int separate_armature_exec(bContext *C, wmOperator *op)
static int armature_parent_clear_exec(bContext *C, wmOperator *op)
int ED_armature_join_objects_exec(bContext *C, wmOperator *op)
void ARMATURE_OT_parent_set(wmOperatorType *ot)
static void separated_armature_fix_links(Main *bmain, Object *origArm, Object *newArm)
static void bone_connect_to_new_parent(ListBase *edbo, EditBone *selbone, EditBone *actbone, short mode)
static int armature_parent_set_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *UNUSED(event))
#define ARM_PAR_OFFSET
void ARMATURE_OT_separate(wmOperatorType *ot)
static void editbone_clear_parent(EditBone *ebone, int mode)
#define ARM_PAR_CONNECT
static const EnumPropertyItem prop_editarm_make_parent_types[]
struct tJoinArmature_AdtFixData tJoinArmature_AdtFixData
static void bone_connect_to_existing_parent(EditBone *bone)
bool ED_armature_edit_deselect_all(Object *obedit)
void ED_armature_edit_refresh_layer_used(bArmature *arm)
void ED_armature_edit_sync_selection(ListBase *edbo)
void ED_armature_ebone_to_mat3(EditBone *ebone, float r_mat[3][3])
void ED_armature_edit_free(struct bArmature *arm)
void ED_armature_from_edit(Main *bmain, bArmature *arm)
EditBone * ED_armature_ebone_find_name(const ListBase *edbo, const char *name)
void ED_armature_to_edit(bArmature *arm)
EditBone * ED_armature_ebone_get_mirrored(const ListBase *edbo, EditBone *ebo)
unsigned int U
Definition: btGjkEpa3.h:78
Scene scene
void * user_data
#define atan2f(x, y)
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
int RNA_enum_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6402
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, int default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3771
struct Object * object
ListBase variables
struct DriverVar * next
char name[64]
Definition: BKE_armature.h:57
struct EditBone * next
Definition: BKE_armature.h:49
float tail[3]
Definition: BKE_armature.h:66
struct EditBone * parent
Definition: BKE_armature.h:55
float rad_tail
Definition: BKE_armature.h:78
float rad_head
Definition: BKE_armature.h:78
float head[3]
Definition: BKE_armature.h:65
char * rna_path
ChannelDriver * driver
Definition: DNA_ID.h:273
void * next
Definition: DNA_ID.h:274
void * first
Definition: DNA_listBase.h:47
Definition: BKE_main.h:116
ListBase objects
Definition: BKE_main.h:148
short partype
ListBase constraints
struct bPose * pose
float obmat[4][4]
struct AnimData * adt
struct Object * parent
void * data
char parsubstr[64]
struct AnimData * adt
struct EditBone * act_edbone
ListBase * edbo
struct bConstraintTarget * next
int(* get_constraint_targets)(struct bConstraint *con, struct ListBase *list)
void(* flush_constraint_targets)(struct bConstraint *con, struct ListBase *list, bool no_copy)
struct bConstraint * next
ListBase constraints
struct bPoseChannel * next
ListBase chanbase
int(* invoke)(struct bContext *, struct wmOperator *, const struct wmEvent *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:752
const char * name
Definition: WM_types.h:721
const char * idname
Definition: WM_types.h:723
bool(* poll)(struct bContext *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:776
struct StructRNA * srna
Definition: WM_types.h:802
const char * description
Definition: WM_types.h:726
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:736
PropertyRNA * prop
Definition: WM_types.h:814
struct ReportList * reports
struct PointerRNA * ptr
void WM_cursor_wait(bool val)
Definition: wm_cursors.c:226
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
wmOperatorType * ot
Definition: wm_files.c:3156
int WM_operator_confirm(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
int WM_menu_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
Definition: wm_operators.c:982