Blender  V2.93
armature.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  */
19 
24 #include <ctype.h>
25 #include <float.h>
26 #include <math.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #include "MEM_guardedalloc.h"
32 
33 #include "BLI_alloca.h"
34 #include "BLI_ghash.h"
35 #include "BLI_listbase.h"
36 #include "BLI_math.h"
37 #include "BLI_string.h"
38 #include "BLI_utildefines.h"
39 #include "BLT_translation.h"
40 
41 #include "DNA_defaults.h"
42 
43 #include "DNA_armature_types.h"
44 #include "DNA_constraint_types.h"
45 #include "DNA_listBase.h"
46 #include "DNA_object_types.h"
47 #include "DNA_scene_types.h"
48 
49 #include "BKE_action.h"
50 #include "BKE_anim_data.h"
51 #include "BKE_anim_visualization.h"
52 #include "BKE_armature.h"
53 #include "BKE_constraint.h"
54 #include "BKE_curve.h"
55 #include "BKE_idprop.h"
56 #include "BKE_idtype.h"
57 #include "BKE_lib_id.h"
58 #include "BKE_lib_query.h"
59 #include "BKE_main.h"
60 #include "BKE_object.h"
61 #include "BKE_scene.h"
62 
63 #include "DEG_depsgraph_build.h"
64 #include "DEG_depsgraph_query.h"
65 
66 #include "BIK_api.h"
67 
68 #include "BLO_read_write.h"
69 
70 #include "CLG_log.h"
71 
72 static CLG_LogRef LOG = {"bke.armature"};
73 
74 /* -------------------------------------------------------------------- */
78 static void copy_bonechildren(Bone *bone_dst,
79  const Bone *bone_src,
80  const Bone *bone_src_act,
81  Bone **r_bone_dst_act,
82  const int flag);
83 
84 static void copy_bonechildren_custom_handles(Bone *bone_dst, bArmature *arm_dst);
85 
88 /* -------------------------------------------------------------------- */
92 static void armature_init_data(ID *id)
93 {
94  bArmature *armature = (bArmature *)id;
96 
98 }
99 
110 static void armature_copy_data(Main *UNUSED(bmain), ID *id_dst, const ID *id_src, const int flag)
111 {
112  bArmature *armature_dst = (bArmature *)id_dst;
113  const bArmature *armature_src = (const bArmature *)id_src;
114 
115  Bone *bone_src, *bone_dst;
116  Bone *bone_dst_act = NULL;
117 
118  /* We never handle usercount here for own data. */
119  const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT;
120 
121  armature_dst->bonehash = NULL;
122 
123  BLI_duplicatelist(&armature_dst->bonebase, &armature_src->bonebase);
124 
125  /* Duplicate the childrens' lists */
126  bone_dst = armature_dst->bonebase.first;
127  for (bone_src = armature_src->bonebase.first; bone_src; bone_src = bone_src->next) {
128  bone_dst->parent = NULL;
129  copy_bonechildren(bone_dst, bone_src, armature_src->act_bone, &bone_dst_act, flag_subdata);
130  bone_dst = bone_dst->next;
131  }
132 
133  armature_dst->act_bone = bone_dst_act;
134 
135  BKE_armature_bone_hash_make(armature_dst);
136 
137  /* Fix custom handle references. */
138  for (bone_dst = armature_dst->bonebase.first; bone_dst; bone_dst = bone_dst->next) {
139  copy_bonechildren_custom_handles(bone_dst, armature_dst);
140  }
141 
142  armature_dst->edbo = NULL;
143  armature_dst->act_edbone = NULL;
144 }
145 
147 static void armature_free_data(struct ID *id)
148 {
149  bArmature *armature = (bArmature *)id;
150 
151  BKE_armature_bone_hash_free(armature);
152  BKE_armature_bonelist_free(&armature->bonebase, false);
153 
154  /* free editmode data */
155  if (armature->edbo) {
156  BKE_armature_editbonelist_free(armature->edbo, false);
157  MEM_freeN(armature->edbo);
158  armature->edbo = NULL;
159  }
160 }
161 
163 {
166 
167  LISTBASE_FOREACH (Bone *, curbone, &bone->childbase) {
168  armature_foreach_id_bone(curbone, data);
169  }
170 }
171 
173 {
176 }
177 
179 {
180  bArmature *arm = (bArmature *)id;
181  LISTBASE_FOREACH (Bone *, bone, &arm->bonebase) {
183  }
184 
185  if (arm->edbo != NULL) {
186  LISTBASE_FOREACH (EditBone *, edit_bone, arm->edbo) {
188  }
189  }
190 }
191 
192 static void write_bone(BlendWriter *writer, Bone *bone)
193 {
194  /* PATCH for upward compatibility after 2.37+ armature recode */
195  bone->size[0] = bone->size[1] = bone->size[2] = 1.0f;
196 
197  /* Write this bone */
198  BLO_write_struct(writer, Bone, bone);
199 
200  /* Write ID Properties -- and copy this comment EXACTLY for easy finding
201  * of library blocks that implement this.*/
202  if (bone->prop) {
203  IDP_BlendWrite(writer, bone->prop);
204  }
205 
206  /* Write Children */
207  LISTBASE_FOREACH (Bone *, cbone, &bone->childbase) {
208  write_bone(writer, cbone);
209  }
210 }
211 
212 static void armature_blend_write(BlendWriter *writer, ID *id, const void *id_address)
213 {
214  bArmature *arm = (bArmature *)id;
215  if (arm->id.us > 0 || BLO_write_is_undo(writer)) {
216  /* Clean up, important in undo case to reduce false detection of changed datablocks. */
217  arm->bonehash = NULL;
218  arm->edbo = NULL;
219  /* Must always be cleared (armatures don't have their own edit-data). */
220  arm->needs_flush_to_id = 0;
221  arm->act_edbone = NULL;
222 
223  BLO_write_id_struct(writer, bArmature, id_address, &arm->id);
224  BKE_id_blend_write(writer, &arm->id);
225 
226  if (arm->adt) {
227  BKE_animdata_blend_write(writer, arm->adt);
228  }
229 
230  /* Direct data */
231  LISTBASE_FOREACH (Bone *, bone, &arm->bonebase) {
232  write_bone(writer, bone);
233  }
234  }
235 }
236 
237 static void direct_link_bones(BlendDataReader *reader, Bone *bone)
238 {
239  BLO_read_data_address(reader, &bone->parent);
240  BLO_read_data_address(reader, &bone->prop);
241  IDP_BlendDataRead(reader, &bone->prop);
242 
243  BLO_read_data_address(reader, &bone->bbone_next);
244  BLO_read_data_address(reader, &bone->bbone_prev);
245 
247 
248  BLO_read_list(reader, &bone->childbase);
249 
250  LISTBASE_FOREACH (Bone *, child, &bone->childbase) {
251  direct_link_bones(reader, child);
252  }
253 }
254 
255 static void armature_blend_read_data(BlendDataReader *reader, ID *id)
256 {
257  bArmature *arm = (bArmature *)id;
258  BLO_read_list(reader, &arm->bonebase);
259  arm->bonehash = NULL;
260  arm->edbo = NULL;
261  /* Must always be cleared (armatures don't have their own edit-data). */
262  arm->needs_flush_to_id = 0;
263 
264  BLO_read_data_address(reader, &arm->adt);
265  BKE_animdata_blend_read_data(reader, arm->adt);
266 
267  LISTBASE_FOREACH (Bone *, bone, &arm->bonebase) {
268  direct_link_bones(reader, bone);
269  }
270 
271  BLO_read_data_address(reader, &arm->act_bone);
272  arm->act_edbone = NULL;
273 
275 }
276 
277 static void lib_link_bones(BlendLibReader *reader, Bone *bone)
278 {
279  IDP_BlendReadLib(reader, bone->prop);
280 
281  LISTBASE_FOREACH (Bone *, curbone, &bone->childbase) {
282  lib_link_bones(reader, curbone);
283  }
284 }
285 
286 static void armature_blend_read_lib(BlendLibReader *reader, ID *id)
287 {
288  bArmature *arm = (bArmature *)id;
289  LISTBASE_FOREACH (Bone *, curbone, &arm->bonebase) {
290  lib_link_bones(reader, curbone);
291  }
292 }
293 
294 static void expand_bones(BlendExpander *expander, Bone *bone)
295 {
296  IDP_BlendReadExpand(expander, bone->prop);
297 
298  LISTBASE_FOREACH (Bone *, curBone, &bone->childbase) {
299  expand_bones(expander, curBone);
300  }
301 }
302 
303 static void armature_blend_read_expand(BlendExpander *expander, ID *id)
304 {
305  bArmature *arm = (bArmature *)id;
306  LISTBASE_FOREACH (Bone *, curBone, &arm->bonebase) {
307  expand_bones(expander, curBone);
308  }
309 }
310 
312  .id_code = ID_AR,
313  .id_filter = FILTER_ID_AR,
314  .main_listbase_index = INDEX_ID_AR,
315  .struct_size = sizeof(bArmature),
316  .name = "Armature",
317  .name_plural = "armatures",
318  .translation_context = BLT_I18NCONTEXT_ID_ARMATURE,
319  .flags = 0,
320 
322  .copy_data = armature_copy_data,
323  .free_data = armature_free_data,
324  .make_local = NULL,
325  .foreach_id = armature_foreach_id,
326  .foreach_cache = NULL,
327  .owner_get = NULL,
328 
329  .blend_write = armature_blend_write,
330  .blend_read_data = armature_blend_read_data,
331  .blend_read_lib = armature_blend_read_lib,
332  .blend_read_expand = armature_blend_read_expand,
333 
334  .blend_read_undo_preserve = NULL,
335 
336  .lib_override_apply_post = NULL,
337 };
338 
341 /* -------------------------------------------------------------------- */
345 bArmature *BKE_armature_add(Main *bmain, const char *name)
346 {
347  bArmature *arm;
348 
349  arm = BKE_id_new(bmain, ID_AR, name);
350  return arm;
351 }
352 
354 {
355  if (ob->type == OB_ARMATURE) {
356  return (bArmature *)ob->data;
357  }
358  return NULL;
359 }
360 
362 {
363  int i = 0;
364  LISTBASE_FOREACH (Bone *, bone, lb) {
365  i += 1 + BKE_armature_bonelist_count(&bone->childbase);
366  }
367 
368  return i;
369 }
370 
371 void BKE_armature_bonelist_free(ListBase *lb, const bool do_id_user)
372 {
373  Bone *bone;
374 
375  for (bone = lb->first; bone; bone = bone->next) {
376  if (bone->prop) {
377  IDP_FreeProperty_ex(bone->prop, do_id_user);
378  }
379  BKE_armature_bonelist_free(&bone->childbase, do_id_user);
380  }
381 
382  BLI_freelistN(lb);
383 }
384 
385 void BKE_armature_editbonelist_free(ListBase *lb, const bool do_id_user)
386 {
387  LISTBASE_FOREACH_MUTABLE (EditBone *, edit_bone, lb) {
388  if (edit_bone->prop) {
389  IDP_FreeProperty_ex(edit_bone->prop, do_id_user);
390  }
391  BLI_remlink_safe(lb, edit_bone);
392  MEM_freeN(edit_bone);
393  }
394 }
395 
396 static void copy_bonechildren(Bone *bone_dst,
397  const Bone *bone_src,
398  const Bone *bone_src_act,
399  Bone **r_bone_dst_act,
400  const int flag)
401 {
402  Bone *bone_src_child, *bone_dst_child;
403 
404  if (bone_src == bone_src_act) {
405  *r_bone_dst_act = bone_dst;
406  }
407 
408  if (bone_src->prop) {
409  bone_dst->prop = IDP_CopyProperty_ex(bone_src->prop, flag);
410  }
411 
412  /* Copy this bone's list */
413  BLI_duplicatelist(&bone_dst->childbase, &bone_src->childbase);
414 
415  /* For each child in the list, update its children */
416  for (bone_src_child = bone_src->childbase.first, bone_dst_child = bone_dst->childbase.first;
417  bone_src_child;
418  bone_src_child = bone_src_child->next, bone_dst_child = bone_dst_child->next) {
419  bone_dst_child->parent = bone_dst;
420  copy_bonechildren(bone_dst_child, bone_src_child, bone_src_act, r_bone_dst_act, flag);
421  }
422 }
423 
424 static void copy_bonechildren_custom_handles(Bone *bone_dst, bArmature *arm_dst)
425 {
426  Bone *bone_dst_child;
427 
428  if (bone_dst->bbone_prev) {
429  bone_dst->bbone_prev = BKE_armature_find_bone_name(arm_dst, bone_dst->bbone_prev->name);
430  }
431  if (bone_dst->bbone_next) {
432  bone_dst->bbone_next = BKE_armature_find_bone_name(arm_dst, bone_dst->bbone_next->name);
433  }
434 
435  for (bone_dst_child = bone_dst->childbase.first; bone_dst_child;
436  bone_dst_child = bone_dst_child->next) {
437  copy_bonechildren_custom_handles(bone_dst_child, arm_dst);
438  }
439 }
440 
443 /* -------------------------------------------------------------------- */
447 static void copy_bone_transform(Bone *bone_dst, const Bone *bone_src)
448 {
449  bone_dst->roll = bone_src->roll;
450 
451  copy_v3_v3(bone_dst->head, bone_src->head);
452  copy_v3_v3(bone_dst->tail, bone_src->tail);
453 
454  copy_m3_m3(bone_dst->bone_mat, bone_src->bone_mat);
455 
456  copy_v3_v3(bone_dst->arm_head, bone_src->arm_head);
457  copy_v3_v3(bone_dst->arm_tail, bone_src->arm_tail);
458 
459  copy_m4_m4(bone_dst->arm_mat, bone_src->arm_mat);
460 
461  bone_dst->arm_roll = bone_src->arm_roll;
462 }
463 
464 void BKE_armature_copy_bone_transforms(bArmature *armature_dst, const bArmature *armature_src)
465 {
466  Bone *bone_dst = armature_dst->bonebase.first;
467  const Bone *bone_src = armature_src->bonebase.first;
468  while (bone_dst != NULL) {
469  BLI_assert(bone_src != NULL);
470  copy_bone_transform(bone_dst, bone_src);
471  bone_dst = bone_dst->next;
472  bone_src = bone_src->next;
473  }
474 }
475 
478 /* -------------------------------------------------------------------- */
485 static void armature_transform_recurse(ListBase *bonebase,
486  const float mat[4][4],
487  const bool do_props,
488  /* Cached from 'mat'. */
489  const float mat3[3][3],
490  const float scale,
491  /* Child bones. */
492  const Bone *bone_parent,
493  const float arm_mat_parent_inv[4][4])
494 {
495  LISTBASE_FOREACH (Bone *, bone, bonebase) {
496 
497  /* Store the initial bone roll in a matrix, this is needed even for child bones
498  * so any change in head/tail doesn't cause the roll to change.
499  *
500  * Logic here is different to edit-mode because
501  * this is calculated in relative to the parent. */
502  float roll_mat3_pre[3][3];
503  {
504  float delta[3];
505  sub_v3_v3v3(delta, bone->tail, bone->head);
506  vec_roll_to_mat3(delta, bone->roll, roll_mat3_pre);
507  if (bone->parent == NULL) {
508  mul_m3_m3m3(roll_mat3_pre, mat3, roll_mat3_pre);
509  }
510  }
511  /* Optional, use this for predictable results since the roll is re-calculated below anyway. */
512  bone->roll = 0.0f;
513 
514  mul_m4_v3(mat, bone->arm_head);
515  mul_m4_v3(mat, bone->arm_tail);
516 
517  /* Get the new head and tail */
518  if (bone_parent) {
519  sub_v3_v3v3(bone->head, bone->arm_head, bone_parent->arm_tail);
520  sub_v3_v3v3(bone->tail, bone->arm_tail, bone_parent->arm_tail);
521 
522  mul_mat3_m4_v3(arm_mat_parent_inv, bone->head);
523  mul_mat3_m4_v3(arm_mat_parent_inv, bone->tail);
524  }
525  else {
526  copy_v3_v3(bone->head, bone->arm_head);
527  copy_v3_v3(bone->tail, bone->arm_tail);
528  }
529 
530  /* Now the head/tail have been updated, set the roll back, matching 'roll_mat3_pre'. */
531  {
532  float roll_mat3_post[3][3], delta_mat3[3][3];
533  float delta[3];
534  sub_v3_v3v3(delta, bone->tail, bone->head);
535  vec_roll_to_mat3(delta, 0.0f, roll_mat3_post);
536  invert_m3(roll_mat3_post);
537  mul_m3_m3m3(delta_mat3, roll_mat3_post, roll_mat3_pre);
538  bone->roll = atan2f(delta_mat3[2][0], delta_mat3[2][2]);
539  }
540 
541  BKE_armature_where_is_bone(bone, bone_parent, false);
542 
543  {
544  float arm_mat3[3][3];
545  copy_m3_m4(arm_mat3, bone->arm_mat);
546  mat3_to_vec_roll(arm_mat3, NULL, &bone->arm_roll);
547  }
548 
549  if (do_props) {
550  bone->rad_head *= scale;
551  bone->rad_tail *= scale;
552  bone->dist *= scale;
553 
554  /* we could be smarter and scale by the matrix along the x & z axis */
555  bone->xwidth *= scale;
556  bone->zwidth *= scale;
557  }
558 
559  if (!BLI_listbase_is_empty(&bone->childbase)) {
560  float arm_mat_inv[4][4];
561  invert_m4_m4(arm_mat_inv, bone->arm_mat);
562  armature_transform_recurse(&bone->childbase, mat, do_props, mat3, scale, bone, arm_mat_inv);
563  }
564  }
565 }
566 
567 void BKE_armature_transform(bArmature *arm, const float mat[4][4], const bool do_props)
568 {
569  /* Store the scale of the matrix here to use on envelopes. */
570  float scale = mat4_to_scale(mat);
571  float mat3[3][3];
572 
573  copy_m3_m4(mat3, mat);
574  normalize_m3(mat3);
575 
576  armature_transform_recurse(&arm->bonebase, mat, do_props, mat3, scale, NULL, NULL);
577 }
578 
581 /* -------------------------------------------------------------------- */
587 static Bone *get_named_bone_bonechildren(ListBase *lb, const char *name)
588 {
589  Bone *curBone, *rbone;
590 
591  for (curBone = lb->first; curBone; curBone = curBone->next) {
592  if (STREQ(curBone->name, name)) {
593  return curBone;
594  }
595 
596  rbone = get_named_bone_bonechildren(&curBone->childbase, name);
597  if (rbone) {
598  return rbone;
599  }
600  }
601 
602  return NULL;
603 }
604 
610 {
611  if (!arm) {
612  return NULL;
613  }
614 
615  if (arm->bonehash) {
616  return BLI_ghash_lookup(arm->bonehash, name);
617  }
618 
619  return get_named_bone_bonechildren(&arm->bonebase, name);
620 }
621 
623 {
624  LISTBASE_FOREACH (Bone *, bone, lb) {
625  BLI_ghash_insert(bone_hash, bone->name, bone);
626  armature_bone_from_name_insert_recursive(bone_hash, &bone->childbase);
627  }
628 }
629 
637 {
638  const int bones_count = BKE_armature_bonelist_count(&arm->bonebase);
639  GHash *bone_hash = BLI_ghash_str_new_ex(__func__, bones_count);
641  return bone_hash;
642 }
643 
645 {
646  if (!arm->bonehash) {
648  }
649 }
650 
652 {
653  if (arm->bonehash) {
655  arm->bonehash = NULL;
656  }
657 }
658 
661 /* -------------------------------------------------------------------- */
665 bool BKE_armature_bone_flag_test_recursive(const Bone *bone, int flag)
666 {
667  if (bone->flag & flag) {
668  return true;
669  }
670  if (bone->parent) {
671  return BKE_armature_bone_flag_test_recursive(bone->parent, flag);
672  }
673  return false;
674 }
675 
678 /* -------------------------------------------------------------------- */
683 {
684  LISTBASE_FOREACH (Bone *, bone, bones) {
685  arm->layer_used |= bone->layer;
686  armature_refresh_layer_used_recursive(arm, &bone->childbase);
687  }
688 }
689 
691 {
692  if (arm->edbo != NULL) {
693  /* Don't perform this update when the armature is in edit mode. In that case it should be
694  * handled by ED_armature_edit_refresh_layer_used(). */
695  return;
696  }
697 
698  arm->layer_used = 0;
700 
701  if (depsgraph == NULL || DEG_is_active(depsgraph)) {
702  bArmature *arm_orig = (bArmature *)DEG_get_original_id(&arm->id);
703  arm_orig->layer_used = arm->layer_used;
704  }
705 }
706 
709 /* -------------------------------------------------------------------- */
713 /* Finds the best possible extension to the name on a particular axis. (For renaming, check for
714  * unique names afterwards) strip_number: removes number extensions (TODO: not used)
715  * axis: the axis to name on
716  * head/tail: the head/tail co-ordinate of the bone on the specified axis */
718  char name[MAXBONENAME], int UNUSED(strip_number), short axis, float head, float tail)
719 {
720  unsigned int len;
721  char basename[MAXBONENAME] = "";
722  char extension[5] = "";
723 
724  len = strlen(name);
725  if (len == 0) {
726  return false;
727  }
728  BLI_strncpy(basename, name, sizeof(basename));
729 
730  /* Figure out extension to append:
731  * - The extension to append is based upon the axis that we are working on.
732  * - If head happens to be on 0, then we must consider the tail position as well to decide
733  * which side the bone is on
734  * -> If tail is 0, then its bone is considered to be on axis, so no extension should be added
735  * -> Otherwise, extension is added from perspective of object based on which side tail goes to
736  * - If head is non-zero, extension is added from perspective of object based on side head is on
737  */
738  if (axis == 2) {
739  /* z-axis - vertical (top/bottom) */
740  if (IS_EQF(head, 0.0f)) {
741  if (tail < 0) {
742  strcpy(extension, "Bot");
743  }
744  else if (tail > 0) {
745  strcpy(extension, "Top");
746  }
747  }
748  else {
749  if (head < 0) {
750  strcpy(extension, "Bot");
751  }
752  else {
753  strcpy(extension, "Top");
754  }
755  }
756  }
757  else if (axis == 1) {
758  /* y-axis - depth (front/back) */
759  if (IS_EQF(head, 0.0f)) {
760  if (tail < 0) {
761  strcpy(extension, "Fr");
762  }
763  else if (tail > 0) {
764  strcpy(extension, "Bk");
765  }
766  }
767  else {
768  if (head < 0) {
769  strcpy(extension, "Fr");
770  }
771  else {
772  strcpy(extension, "Bk");
773  }
774  }
775  }
776  else {
777  /* x-axis - horizontal (left/right) */
778  if (IS_EQF(head, 0.0f)) {
779  if (tail < 0) {
780  strcpy(extension, "R");
781  }
782  else if (tail > 0) {
783  strcpy(extension, "L");
784  }
785  }
786  else {
787  if (head < 0) {
788  strcpy(extension, "R");
789  /* XXX Shouldn't this be simple else, as for z and y axes? */
790  }
791  else if (head > 0) {
792  strcpy(extension, "L");
793  }
794  }
795  }
796 
797  /* Simple name truncation
798  * - truncate if there is an extension and it wouldn't be able to fit
799  * - otherwise, just append to end
800  */
801  if (extension[0]) {
802  bool changed = true;
803 
804  while (changed) { /* remove extensions */
805  changed = false;
806  if (len > 2 && basename[len - 2] == '.') {
807  if (ELEM(basename[len - 1], 'L', 'R')) { /* L R */
808  basename[len - 2] = '\0';
809  len -= 2;
810  changed = true;
811  }
812  }
813  else if (len > 3 && basename[len - 3] == '.') {
814  if ((basename[len - 2] == 'F' && basename[len - 1] == 'r') || /* Fr */
815  (basename[len - 2] == 'B' && basename[len - 1] == 'k')) /* Bk */
816  {
817  basename[len - 3] = '\0';
818  len -= 3;
819  changed = true;
820  }
821  }
822  else if (len > 4 && basename[len - 4] == '.') {
823  if ((basename[len - 3] == 'T' && basename[len - 2] == 'o' &&
824  basename[len - 1] == 'p') || /* Top */
825  (basename[len - 3] == 'B' && basename[len - 2] == 'o' &&
826  basename[len - 1] == 't')) /* Bot */
827  {
828  basename[len - 4] = '\0';
829  len -= 4;
830  changed = true;
831  }
832  }
833  }
834 
835  if ((MAXBONENAME - len) < strlen(extension) + 1) { /* add 1 for the '.' */
836  strncpy(name, basename, len - strlen(extension));
837  }
838 
839  BLI_snprintf(name, MAXBONENAME, "%s.%s", basename, extension);
840 
841  return true;
842  }
843  return false;
844 }
845 
848 /* -------------------------------------------------------------------- */
852 /* Compute a set of bezier parameter values that produce approximately equally spaced points. */
853 static void equalize_cubic_bezier(const float control[4][3],
854  int temp_segments,
855  int final_segments,
856  float *r_t_points)
857 {
858  float(*coords)[3] = BLI_array_alloca(coords, temp_segments + 1);
859  float *pdist = BLI_array_alloca(pdist, temp_segments + 1);
860 
861  /* Compute the first pass of bezier point coordinates. */
862  for (int i = 0; i < 3; i++) {
863  BKE_curve_forward_diff_bezier(control[0][i],
864  control[1][i],
865  control[2][i],
866  control[3][i],
867  &coords[0][i],
868  temp_segments,
869  sizeof(*coords));
870  }
871 
872  /* Calculate the length of the polyline at each point. */
873  pdist[0] = 0.0f;
874 
875  for (int i = 0; i < temp_segments; i++) {
876  pdist[i + 1] = pdist[i] + len_v3v3(coords[i], coords[i + 1]);
877  }
878 
879  /* Go over distances and calculate new parameter values. */
880  float dist_step = pdist[temp_segments] / final_segments;
881 
882  r_t_points[0] = 0.0f;
883 
884  for (int i = 1, nr = 1; i <= final_segments; i++) {
885  float dist = i * dist_step;
886 
887  /* We're looking for location (distance) 'dist' in the array. */
888  while ((nr < temp_segments) && (dist >= pdist[nr])) {
889  nr++;
890  }
891 
892  float fac = (pdist[nr] - dist) / (pdist[nr] - pdist[nr - 1]);
893 
894  r_t_points[i] = (nr - fac) / temp_segments;
895  }
896 
897  r_t_points[final_segments] = 1.0f;
898 }
899 
900 /* Evaluate bezier position and tangent at a specific parameter value
901  * using the De Casteljau algorithm. */
902 static void evaluate_cubic_bezier(const float control[4][3],
903  float t,
904  float r_pos[3],
905  float r_tangent[3])
906 {
907  float layer1[3][3];
908  interp_v3_v3v3(layer1[0], control[0], control[1], t);
909  interp_v3_v3v3(layer1[1], control[1], control[2], t);
910  interp_v3_v3v3(layer1[2], control[2], control[3], t);
911 
912  float layer2[2][3];
913  interp_v3_v3v3(layer2[0], layer1[0], layer1[1], t);
914  interp_v3_v3v3(layer2[1], layer1[1], layer1[2], t);
915 
916  sub_v3_v3v3(r_tangent, layer2[1], layer2[0]);
917  madd_v3_v3v3fl(r_pos, layer2[0], r_tangent, t);
918 }
919 
920 /* Get "next" and "prev" bones - these are used for handle calculations. */
922 {
923  if (pchan->bone->bbone_prev_type == BBONE_HANDLE_AUTO) {
924  /* Use connected parent. */
925  if (pchan->bone->flag & BONE_CONNECTED) {
926  *r_prev = pchan->parent;
927  }
928  else {
929  *r_prev = NULL;
930  }
931  }
932  else {
933  /* Use the provided bone as prev - leave blank to eliminate this effect altogether. */
934  *r_prev = pchan->bbone_prev;
935  }
936 
937  if (pchan->bone->bbone_next_type == BBONE_HANDLE_AUTO) {
938  /* Use connected child. */
939  *r_next = pchan->child;
940  }
941  else {
942  /* Use the provided bone as next - leave blank to eliminate this effect altogether. */
943  *r_next = pchan->bbone_next;
944  }
945 }
946 
947 /* Compute B-Bone spline parameters for the given channel. */
949  const bool rest,
950  struct BBoneSplineParameters *param)
951 {
952  bPoseChannel *next, *prev;
953  Bone *bone = pchan->bone;
954  float imat[4][4], posemat[4][4];
955  float delta[3];
956 
957  memset(param, 0, sizeof(*param));
958 
959  param->segments = bone->segments;
960  param->length = bone->length;
961 
962  if (!rest) {
963  float scale[3];
964 
965  /* Check if we need to take non-uniform bone scaling into account. */
966  mat4_to_size(scale, pchan->pose_mat);
967 
968  if (fabsf(scale[0] - scale[1]) > 1e-6f || fabsf(scale[1] - scale[2]) > 1e-6f) {
969  param->do_scale = true;
970  copy_v3_v3(param->scale, scale);
971  }
972  }
973 
974  BKE_pchan_bbone_handles_get(pchan, &prev, &next);
975 
976  /* Find the handle points, since this is inside bone space, the
977  * first point = (0, 0, 0)
978  * last point = (0, length, 0) */
979  if (rest) {
980  invert_m4_m4(imat, pchan->bone->arm_mat);
981  }
982  else if (param->do_scale) {
983  copy_m4_m4(posemat, pchan->pose_mat);
984  normalize_m4(posemat);
985  invert_m4_m4(imat, posemat);
986  }
987  else {
988  invert_m4_m4(imat, pchan->pose_mat);
989  }
990 
991  if (prev) {
992  float h1[3];
993  bool done = false;
994 
995  param->use_prev = true;
996 
997  /* Transform previous point inside this bone space. */
998  if (bone->bbone_prev_type == BBONE_HANDLE_RELATIVE) {
999  /* Use delta movement (from restpose), and apply this relative to the current bone's head. */
1000  if (rest) {
1001  /* In restpose, arm_head == pose_head */
1002  zero_v3(param->prev_h);
1003  done = true;
1004  }
1005  else {
1006  sub_v3_v3v3(delta, prev->pose_head, prev->bone->arm_head);
1007  sub_v3_v3v3(h1, pchan->pose_head, delta);
1008  }
1009  }
1010  else if (bone->bbone_prev_type == BBONE_HANDLE_TANGENT) {
1011  /* Use bone direction by offsetting so that its tail meets current bone's head */
1012  if (rest) {
1013  sub_v3_v3v3(delta, prev->bone->arm_tail, prev->bone->arm_head);
1014  sub_v3_v3v3(h1, bone->arm_head, delta);
1015  }
1016  else {
1017  sub_v3_v3v3(delta, prev->pose_tail, prev->pose_head);
1018  sub_v3_v3v3(h1, pchan->pose_head, delta);
1019  }
1020  }
1021  else {
1022  /* Apply special handling for smoothly joining B-Bone chains */
1023  param->prev_bbone = (prev->bone->segments > 1);
1024 
1025  /* Use bone head as absolute position. */
1026  copy_v3_v3(h1, rest ? prev->bone->arm_head : prev->pose_head);
1027  }
1028 
1029  if (!done) {
1030  mul_v3_m4v3(param->prev_h, imat, h1);
1031  }
1032 
1033  if (!param->prev_bbone) {
1034  /* Find the previous roll to interpolate. */
1035  mul_m4_m4m4(param->prev_mat, imat, rest ? prev->bone->arm_mat : prev->pose_mat);
1036  }
1037  }
1038 
1039  if (next) {
1040  float h2[3];
1041  bool done = false;
1042 
1043  param->use_next = true;
1044 
1045  /* Transform next point inside this bone space. */
1046  if (bone->bbone_next_type == BBONE_HANDLE_RELATIVE) {
1047  /* Use delta movement (from restpose), and apply this relative to the current bone's tail. */
1048  if (rest) {
1049  /* In restpose, arm_head == pose_head */
1050  copy_v3_fl3(param->next_h, 0.0f, param->length, 0.0);
1051  done = true;
1052  }
1053  else {
1054  sub_v3_v3v3(delta, next->pose_head, next->bone->arm_head);
1055  add_v3_v3v3(h2, pchan->pose_tail, delta);
1056  }
1057  }
1058  else if (bone->bbone_next_type == BBONE_HANDLE_TANGENT) {
1059  /* Use bone direction by offsetting so that its head meets current bone's tail */
1060  if (rest) {
1061  sub_v3_v3v3(delta, next->bone->arm_tail, next->bone->arm_head);
1062  add_v3_v3v3(h2, bone->arm_tail, delta);
1063  }
1064  else {
1065  sub_v3_v3v3(delta, next->pose_tail, next->pose_head);
1066  add_v3_v3v3(h2, pchan->pose_tail, delta);
1067  }
1068  }
1069  else {
1070  /* Apply special handling for smoothly joining B-Bone chains */
1071  param->next_bbone = (next->bone->segments > 1);
1072 
1073  /* Use bone tail as absolute position. */
1074  copy_v3_v3(h2, rest ? next->bone->arm_tail : next->pose_tail);
1075  }
1076 
1077  if (!done) {
1078  mul_v3_m4v3(param->next_h, imat, h2);
1079  }
1080 
1081  /* Find the next roll to interpolate as well. */
1082  mul_m4_m4m4(param->next_mat, imat, rest ? next->bone->arm_mat : next->pose_mat);
1083  }
1084 
1085  /* Add effects from bbone properties over the top
1086  * - These properties allow users to hand-animate the
1087  * bone curve/shape, without having to resort to using
1088  * extra bones
1089  * - The "bone" level offsets are for defining the restpose
1090  * shape of the bone (e.g. for curved eyebrows for example).
1091  * -> In the viewport, it's needed to define what the rest pose
1092  * looks like
1093  * -> For "rest == 0", we also still need to have it present
1094  * so that we can "cancel out" this restpose when it comes
1095  * time to deform some geometry, it won't cause double transforms.
1096  * - The "pchan" level offsets are the ones that animators actually
1097  * end up animating
1098  */
1099  {
1100  param->ease1 = bone->ease1 + (!rest ? pchan->ease1 : 0.0f);
1101  param->ease2 = bone->ease2 + (!rest ? pchan->ease2 : 0.0f);
1102 
1103  param->roll1 = bone->roll1 + (!rest ? pchan->roll1 : 0.0f);
1104  param->roll2 = bone->roll2 + (!rest ? pchan->roll2 : 0.0f);
1105 
1106  if (bone->flag & BONE_ADD_PARENT_END_ROLL) {
1107  if (prev) {
1108  if (prev->bone) {
1109  param->roll1 += prev->bone->roll2;
1110  }
1111 
1112  if (!rest) {
1113  param->roll1 += prev->roll2;
1114  }
1115  }
1116  }
1117 
1118  param->scale_in_x = bone->scale_in_x * (!rest ? pchan->scale_in_x : 1.0f);
1119  param->scale_in_y = bone->scale_in_y * (!rest ? pchan->scale_in_y : 1.0f);
1120  param->scale_out_x = bone->scale_out_x * (!rest ? pchan->scale_out_x : 1.0f);
1121  param->scale_out_y = bone->scale_out_y * (!rest ? pchan->scale_out_y : 1.0f);
1122 
1123  /* Extra curve x / y */
1124  param->curve_in_x = bone->curve_in_x + (!rest ? pchan->curve_in_x : 0.0f);
1125  param->curve_in_y = bone->curve_in_y + (!rest ? pchan->curve_in_y : 0.0f);
1126 
1127  param->curve_out_x = bone->curve_out_x + (!rest ? pchan->curve_out_x : 0.0f);
1128  param->curve_out_y = bone->curve_out_y + (!rest ? pchan->curve_out_y : 0.0f);
1129  }
1130 }
1131 
1132 /* Fills the array with the desired amount of bone->segments elements.
1133  * This calculation is done within unit bone space. */
1135  const bool rest,
1136  const bool for_deform,
1137  Mat4 *result_array)
1138 {
1139  BBoneSplineParameters param;
1140 
1141  BKE_pchan_bbone_spline_params_get(pchan, rest, &param);
1142 
1143  pchan->bone->segments = BKE_pchan_bbone_spline_compute(&param, for_deform, result_array);
1144 }
1145 
1146 /* Computes the bezier handle vectors and rolls coming from custom handles. */
1148  float h1[3],
1149  float *r_roll1,
1150  float h2[3],
1151  float *r_roll2,
1152  bool ease,
1153  bool offsets)
1154 {
1155  float mat3[3][3];
1156  float length = param->length;
1157  float epsilon = 1e-5 * length;
1158 
1159  if (param->do_scale) {
1160  length *= param->scale[1];
1161  }
1162 
1163  *r_roll1 = *r_roll2 = 0.0f;
1164 
1165  if (param->use_prev) {
1166  copy_v3_v3(h1, param->prev_h);
1167 
1168  if (param->prev_bbone) {
1169  /* If previous bone is B-bone too, use average handle direction. */
1170  h1[1] -= length;
1171  }
1172 
1173  if (normalize_v3(h1) < epsilon) {
1174  copy_v3_fl3(h1, 0.0f, -1.0f, 0.0f);
1175  }
1176 
1177  negate_v3(h1);
1178 
1179  if (!param->prev_bbone) {
1180  /* Find the previous roll to interpolate. */
1181  copy_m3_m4(mat3, param->prev_mat);
1182  mat3_vec_to_roll(mat3, h1, r_roll1);
1183  }
1184  }
1185  else {
1186  h1[0] = 0.0f;
1187  h1[1] = 1.0;
1188  h1[2] = 0.0f;
1189  }
1190 
1191  if (param->use_next) {
1192  copy_v3_v3(h2, param->next_h);
1193 
1194  /* If next bone is B-bone too, use average handle direction. */
1195  if (param->next_bbone) {
1196  /* pass */
1197  }
1198  else {
1199  h2[1] -= length;
1200  }
1201 
1202  if (normalize_v3(h2) < epsilon) {
1203  copy_v3_fl3(h2, 0.0f, 1.0f, 0.0f);
1204  }
1205 
1206  /* Find the next roll to interpolate as well. */
1207  copy_m3_m4(mat3, param->next_mat);
1208  mat3_vec_to_roll(mat3, h2, r_roll2);
1209  }
1210  else {
1211  h2[0] = 0.0f;
1212  h2[1] = 1.0f;
1213  h2[2] = 0.0f;
1214  }
1215 
1216  if (ease) {
1217  const float circle_factor = length * (cubic_tangent_factor_circle_v3(h1, h2) / 0.75f);
1218 
1219  const float hlength1 = param->ease1 * circle_factor;
1220  const float hlength2 = param->ease2 * circle_factor;
1221 
1222  /* and only now negate h2 */
1223  mul_v3_fl(h1, hlength1);
1224  mul_v3_fl(h2, -hlength2);
1225  }
1226 
1227  /* Add effects from bbone properties over the top
1228  * - These properties allow users to hand-animate the
1229  * bone curve/shape, without having to resort to using
1230  * extra bones
1231  * - The "bone" level offsets are for defining the rest-pose
1232  * shape of the bone (e.g. for curved eyebrows for example).
1233  * -> In the viewport, it's needed to define what the rest pose
1234  * looks like
1235  * -> For "rest == 0", we also still need to have it present
1236  * so that we can "cancel out" this rest-pose when it comes
1237  * time to deform some geometry, it won't cause double transforms.
1238  * - The "pchan" level offsets are the ones that animators actually
1239  * end up animating
1240  */
1241  if (offsets) {
1242  /* Add extra rolls. */
1243  *r_roll1 += param->roll1;
1244  *r_roll2 += param->roll2;
1245 
1246  /* Extra curve x / y */
1247  /* NOTE:
1248  * Scale correction factors here are to compensate for some random floating-point glitches
1249  * when scaling up the bone or its parent by a factor of approximately 8.15/6, which results
1250  * in the bone length getting scaled up too (from 1 to 8), causing the curve to flatten out.
1251  */
1252  const float xscale_correction = (param->do_scale) ? param->scale[0] : 1.0f;
1253  const float yscale_correction = (param->do_scale) ? param->scale[2] : 1.0f;
1254 
1255  h1[0] += param->curve_in_x * xscale_correction;
1256  h1[2] += param->curve_in_y * yscale_correction;
1257 
1258  h2[0] += param->curve_out_x * xscale_correction;
1259  h2[2] += param->curve_out_y * yscale_correction;
1260  }
1261 }
1262 
1264  const float scalemats[2][4][4],
1265  const float pos[3],
1266  const float axis[3],
1267  float roll,
1268  float scalex,
1269  float scaley,
1270  float result[4][4])
1271 {
1272  float mat3[3][3];
1273 
1274  vec_roll_to_mat3(axis, roll, mat3);
1275 
1276  copy_m4_m3(result, mat3);
1277  copy_v3_v3(result[3], pos);
1278 
1279  if (param->do_scale) {
1280  /* Correct for scaling when this matrix is used in scaled space. */
1281  mul_m4_series(result, scalemats[0], result, scalemats[1]);
1282  }
1283 
1284  /* BBone scale... */
1285  mul_v3_fl(result[0], scalex);
1286  mul_v3_fl(result[2], scaley);
1287 }
1288 
1289 /* Fade from first to second derivative when the handle is very short. */
1290 static void ease_handle_axis(const float deriv1[3], const float deriv2[3], float r_axis[3])
1291 {
1292  const float gap = 0.1f;
1293 
1294  copy_v3_v3(r_axis, deriv1);
1295 
1296  float len1 = len_squared_v3(deriv1), len2 = len_squared_v3(deriv2);
1297  float ratio = len1 / len2;
1298 
1299  if (ratio < gap * gap) {
1300  madd_v3_v3fl(r_axis, deriv2, gap - sqrtf(ratio));
1301  }
1302 }
1303 
1304 /* Fills the array with the desired amount of bone->segments elements.
1305  * This calculation is done within unit bone space. */
1307  const bool for_deform,
1308  Mat4 *result_array)
1309 {
1310  float scalemats[2][4][4];
1311  float bezt_controls[4][3];
1312  float h1[3], roll1, h2[3], roll2, prev[3], cur[3], axis[3];
1313  float length = param->length;
1314 
1315  if (param->do_scale) {
1316  size_to_mat4(scalemats[1], param->scale);
1317  invert_m4_m4(scalemats[0], scalemats[1]);
1318 
1319  length *= param->scale[1];
1320  }
1321 
1322  BKE_pchan_bbone_handles_compute(param, h1, &roll1, h2, &roll2, true, true);
1323 
1324  /* Make curve. */
1326 
1327  copy_v3_fl3(bezt_controls[3], 0.0f, length, 0.0f);
1328  add_v3_v3v3(bezt_controls[2], bezt_controls[3], h2);
1329  copy_v3_v3(bezt_controls[1], h1);
1330  zero_v3(bezt_controls[0]);
1331 
1332  float bezt_points[MAX_BBONE_SUBDIV + 1];
1333 
1334  equalize_cubic_bezier(bezt_controls, MAX_BBONE_SUBDIV, param->segments, bezt_points);
1335 
1336  /* Deformation uses N+1 matrices computed at points between the segments. */
1337  if (for_deform) {
1338  /* Bezier derivatives. */
1339  float bezt_deriv1[3][3], bezt_deriv2[2][3];
1340 
1341  for (int i = 0; i < 3; i++) {
1342  sub_v3_v3v3(bezt_deriv1[i], bezt_controls[i + 1], bezt_controls[i]);
1343  }
1344  for (int i = 0; i < 2; i++) {
1345  sub_v3_v3v3(bezt_deriv2[i], bezt_deriv1[i + 1], bezt_deriv1[i]);
1346  }
1347 
1348  /* End points require special handling to fix zero length handles. */
1349  ease_handle_axis(bezt_deriv1[0], bezt_deriv2[0], axis);
1351  scalemats,
1352  bezt_controls[0],
1353  axis,
1354  roll1,
1355  param->scale_in_x,
1356  param->scale_in_y,
1357  result_array[0].mat);
1358 
1359  for (int a = 1; a < param->segments; a++) {
1360  evaluate_cubic_bezier(bezt_controls, bezt_points[a], cur, axis);
1361 
1362  float fac = ((float)a) / param->segments;
1363  float roll = interpf(roll2, roll1, fac);
1364  float scalex = interpf(param->scale_out_x, param->scale_in_x, fac);
1365  float scaley = interpf(param->scale_out_y, param->scale_in_y, fac);
1366 
1368  param, scalemats, cur, axis, roll, scalex, scaley, result_array[a].mat);
1369  }
1370 
1371  negate_v3(bezt_deriv2[1]);
1372  ease_handle_axis(bezt_deriv1[2], bezt_deriv2[1], axis);
1374  scalemats,
1375  bezt_controls[3],
1376  axis,
1377  roll2,
1378  param->scale_out_x,
1379  param->scale_out_y,
1380  result_array[param->segments].mat);
1381  }
1382  /* Other code (e.g. display) uses matrices for the segments themselves. */
1383  else {
1384  zero_v3(prev);
1385 
1386  for (int a = 0; a < param->segments; a++) {
1387  evaluate_cubic_bezier(bezt_controls, bezt_points[a + 1], cur, axis);
1388 
1389  sub_v3_v3v3(axis, cur, prev);
1390 
1391  float fac = (a + 0.5f) / param->segments;
1392  float roll = interpf(roll2, roll1, fac);
1393  float scalex = interpf(param->scale_out_x, param->scale_in_x, fac);
1394  float scaley = interpf(param->scale_out_y, param->scale_in_y, fac);
1395 
1397  param, scalemats, prev, axis, roll, scalex, scaley, result_array[a].mat);
1398  copy_v3_v3(prev, cur);
1399  }
1400  }
1401 
1402  return param->segments;
1403 }
1404 
1405 static void allocate_bbone_cache(bPoseChannel *pchan, int segments)
1406 {
1407  bPoseChannel_Runtime *runtime = &pchan->runtime;
1408 
1409  if (runtime->bbone_segments != segments) {
1411 
1412  runtime->bbone_segments = segments;
1414  sizeof(Mat4), 1 + (uint)segments, "bPoseChannel_Runtime::bbone_rest_mats");
1416  sizeof(Mat4), 1 + (uint)segments, "bPoseChannel_Runtime::bbone_pose_mats");
1418  sizeof(Mat4), 2 + (uint)segments, "bPoseChannel_Runtime::bbone_deform_mats");
1420  sizeof(DualQuat), 1 + (uint)segments, "bPoseChannel_Runtime::bbone_dual_quats");
1421  }
1422 }
1423 
1426 {
1427  bPoseChannel_Runtime *runtime = &pchan->runtime;
1428  Bone *bone = pchan->bone;
1429  int segments = bone->segments;
1430 
1431  BLI_assert(segments > 1);
1432 
1433  /* Allocate the cache if needed. */
1434  allocate_bbone_cache(pchan, segments);
1435 
1436  /* Compute the shape. */
1437  Mat4 *b_bone = runtime->bbone_pose_mats;
1438  Mat4 *b_bone_rest = runtime->bbone_rest_mats;
1439  Mat4 *b_bone_mats = runtime->bbone_deform_mats;
1440  DualQuat *b_bone_dual_quats = runtime->bbone_dual_quats;
1441  int a;
1442 
1443  BKE_pchan_bbone_spline_setup(pchan, false, true, b_bone);
1444  BKE_pchan_bbone_spline_setup(pchan, true, true, b_bone_rest);
1445 
1446  /* Compute deform matrices. */
1447  /* first matrix is the inverse arm_mat, to bring points in local bone space
1448  * for finding out which segment it belongs to */
1449  invert_m4_m4(b_bone_mats[0].mat, bone->arm_mat);
1450 
1451  /* then we make the b_bone_mats:
1452  * - first transform to local bone space
1453  * - translate over the curve to the bbone mat space
1454  * - transform with b_bone matrix
1455  * - transform back into global space */
1456 
1457  for (a = 0; a <= bone->segments; a++) {
1458  float tmat[4][4];
1459 
1460  invert_m4_m4(tmat, b_bone_rest[a].mat);
1461  mul_m4_series(b_bone_mats[a + 1].mat,
1462  pchan->chan_mat,
1463  bone->arm_mat,
1464  b_bone[a].mat,
1465  tmat,
1466  b_bone_mats[0].mat);
1467 
1468  /* Compute the orthonormal object space rest matrix of the segment. */
1469  mul_m4_m4m4(tmat, bone->arm_mat, b_bone_rest[a].mat);
1470  normalize_m4(tmat);
1471 
1472  mat4_to_dquat(&b_bone_dual_quats[a], tmat, b_bone_mats[a + 1].mat);
1473  }
1474 }
1475 
1478 {
1479  bPoseChannel_Runtime *runtime = &pchan->runtime;
1480  bPoseChannel_Runtime *runtime_from = &pchan_from->runtime;
1481  int segments = runtime_from->bbone_segments;
1482 
1483  if (segments <= 1) {
1485  }
1486  else {
1487  allocate_bbone_cache(pchan, segments);
1488 
1489  memcpy(runtime->bbone_rest_mats, runtime_from->bbone_rest_mats, sizeof(Mat4) * (1 + segments));
1490  memcpy(runtime->bbone_pose_mats, runtime_from->bbone_pose_mats, sizeof(Mat4) * (1 + segments));
1491  memcpy(runtime->bbone_deform_mats,
1492  runtime_from->bbone_deform_mats,
1493  sizeof(Mat4) * (2 + segments));
1494  memcpy(runtime->bbone_dual_quats,
1495  runtime_from->bbone_dual_quats,
1496  sizeof(DualQuat) * (1 + segments));
1497  }
1498 }
1499 
1505  float pos,
1506  int *r_index,
1507  float *r_blend_next)
1508 {
1509  int segments = pchan->bone->segments;
1510 
1511  CLAMP(pos, 0.0f, 1.0f);
1512 
1513  /* Calculate the indices of the 2 affecting b_bone segments.
1514  * Integer part is the first segment's index.
1515  * Integer part plus 1 is the second segment's index.
1516  * Fractional part is the blend factor. */
1517  float pre_blend = pos * (float)segments;
1518 
1519  int index = (int)floorf(pre_blend);
1520  CLAMP(index, 0, segments - 1);
1521 
1522  float blend = pre_blend - index;
1523  CLAMP(blend, 0.0f, 1.0f);
1524 
1525  *r_index = index;
1526  *r_blend_next = blend;
1527 }
1528 
1531 /* -------------------------------------------------------------------- */
1535 /* Convert World-Space Matrix to Pose-Space Matrix */
1536 void BKE_armature_mat_world_to_pose(Object *ob, const float inmat[4][4], float outmat[4][4])
1537 {
1538  float obmat[4][4];
1539 
1540  /* prevent crashes */
1541  if (ob == NULL) {
1542  return;
1543  }
1544 
1545  /* get inverse of (armature) object's matrix */
1546  invert_m4_m4(obmat, ob->obmat);
1547 
1548  /* multiply given matrix by object's-inverse to find pose-space matrix */
1549  mul_m4_m4m4(outmat, inmat, obmat);
1550 }
1551 
1552 /* Convert World-Space Location to Pose-Space Location
1553  * NOTE: this cannot be used to convert to pose-space location of the supplied
1554  * pose-channel into its local space (i.e. 'visual'-keyframing) */
1555 void BKE_armature_loc_world_to_pose(Object *ob, const float inloc[3], float outloc[3])
1556 {
1557  float xLocMat[4][4];
1558  float nLocMat[4][4];
1559 
1560  /* build matrix for location */
1561  unit_m4(xLocMat);
1562  copy_v3_v3(xLocMat[3], inloc);
1563 
1564  /* get bone-space cursor matrix and extract location */
1565  BKE_armature_mat_world_to_pose(ob, xLocMat, nLocMat);
1566  copy_v3_v3(outloc, nLocMat[3]);
1567 }
1568 
1571 /* -------------------------------------------------------------------- */
1575 /* Simple helper, computes the offset bone matrix.
1576  * offs_bone = yoffs(b-1) + root(b) + bonemat(b). */
1577 void BKE_bone_offset_matrix_get(const Bone *bone, float offs_bone[4][4])
1578 {
1579  BLI_assert(bone->parent != NULL);
1580 
1581  /* Bone transform itself. */
1582  copy_m4_m3(offs_bone, bone->bone_mat);
1583 
1584  /* The bone's root offset (is in the parent's coordinate system). */
1585  copy_v3_v3(offs_bone[3], bone->head);
1586 
1587  /* Get the length translation of parent (length along y axis). */
1588  offs_bone[3][1] += bone->parent->length;
1589 }
1590 
1591 /* Construct the matrices (rot/scale and loc)
1592  * to apply the PoseChannels into the armature (object) space.
1593  * I.e. (roughly) the "pose_mat(b-1) * yoffs(b-1) * d_root(b) * bone_mat(b)" in the
1594  * pose_mat(b)= pose_mat(b-1) * yoffs(b-1) * d_root(b) * bone_mat(b) * chan_mat(b)
1595  * ...function.
1596  *
1597  * This allows to get the transformations of a bone in its object space,
1598  * *before* constraints (and IK) get applied (used by pose evaluation code).
1599  * And reverse: to find pchan transformations needed to place a bone at a given loc/rot/scale
1600  * in object space (used by interactive transform, and snapping code).
1601  *
1602  * Note that, with the HINGE/NO_SCALE/NO_LOCAL_LOCATION options, the location matrix
1603  * will differ from the rotation/scale matrix...
1604  *
1605  * NOTE: This cannot be used to convert to pose-space transforms of the supplied
1606  * pose-channel into its local space (i.e. 'visual'-keyframing).
1607  * (note: I don't understand that, so I keep it :p --mont29).
1608  */
1610  BoneParentTransform *r_bpt)
1611 {
1612  const Bone *bone, *parbone;
1613  const bPoseChannel *parchan;
1614 
1615  /* set up variables for quicker access below */
1616  bone = pchan->bone;
1617  parbone = bone->parent;
1618  parchan = pchan->parent;
1619 
1620  if (parchan) {
1621  float offs_bone[4][4];
1622  /* yoffs(b-1) + root(b) + bonemat(b). */
1623  BKE_bone_offset_matrix_get(bone, offs_bone);
1624 
1626  bone->inherit_scale_mode,
1627  offs_bone,
1628  parbone->arm_mat,
1629  parchan->pose_mat,
1630  r_bpt);
1631  }
1632  else {
1634  bone->flag, bone->inherit_scale_mode, bone->arm_mat, NULL, NULL, r_bpt);
1635  }
1636 }
1637 
1638 /* Compute the parent transform using data decoupled from specific data structures.
1639  *
1640  * bone_flag: Bone->flag containing settings
1641  * offs_bone: delta from parent to current arm_mat (or just arm_mat if no parent)
1642  * parent_arm_mat, parent_pose_mat: arm_mat and pose_mat of parent, or NULL
1643  * r_bpt: OUTPUT parent transform */
1645  int inherit_scale_mode,
1646  const float offs_bone[4][4],
1647  const float parent_arm_mat[4][4],
1648  const float parent_pose_mat[4][4],
1649  BoneParentTransform *r_bpt)
1650 {
1651  copy_v3_fl(r_bpt->post_scale, 1.0f);
1652 
1653  if (parent_pose_mat) {
1654  const bool use_rotation = (bone_flag & BONE_HINGE) == 0;
1655  const bool full_transform = use_rotation && inherit_scale_mode == BONE_INHERIT_SCALE_FULL;
1656 
1657  /* Compose the rotscale matrix for this bone. */
1658  if (full_transform) {
1659  /* Parent pose rotation and scale. */
1660  mul_m4_m4m4(r_bpt->rotscale_mat, parent_pose_mat, offs_bone);
1661  }
1662  else {
1663  float tmat[4][4], tscale[3];
1664 
1665  /* If using parent pose rotation: */
1666  if (use_rotation) {
1667  copy_m4_m4(tmat, parent_pose_mat);
1668 
1669  /* Normalize the matrix when needed. */
1670  switch (inherit_scale_mode) {
1673  /* Keep scale and shear. */
1674  break;
1675 
1678  /* Remove scale and shear from parent. */
1679  orthogonalize_m4_stable(tmat, 1, true);
1680  break;
1681 
1683  /* Remove shear and extract scale. */
1684  orthogonalize_m4_stable(tmat, 1, false);
1685  normalize_m4_ex(tmat, r_bpt->post_scale);
1686  break;
1687 
1689  /* Remove only scale - bad legacy way. */
1690  normalize_m4(tmat);
1691  break;
1692 
1693  default:
1695  }
1696  }
1697  /* If removing parent pose rotation: */
1698  else {
1699  copy_m4_m4(tmat, parent_arm_mat);
1700 
1701  /* Copy the parent scale when needed. */
1702  switch (inherit_scale_mode) {
1704  /* Ignore effects of shear. */
1705  mat4_to_size(tscale, parent_pose_mat);
1706  rescale_m4(tmat, tscale);
1707  break;
1708 
1710  /* Take the effects of parent shear into account to get exact volume. */
1711  mat4_to_size_fix_shear(tscale, parent_pose_mat);
1712  rescale_m4(tmat, tscale);
1713  break;
1714 
1716  mat4_to_size_fix_shear(r_bpt->post_scale, parent_pose_mat);
1717  break;
1718 
1722  /* Keep unscaled. */
1723  break;
1724 
1725  default:
1727  }
1728  }
1729 
1730  /* Apply the average parent scale when needed. */
1731  if (inherit_scale_mode == BONE_INHERIT_SCALE_AVERAGE) {
1732  mul_mat3_m4_fl(tmat, cbrtf(fabsf(mat4_to_volume_scale(parent_pose_mat))));
1733  }
1734 
1735  mul_m4_m4m4(r_bpt->rotscale_mat, tmat, offs_bone);
1736 
1737  /* Remove remaining shear when needed, preserving volume. */
1738  if (inherit_scale_mode == BONE_INHERIT_SCALE_FIX_SHEAR) {
1739  orthogonalize_m4_stable(r_bpt->rotscale_mat, 1, false);
1740  }
1741  }
1742 
1743  /* Compose the loc matrix for this bone. */
1744  /* NOTE: That version does not modify bone's loc when HINGE/NO_SCALE options are set. */
1745 
1746  /* In this case, use the object's space *orientation*. */
1747  if (bone_flag & BONE_NO_LOCAL_LOCATION) {
1748  /* XXX I'm sure that code can be simplified! */
1749  float bone_loc[4][4], bone_rotscale[3][3], tmat4[4][4], tmat3[3][3];
1750  unit_m4(bone_loc);
1751  unit_m4(r_bpt->loc_mat);
1752  unit_m4(tmat4);
1753 
1754  mul_v3_m4v3(bone_loc[3], parent_pose_mat, offs_bone[3]);
1755 
1756  unit_m3(bone_rotscale);
1757  copy_m3_m4(tmat3, parent_pose_mat);
1758  mul_m3_m3m3(bone_rotscale, tmat3, bone_rotscale);
1759 
1760  copy_m4_m3(tmat4, bone_rotscale);
1761  mul_m4_m4m4(r_bpt->loc_mat, bone_loc, tmat4);
1762  }
1763  /* Those flags do not affect position, use plain parent transform space! */
1764  else if (!full_transform) {
1765  mul_m4_m4m4(r_bpt->loc_mat, parent_pose_mat, offs_bone);
1766  }
1767  /* Else (i.e. default, usual case),
1768  * just use the same matrix for rotation/scaling, and location. */
1769  else {
1770  copy_m4_m4(r_bpt->loc_mat, r_bpt->rotscale_mat);
1771  }
1772  }
1773  /* Root bones. */
1774  else {
1775  /* Rotation/scaling. */
1776  copy_m4_m4(r_bpt->rotscale_mat, offs_bone);
1777  /* Translation. */
1778  if (bone_flag & BONE_NO_LOCAL_LOCATION) {
1779  /* Translation of arm_mat, without the rotation. */
1780  unit_m4(r_bpt->loc_mat);
1781  copy_v3_v3(r_bpt->loc_mat[3], offs_bone[3]);
1782  }
1783  else {
1784  copy_m4_m4(r_bpt->loc_mat, r_bpt->rotscale_mat);
1785  }
1786  }
1787 }
1788 
1790 {
1791  unit_m4(bpt->rotscale_mat);
1792  unit_m4(bpt->loc_mat);
1793  copy_v3_fl(bpt->post_scale, 1.0f);
1794 }
1795 
1797 {
1798  invert_m4(bpt->rotscale_mat);
1799  invert_m4(bpt->loc_mat);
1800  invert_v3(bpt->post_scale);
1801 }
1802 
1804  const struct BoneParentTransform *in2,
1805  struct BoneParentTransform *result)
1806 {
1807  mul_m4_m4m4(result->rotscale_mat, in1->rotscale_mat, in2->rotscale_mat);
1808  mul_m4_m4m4(result->loc_mat, in1->loc_mat, in2->loc_mat);
1809  mul_v3_v3v3(result->post_scale, in1->post_scale, in2->post_scale);
1810 }
1811 
1813  const float inmat[4][4],
1814  float outmat[4][4])
1815 {
1816  /* in case inmat == outmat */
1817  float tmploc[3];
1818  copy_v3_v3(tmploc, inmat[3]);
1819 
1820  mul_m4_m4m4(outmat, bpt->rotscale_mat, inmat);
1821  mul_v3_m4v3(outmat[3], bpt->loc_mat, tmploc);
1822  rescale_m4(outmat, bpt->post_scale);
1823 }
1824 
1825 /* Convert Pose-Space Matrix to Bone-Space Matrix.
1826  * NOTE: this cannot be used to convert to pose-space transforms of the supplied
1827  * pose-channel into its local space (i.e. 'visual'-keyframing) */
1829  const float inmat[4][4],
1830  float outmat[4][4])
1831 {
1832  BoneParentTransform bpt;
1833 
1836  BKE_bone_parent_transform_apply(&bpt, inmat, outmat);
1837 }
1838 
1839 /* Convert Bone-Space Matrix to Pose-Space Matrix. */
1841  const float inmat[4][4],
1842  float outmat[4][4])
1843 {
1844  BoneParentTransform bpt;
1845 
1847  BKE_bone_parent_transform_apply(&bpt, inmat, outmat);
1848 }
1849 
1850 /* Convert Pose-Space Location to Bone-Space Location
1851  * NOTE: this cannot be used to convert to pose-space location of the supplied
1852  * pose-channel into its local space (i.e. 'visual'-keyframing) */
1853 void BKE_armature_loc_pose_to_bone(bPoseChannel *pchan, const float inloc[3], float outloc[3])
1854 {
1855  float xLocMat[4][4];
1856  float nLocMat[4][4];
1857 
1858  /* build matrix for location */
1859  unit_m4(xLocMat);
1860  copy_v3_v3(xLocMat[3], inloc);
1861 
1862  /* get bone-space cursor matrix and extract location */
1863  BKE_armature_mat_pose_to_bone(pchan, xLocMat, nLocMat);
1864  copy_v3_v3(outloc, nLocMat[3]);
1865 }
1866 
1869 /* -------------------------------------------------------------------- */
1876  Object *ob,
1877  bPoseChannel *pchan,
1878  const float inmat[4][4],
1879  float outmat[4][4])
1880 {
1881  bPoseChannel work_pchan = *pchan;
1882 
1883  /* recalculate pose matrix with only parent transformations,
1884  * bone loc/sca/rot is ignored, scene and frame are not used. */
1885  BKE_pose_where_is_bone(depsgraph, NULL, ob, &work_pchan, 0.0f, false);
1886 
1887  /* Find the matrix, need to remove the bone transforms first so this is calculated
1888  * as a matrix to set rather than a difference on top of what's already there. */
1889  unit_m4(outmat);
1890  BKE_pchan_apply_mat4(&work_pchan, outmat, false);
1891 
1892  BKE_armature_mat_pose_to_bone(&work_pchan, inmat, outmat);
1893 }
1894 
1898 void BKE_pchan_mat3_to_rot(bPoseChannel *pchan, const float mat[3][3], bool use_compat)
1899 {
1900  BLI_ASSERT_UNIT_M3(mat);
1901 
1902  switch (pchan->rotmode) {
1903  case ROT_MODE_QUAT:
1904  mat3_normalized_to_quat(pchan->quat, mat);
1905  break;
1906  case ROT_MODE_AXISANGLE:
1907  mat3_normalized_to_axis_angle(pchan->rotAxis, &pchan->rotAngle, mat);
1908  break;
1909  default: /* euler */
1910  if (use_compat) {
1911  mat3_normalized_to_compatible_eulO(pchan->eul, pchan->eul, pchan->rotmode, mat);
1912  }
1913  else {
1914  mat3_normalized_to_eulO(pchan->eul, pchan->rotmode, mat);
1915  }
1916  break;
1917  }
1918 }
1919 
1923 void BKE_pchan_rot_to_mat3(const bPoseChannel *pchan, float r_mat[3][3])
1924 {
1925  /* rotations may either be quats, eulers (with various rotation orders), or axis-angle */
1926  if (pchan->rotmode > 0) {
1927  /* Euler rotations (will cause gimbal lock,
1928  * but this can be alleviated a bit with rotation orders) */
1929  eulO_to_mat3(r_mat, pchan->eul, pchan->rotmode);
1930  }
1931  else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
1932  /* axis-angle - not really that great for 3D-changing orientations */
1933  axis_angle_to_mat3(r_mat, pchan->rotAxis, pchan->rotAngle);
1934  }
1935  else {
1936  /* quats are normalized before use to eliminate scaling issues */
1937  float quat[4];
1938 
1939  /* NOTE: we now don't normalize the stored values anymore,
1940  * since this was kind of evil in some cases but if this proves to be too problematic,
1941  * switch back to the old system of operating directly on the stored copy. */
1942  normalize_qt_qt(quat, pchan->quat);
1943  quat_to_mat3(r_mat, quat);
1944  }
1945 }
1946 
1951 void BKE_pchan_apply_mat4(bPoseChannel *pchan, const float mat[4][4], bool use_compat)
1952 {
1953  float rot[3][3];
1954  mat4_to_loc_rot_size(pchan->loc, rot, pchan->size, mat);
1955  BKE_pchan_mat3_to_rot(pchan, rot, use_compat);
1956 }
1957 
1963 void BKE_armature_mat_pose_to_delta(float delta_mat[4][4],
1964  float pose_mat[4][4],
1965  float arm_mat[4][4])
1966 {
1967  float imat[4][4];
1968 
1969  invert_m4_m4(imat, arm_mat);
1970  mul_m4_m4m4(delta_mat, imat, pose_mat);
1971 }
1972 
1975 /* -------------------------------------------------------------------- */
1981 /* Called from RNA when rotation mode changes
1982  * - the result should be that the rotations given in the provided pointers have had conversions
1983  * applied (as appropriate), such that the rotation of the element hasn't 'visually' changed */
1985  float quat[4], float eul[3], float axis[3], float *angle, short oldMode, short newMode)
1986 {
1987  /* check if any change - if so, need to convert data */
1988  if (newMode > 0) { /* to euler */
1989  if (oldMode == ROT_MODE_AXISANGLE) {
1990  /* axis-angle to euler */
1991  axis_angle_to_eulO(eul, newMode, axis, *angle);
1992  }
1993  else if (oldMode == ROT_MODE_QUAT) {
1994  /* quat to euler */
1995  normalize_qt(quat);
1996  quat_to_eulO(eul, newMode, quat);
1997  }
1998  /* else { no conversion needed } */
1999  }
2000  else if (newMode == ROT_MODE_QUAT) { /* to quat */
2001  if (oldMode == ROT_MODE_AXISANGLE) {
2002  /* axis angle to quat */
2003  axis_angle_to_quat(quat, axis, *angle);
2004  }
2005  else if (oldMode > 0) {
2006  /* euler to quat */
2007  eulO_to_quat(quat, eul, oldMode);
2008  }
2009  /* else { no conversion needed } */
2010  }
2011  else if (newMode == ROT_MODE_AXISANGLE) { /* to axis-angle */
2012  if (oldMode > 0) {
2013  /* euler to axis angle */
2014  eulO_to_axis_angle(axis, angle, eul, oldMode);
2015  }
2016  else if (oldMode == ROT_MODE_QUAT) {
2017  /* quat to axis angle */
2018  normalize_qt(quat);
2019  quat_to_axis_angle(axis, angle, quat);
2020  }
2021 
2022  /* When converting to axis-angle,
2023  * we need a special exception for the case when there is no axis. */
2024  if (IS_EQF(axis[0], axis[1]) && IS_EQF(axis[1], axis[2])) {
2025  /* for now, rotate around y-axis then (so that it simply becomes the roll) */
2026  axis[1] = 1.0f;
2027  }
2028  }
2029 }
2030 
2033 /* -------------------------------------------------------------------- */
2057 /* Computes vector and roll based on a rotation.
2058  * "mat" must contain only a rotation, and no scaling. */
2059 void mat3_to_vec_roll(const float mat[3][3], float r_vec[3], float *r_roll)
2060 {
2061  if (r_vec) {
2062  copy_v3_v3(r_vec, mat[1]);
2063  }
2064 
2065  if (r_roll) {
2066  mat3_vec_to_roll(mat, mat[1], r_roll);
2067  }
2068 }
2069 
2070 /* Computes roll around the vector that best approximates the matrix.
2071  * If vec is the Y vector from purely rotational mat, result should be exact. */
2072 void mat3_vec_to_roll(const float mat[3][3], const float vec[3], float *r_roll)
2073 {
2074  float vecmat[3][3], vecmatinv[3][3], rollmat[3][3], q[4];
2075 
2076  /* Compute the orientation relative to the vector with zero roll. */
2077  vec_roll_to_mat3(vec, 0.0f, vecmat);
2078  invert_m3_m3(vecmatinv, vecmat);
2079  mul_m3_m3m3(rollmat, vecmatinv, mat);
2080 
2081  /* Extract the twist angle as the roll value. */
2082  mat3_to_quat(q, rollmat);
2083 
2084  *r_roll = quat_split_swing_and_twist(q, 1, NULL, NULL);
2085 }
2086 
2087 /* Calculates the rest matrix of a bone based on its vector and a roll around that vector. */
2155 void vec_roll_to_mat3_normalized(const float nor[3], const float roll, float r_mat[3][3])
2156 {
2157  const float THETA_SAFE = 1.0e-5f; /* theta above this value are always safe to use. */
2158  const float THETA_CRITICAL = 1.0e-9f; /* above this is safe under certain conditions. */
2159 
2160  const float x = nor[0];
2161  const float y = nor[1];
2162  const float z = nor[2];
2163 
2164  const float theta = 1.0f + y;
2165  const float theta_alt = x * x + z * z;
2166  float rMatrix[3][3], bMatrix[3][3];
2167 
2169 
2170  /* When theta is close to zero (nor is aligned close to negative Y Axis),
2171  * we have to check we do have non-null X/Z components as well.
2172  * Also, due to float precision errors, nor can be (0.0, -0.99999994, 0.0) which results
2173  * in theta being close to zero. This will cause problems when theta is used as divisor.
2174  */
2175  if (theta > THETA_SAFE || ((x || z) && theta > THETA_CRITICAL)) {
2176  /* nor is *not* aligned to negative Y-axis (0,-1,0).
2177  * We got these values for free... so be happy with it... ;)
2178  */
2179 
2180  bMatrix[0][1] = -x;
2181  bMatrix[1][0] = x;
2182  bMatrix[1][1] = y;
2183  bMatrix[1][2] = z;
2184  bMatrix[2][1] = -z;
2185 
2186  if (theta > THETA_SAFE) {
2187  /* nor differs significantly from negative Y axis (0,-1,0): apply the general case. */
2188  bMatrix[0][0] = 1 - x * x / theta;
2189  bMatrix[2][2] = 1 - z * z / theta;
2190  bMatrix[2][0] = bMatrix[0][2] = -x * z / theta;
2191  }
2192  else {
2193  /* nor is close to negative Y axis (0,-1,0): apply the special case. */
2194  bMatrix[0][0] = (x + z) * (x - z) / -theta_alt;
2195  bMatrix[2][2] = -bMatrix[0][0];
2196  bMatrix[2][0] = bMatrix[0][2] = 2.0f * x * z / theta_alt;
2197  }
2198  }
2199  else {
2200  /* nor is very close to negative Y axis (0,-1,0): use simple symmetry by Z axis. */
2201  unit_m3(bMatrix);
2202  bMatrix[0][0] = bMatrix[1][1] = -1.0;
2203  }
2204 
2205  /* Make Roll matrix */
2206  axis_angle_normalized_to_mat3(rMatrix, nor, roll);
2207 
2208  /* Combine and output result */
2209  mul_m3_m3m3(r_mat, rMatrix, bMatrix);
2210 }
2211 
2212 void vec_roll_to_mat3(const float vec[3], const float roll, float r_mat[3][3])
2213 {
2214  float nor[3];
2215 
2216  normalize_v3_v3(nor, vec);
2217  vec_roll_to_mat3_normalized(nor, roll, r_mat);
2218 }
2219 
2222 /* -------------------------------------------------------------------- */
2230 void BKE_armature_where_is_bone(Bone *bone, const Bone *bone_parent, const bool use_recursion)
2231 {
2232  float vec[3];
2233 
2234  /* Bone Space */
2235  sub_v3_v3v3(vec, bone->tail, bone->head);
2236  bone->length = len_v3(vec);
2237  vec_roll_to_mat3(vec, bone->roll, bone->bone_mat);
2238 
2239  /* this is called on old file reading too... */
2240  if (bone->xwidth == 0.0f) {
2241  bone->xwidth = 0.1f;
2242  bone->zwidth = 0.1f;
2243  bone->segments = 1;
2244  }
2245 
2246  if (bone_parent) {
2247  float offs_bone[4][4];
2248  /* yoffs(b-1) + root(b) + bonemat(b) */
2249  BKE_bone_offset_matrix_get(bone, offs_bone);
2250 
2251  /* Compose the matrix for this bone */
2252  mul_m4_m4m4(bone->arm_mat, bone_parent->arm_mat, offs_bone);
2253  }
2254  else {
2255  copy_m4_m3(bone->arm_mat, bone->bone_mat);
2256  copy_v3_v3(bone->arm_mat[3], bone->head);
2257  }
2258 
2259  /* and the kiddies */
2260  if (use_recursion) {
2261  bone_parent = bone;
2262  for (bone = bone->childbase.first; bone; bone = bone->next) {
2263  BKE_armature_where_is_bone(bone, bone_parent, use_recursion);
2264  }
2265  }
2266 }
2267 
2268 /* updates vectors and matrices on rest-position level, only needed
2269  * after editing armature itself, now only on reading file */
2271 {
2272  Bone *bone;
2273 
2274  /* hierarchical from root to children */
2275  for (bone = arm->bonebase.first; bone; bone = bone->next) {
2276  BKE_armature_where_is_bone(bone, NULL, true);
2277  }
2278 }
2279 
2282 /* -------------------------------------------------------------------- */
2286 /* if bone layer is protected, copy the data from from->pose
2287  * when used with linked libraries this copies from the linked pose into the local pose */
2288 static void pose_proxy_sync(Object *ob, Object *from, int layer_protected)
2289 {
2290  bPose *pose = ob->pose, *frompose = from->pose;
2291  bPoseChannel *pchan, *pchanp;
2292  bConstraint *con;
2293  int error = 0;
2294 
2295  if (frompose == NULL) {
2296  return;
2297  }
2298 
2299  /* in some cases when rigs change, we cant synchronize
2300  * to avoid crashing check for possible errors here */
2301  for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
2302  if (pchan->bone->layer & layer_protected) {
2303  if (BKE_pose_channel_find_name(frompose, pchan->name) == NULL) {
2304  CLOG_ERROR(&LOG,
2305  "failed to sync proxy armature because '%s' is missing pose channel '%s'",
2306  from->id.name,
2307  pchan->name);
2308  error = 1;
2309  }
2310  }
2311  }
2312 
2313  if (error) {
2314  return;
2315  }
2316 
2317  /* clear all transformation values from library */
2318  BKE_pose_rest(frompose, false);
2319 
2320  /* copy over all of the proxy's bone groups */
2321  /* TODO for later
2322  * - implement 'local' bone groups as for constraints
2323  * Note: this isn't trivial, as bones reference groups by index not by pointer,
2324  * so syncing things correctly needs careful attention */
2325  BLI_freelistN(&pose->agroups);
2326  BLI_duplicatelist(&pose->agroups, &frompose->agroups);
2327  pose->active_group = frompose->active_group;
2328 
2329  for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
2330  pchanp = BKE_pose_channel_find_name(frompose, pchan->name);
2331 
2332  if (UNLIKELY(pchanp == NULL)) {
2333  /* happens for proxies that become invalid because of a missing link
2334  * for regular cases it shouldn't happen at all */
2335  }
2336  else if (pchan->bone->layer & layer_protected) {
2337  ListBase proxylocal_constraints = {NULL, NULL};
2338  bPoseChannel pchanw;
2339 
2340  /* copy posechannel to temp, but restore important pointers */
2341  pchanw = *pchanp;
2342  pchanw.bone = pchan->bone;
2343  pchanw.prev = pchan->prev;
2344  pchanw.next = pchan->next;
2345  pchanw.parent = pchan->parent;
2346  pchanw.child = pchan->child;
2347  pchanw.custom_tx = pchan->custom_tx;
2348  pchanw.bbone_prev = pchan->bbone_prev;
2349  pchanw.bbone_next = pchan->bbone_next;
2350 
2351  pchanw.mpath = pchan->mpath;
2352  pchan->mpath = NULL;
2353 
2354  /* Reset runtime data, we don't want to share that with the proxy. */
2356 
2357  /* this is freed so copy a copy, else undo crashes */
2358  if (pchanw.prop) {
2359  pchanw.prop = IDP_CopyProperty(pchanw.prop);
2360 
2361  /* use the values from the existing props */
2362  if (pchan->prop) {
2363  IDP_SyncGroupValues(pchanw.prop, pchan->prop);
2364  }
2365  }
2366 
2367  /* Constraints - proxy constraints are flushed... local ones are added after
2368  * 1: extract constraints not from proxy (CONSTRAINT_PROXY_LOCAL) from pchan's constraints.
2369  * 2: copy proxy-pchan's constraints on-to new.
2370  * 3: add extracted local constraints back on top.
2371  *
2372  * Note for BKE_constraints_copy:
2373  * When copying constraints, disable 'do_extern' otherwise
2374  * we get the libs direct linked in this blend.
2375  */
2376  BKE_constraints_proxylocal_extract(&proxylocal_constraints, &pchan->constraints);
2377  BKE_constraints_copy(&pchanw.constraints, &pchanp->constraints, false);
2378  BLI_movelisttolist(&pchanw.constraints, &proxylocal_constraints);
2379 
2380  /* constraints - set target ob pointer to own object */
2381  for (con = pchanw.constraints.first; con; con = con->next) {
2383  ListBase targets = {NULL, NULL};
2384  bConstraintTarget *ct;
2385 
2386  if (cti && cti->get_constraint_targets) {
2387  cti->get_constraint_targets(con, &targets);
2388 
2389  for (ct = targets.first; ct; ct = ct->next) {
2390  if (ct->tar == from) {
2391  ct->tar = ob;
2392  }
2393  }
2394 
2395  if (cti->flush_constraint_targets) {
2396  cti->flush_constraint_targets(con, &targets, 0);
2397  }
2398  }
2399  }
2400 
2401  /* free stuff from current channel */
2402  BKE_pose_channel_free(pchan);
2403 
2404  /* copy data in temp back over to the cleaned-out (but still allocated) original channel */
2405  *pchan = pchanw;
2406  if (pchan->custom) {
2407  id_us_plus(&pchan->custom->id);
2408  }
2409  }
2410  else {
2411  /* always copy custom shape */
2412  pchan->custom = pchanp->custom;
2413  if (pchan->custom) {
2414  id_us_plus(&pchan->custom->id);
2415  }
2416  if (pchanp->custom_tx) {
2417  pchan->custom_tx = BKE_pose_channel_find_name(pose, pchanp->custom_tx->name);
2418  }
2419 
2420  /* ID-Property Syncing */
2421  {
2422  IDProperty *prop_orig = pchan->prop;
2423  if (pchanp->prop) {
2424  pchan->prop = IDP_CopyProperty(pchanp->prop);
2425  if (prop_orig) {
2426  /* copy existing values across when types match */
2427  IDP_SyncGroupValues(pchan->prop, prop_orig);
2428  }
2429  }
2430  else {
2431  pchan->prop = NULL;
2432  }
2433  if (prop_orig) {
2434  IDP_FreeProperty(prop_orig);
2435  }
2436  }
2437  }
2438  }
2439 }
2440 
2445  bPose *pose, Bone *bone, bPoseChannel *parchan, int counter, Bone **r_last_visited_bone_p)
2446 {
2447  bPoseChannel *pchan = BKE_pose_channel_verify(pose, bone->name); /* verify checks and/or adds */
2448 
2449  pchan->bone = bone;
2450  pchan->parent = parchan;
2451 
2452  /* We ensure the current pchan is immediately after the one we just generated/updated in the
2453  * previous call to `rebuild_pose_bone`.
2454  *
2455  * It may be either the parent, the previous sibling, or the last
2456  * (grand-(grand-(...)))-child (as processed by the recursive, depth-first nature of this
2457  * function) of the previous sibling.
2458  *
2459  * Note: In most cases there is nothing to do here, but pose list may get out of order when some
2460  * bones are added, removed or moved in the armature data. */
2461  bPoseChannel *pchan_prev = pchan->prev;
2462  const Bone *last_visited_bone = *r_last_visited_bone_p;
2463  if ((pchan_prev == NULL && last_visited_bone != NULL) ||
2464  (pchan_prev != NULL && pchan_prev->bone != last_visited_bone)) {
2465  pchan_prev = last_visited_bone != NULL ?
2466  BKE_pose_channel_find_name(pose, last_visited_bone->name) :
2467  NULL;
2468  BLI_remlink(&pose->chanbase, pchan);
2469  BLI_insertlinkafter(&pose->chanbase, pchan_prev, pchan);
2470  }
2471 
2472  *r_last_visited_bone_p = pchan->bone;
2473  counter++;
2474 
2475  for (bone = bone->childbase.first; bone; bone = bone->next) {
2476  counter = rebuild_pose_bone(pose, bone, pchan, counter, r_last_visited_bone_p);
2477  /* for quick detecting of next bone in chain, only b-bone uses it now */
2478  if (bone->flag & BONE_CONNECTED) {
2479  pchan->child = BKE_pose_channel_find_name(pose, bone->name);
2480  }
2481  }
2482 
2483  return counter;
2484 }
2485 
2491 {
2492  LISTBASE_FOREACH (bPoseChannel *, pchan, &pose->chanbase) {
2493  pchan->bone = NULL;
2494  pchan->child = NULL;
2495  }
2496 }
2497 
2499 {
2500  LISTBASE_FOREACH (bPoseChannel *, pchan, &pose->chanbase) {
2501  pchan->bone = BKE_armature_find_bone_name(armature, pchan->name);
2502  }
2503 }
2504 
2507 {
2508  return (bone != NULL) ? BKE_pose_channel_find_name(pose, bone->name) : NULL;
2509 }
2510 
2513 {
2514  pchan->bbone_prev = pose_channel_find_bone(pose, pchan->bone->bbone_prev);
2515  pchan->bbone_next = pose_channel_find_bone(pose, pchan->bone->bbone_next);
2516 }
2517 
2518 void BKE_pose_channels_clear_with_null_bone(bPose *pose, const bool do_id_user)
2519 {
2520  LISTBASE_FOREACH_MUTABLE (bPoseChannel *, pchan, &pose->chanbase) {
2521  if (pchan->bone == NULL) {
2522  BKE_pose_channel_free_ex(pchan, do_id_user);
2524  BLI_freelinkN(&pose->chanbase, pchan);
2525  }
2526  }
2527 }
2528 
2536 void BKE_pose_rebuild(Main *bmain, Object *ob, bArmature *arm, const bool do_id_user)
2537 {
2538  Bone *bone;
2539  bPose *pose;
2540  bPoseChannel *pchan;
2541  int counter = 0;
2542 
2543  /* only done here */
2544  if (ob->pose == NULL) {
2545  /* create new pose */
2546  ob->pose = MEM_callocN(sizeof(bPose), "new pose");
2547 
2548  /* set default settings for animviz */
2550  }
2551  pose = ob->pose;
2552 
2553  /* clear */
2555 
2556  /* first step, check if all channels are there */
2557  Bone *prev_bone = NULL;
2558  for (bone = arm->bonebase.first; bone; bone = bone->next) {
2559  counter = rebuild_pose_bone(pose, bone, NULL, counter, &prev_bone);
2560  }
2561 
2562  /* and a check for garbage */
2563  BKE_pose_channels_clear_with_null_bone(pose, do_id_user);
2564 
2566 
2567  for (pchan = pose->chanbase.first; pchan; pchan = pchan->next) {
2568  /* Find the custom B-Bone handles. */
2569  BKE_pchan_rebuild_bbone_handles(pose, pchan);
2570  /* Re-validate that we are still using a valid pchan form custom transform. */
2571  /* Note that we could store pointers of freed pchan in a GSet to speed this up, however this is
2572  * supposed to be a rarely used feature, so for now assuming that always building that GSet
2573  * would be less optimal. */
2574  if (pchan->custom_tx != NULL && BLI_findindex(&pose->chanbase, pchan->custom_tx) == -1) {
2575  pchan->custom_tx = NULL;
2576  }
2577  }
2578 
2579  /* printf("rebuild pose %s, %d bones\n", ob->id.name, counter); */
2580 
2581  /* synchronize protected layers with proxy */
2582  /* HACK! To preserve 2.7x behavior that you always can pose even locked bones,
2583  * do not do any restoration if this is a COW temp copy! */
2584  /* Switched back to just NO_MAIN tag, for some reasons (c)
2585  * using COW tag was working this morning, but not anymore... */
2586  if (ob->proxy != NULL && (ob->id.tag & LIB_TAG_NO_MAIN) == 0) {
2588  pose_proxy_sync(ob, ob->proxy, arm->layer_protected);
2589  }
2590 
2591  BKE_pose_update_constraint_flags(pose); /* for IK detection for example */
2592 
2593  pose->flag &= ~POSE_RECALC;
2594  pose->flag |= POSE_WAS_REBUILT;
2595 
2596  /* Rebuilding poses forces us to also rebuild the dependency graph,
2597  * since there is one node per pose/bone. */
2598  if (bmain != NULL) {
2599  DEG_relations_tag_update(bmain);
2600  }
2601 }
2602 
2608 void BKE_pose_ensure(Main *bmain, Object *ob, bArmature *arm, const bool do_id_user)
2609 {
2610  BLI_assert(!ELEM(NULL, arm, ob));
2611  if (ob->type == OB_ARMATURE && ((ob->pose == NULL) || (ob->pose->flag & POSE_RECALC))) {
2612  BLI_assert(GS(arm->id.name) == ID_AR);
2613  BKE_pose_rebuild(bmain, ob, arm, do_id_user);
2614  }
2615 }
2616 
2619 /* -------------------------------------------------------------------- */
2626 void BKE_pchan_to_mat4(const bPoseChannel *pchan, float r_chanmat[4][4])
2627 {
2628  float smat[3][3];
2629  float rmat[3][3];
2630  float tmat[3][3];
2631 
2632  /* get scaling matrix */
2633  size_to_mat3(smat, pchan->size);
2634 
2635  /* get rotation matrix */
2636  BKE_pchan_rot_to_mat3(pchan, rmat);
2637 
2638  /* calculate matrix of bone (as 3x3 matrix, but then copy the 4x4) */
2639  mul_m3_m3m3(tmat, rmat, smat);
2640  copy_m4_m3(r_chanmat, tmat);
2641 
2642  /* prevent action channels breaking chains */
2643  /* need to check for bone here, CONSTRAINT_TYPE_ACTION uses this call */
2644  if ((pchan->bone == NULL) || !(pchan->bone->flag & BONE_CONNECTED)) {
2645  copy_v3_v3(r_chanmat[3], pchan->loc);
2646  }
2647 }
2648 
2649 /* loc/rot/size to mat4 */
2650 /* used in constraint.c too */
2652 {
2653  /* this is just a wrapper around the copy of this function which calculates the matrix
2654  * and stores the result in any given channel
2655  */
2656  BKE_pchan_to_mat4(pchan, pchan->chan_mat);
2657 }
2658 
2659 /* calculate tail of posechannel */
2661 {
2662  float vec[3];
2663 
2664  copy_v3_v3(vec, pchan->pose_mat[1]);
2665  mul_v3_fl(vec, pchan->bone->length);
2666  add_v3_v3v3(pchan->pose_tail, pchan->pose_head, vec);
2667 }
2668 
2669 /* The main armature solver, does all constraints excluding IK */
2670 /* pchan is validated, as having bone and parent pointer
2671  * 'do_extra': when zero skips loc/size/rot, constraints and strip modifiers.
2672  */
2674  Scene *scene,
2675  Object *ob,
2676  bPoseChannel *pchan,
2677  float ctime,
2678  bool do_extra)
2679 {
2680  /* This gives a chan_mat with actions (ipos) results. */
2681  if (do_extra) {
2682  BKE_pchan_calc_mat(pchan);
2683  }
2684  else {
2685  unit_m4(pchan->chan_mat);
2686  }
2687 
2688  /* Construct the posemat based on PoseChannels, that we do before applying constraints. */
2689  /* pose_mat(b) = pose_mat(b-1) * yoffs(b-1) * d_root(b) * bone_mat(b) * chan_mat(b) */
2690  BKE_armature_mat_bone_to_pose(pchan, pchan->chan_mat, pchan->pose_mat);
2691 
2692  /* Only rootbones get the cyclic offset (unless user doesn't want that). */
2693  /* XXX That could be a problem for snapping and other "reverse transform" features... */
2694  if (!pchan->parent) {
2695  if ((pchan->bone->flag & BONE_NO_CYCLICOFFSET) == 0) {
2696  add_v3_v3(pchan->pose_mat[3], ob->pose->cyclic_offset);
2697  }
2698  }
2699 
2700  if (do_extra) {
2701  /* Do constraints */
2702  if (pchan->constraints.first) {
2703  bConstraintOb *cob;
2704  float vec[3];
2705 
2706  /* make a copy of location of PoseChannel for later */
2707  copy_v3_v3(vec, pchan->pose_mat[3]);
2708 
2709  /* prepare PoseChannel for Constraint solving
2710  * - makes a copy of matrix, and creates temporary struct to use
2711  */
2713 
2714  /* Solve PoseChannel's Constraints */
2715 
2716  /* ctime doesn't alter objects. */
2717  BKE_constraints_solve(depsgraph, &pchan->constraints, cob, ctime);
2718 
2719  /* cleanup after Constraint Solving
2720  * - applies matrix back to pchan, and frees temporary struct used
2721  */
2723 
2724  /* prevent constraints breaking a chain */
2725  if (pchan->bone->flag & BONE_CONNECTED) {
2726  copy_v3_v3(pchan->pose_mat[3], vec);
2727  }
2728  }
2729  }
2730 
2731  /* calculate head */
2732  copy_v3_v3(pchan->pose_head, pchan->pose_mat[3]);
2733  /* calculate tail */
2735 }
2736 
2737 /* This only reads anim data from channels, and writes to channels */
2738 /* This is the only function adding poses */
2740 {
2741  bArmature *arm;
2742  Bone *bone;
2743  bPoseChannel *pchan;
2744  float imat[4][4];
2745  float ctime;
2746 
2747  if (ob->type != OB_ARMATURE) {
2748  return;
2749  }
2750  arm = ob->data;
2751 
2752  if (ELEM(NULL, arm, scene)) {
2753  return;
2754  }
2755  /* WARNING! passing NULL bmain here means we won't tag depsgraph's as dirty -
2756  * hopefully this is OK. */
2757  BKE_pose_ensure(NULL, ob, arm, true);
2758 
2759  ctime = BKE_scene_frame_get(scene); /* not accurate... */
2760 
2761  /* In edit-mode or rest-position we read the data from the bones. */
2762  if (arm->edbo || (arm->flag & ARM_RESTPOS)) {
2763  for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
2764  bone = pchan->bone;
2765  if (bone) {
2766  copy_m4_m4(pchan->pose_mat, bone->arm_mat);
2767  copy_v3_v3(pchan->pose_head, bone->arm_head);
2768  copy_v3_v3(pchan->pose_tail, bone->arm_tail);
2769  }
2770  }
2771  }
2772  else {
2773  invert_m4_m4(ob->imat, ob->obmat); /* imat is needed */
2774 
2775  /* 1. clear flags */
2776  for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
2777  pchan->flag &= ~(POSE_DONE | POSE_CHAIN | POSE_IKTREE | POSE_IKSPLINE);
2778  }
2779 
2780  /* 2a. construct the IK tree (standard IK) */
2781  BIK_init_tree(depsgraph, scene, ob, ctime);
2782 
2783  /* 2b. construct the Spline IK trees
2784  * - this is not integrated as an IK plugin, since it should be able
2785  * to function in conjunction with standard IK
2786  */
2787  BKE_pose_splineik_init_tree(scene, ob, ctime);
2788 
2789  /* 3. the main loop, channels are already hierarchical sorted from root to children */
2790  for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
2791  /* 4a. if we find an IK root, we handle it separated */
2792  if (pchan->flag & POSE_IKTREE) {
2793  BIK_execute_tree(depsgraph, scene, ob, pchan, ctime);
2794  }
2795  /* 4b. if we find a Spline IK root, we handle it separated too */
2796  else if (pchan->flag & POSE_IKSPLINE) {
2797  BKE_splineik_execute_tree(depsgraph, scene, ob, pchan, ctime);
2798  }
2799  /* 5. otherwise just call the normal solver */
2800  else if (!(pchan->flag & POSE_DONE)) {
2801  BKE_pose_where_is_bone(depsgraph, scene, ob, pchan, ctime, 1);
2802  }
2803  }
2804  /* 6. release the IK tree */
2805  BIK_release_tree(scene, ob, ctime);
2806  }
2807 
2808  /* calculating deform matrices */
2809  for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
2810  if (pchan->bone) {
2811  invert_m4_m4(imat, pchan->bone->arm_mat);
2812  mul_m4_m4m4(pchan->chan_mat, pchan->pose_mat, imat);
2813  }
2814  }
2815 }
2816 
2819 /* -------------------------------------------------------------------- */
2823 static int minmax_armature(Object *ob, float r_min[3], float r_max[3])
2824 {
2825  bPoseChannel *pchan;
2826 
2827  /* For now, we assume BKE_pose_where_is has already been called
2828  * (hence we have valid data in pachan). */
2829  for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
2830  minmax_v3v3_v3(r_min, r_max, pchan->pose_head);
2831  minmax_v3v3_v3(r_min, r_max, pchan->pose_tail);
2832  }
2833 
2834  return (BLI_listbase_is_empty(&ob->pose->chanbase) == false);
2835 }
2836 
2837 static void boundbox_armature(Object *ob)
2838 {
2839  BoundBox *bb;
2840  float min[3], max[3];
2841 
2842  if (ob->runtime.bb == NULL) {
2843  ob->runtime.bb = MEM_callocN(sizeof(BoundBox), "Armature boundbox");
2844  }
2845  bb = ob->runtime.bb;
2846 
2847  INIT_MINMAX(min, max);
2848  if (!minmax_armature(ob, min, max)) {
2849  min[0] = min[1] = min[2] = -1.0f;
2850  max[0] = max[1] = max[2] = 1.0f;
2851  }
2852 
2854 
2855  bb->flag &= ~BOUNDBOX_DIRTY;
2856 }
2857 
2859 {
2860  boundbox_armature(ob);
2861 
2862  return ob->runtime.bb;
2863 }
2864 
2865 bool BKE_pose_minmax(Object *ob, float r_min[3], float r_max[3], bool use_hidden, bool use_select)
2866 {
2867  bool changed = false;
2868 
2869  if (ob->pose) {
2870  bArmature *arm = ob->data;
2871  bPoseChannel *pchan;
2872 
2873  for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
2874  /* XXX pchan->bone may be NULL for duplicated bones, see duplicateEditBoneObjects() comment
2875  * (editarmature.c:2592)... Skip in this case too! */
2876  if (pchan->bone && (!((use_hidden == false) && (PBONE_VISIBLE(arm, pchan->bone) == false)) &&
2877  !((use_select == true) && ((pchan->bone->flag & BONE_SELECTED) == 0)))) {
2878  bPoseChannel *pchan_tx = (pchan->custom && pchan->custom_tx) ? pchan->custom_tx : pchan;
2879  BoundBox *bb_custom = ((pchan->custom) && !(arm->flag & ARM_NO_CUSTOM)) ?
2881  NULL;
2882  if (bb_custom) {
2883  float mat[4][4], smat[4][4];
2884  scale_m4_fl(smat, PCHAN_CUSTOM_DRAW_SIZE(pchan));
2885  mul_m4_series(mat, ob->obmat, pchan_tx->pose_mat, smat);
2886  BKE_boundbox_minmax(bb_custom, mat, r_min, r_max);
2887  }
2888  else {
2889  float vec[3];
2890  mul_v3_m4v3(vec, ob->obmat, pchan_tx->pose_head);
2891  minmax_v3v3_v3(r_min, r_max, vec);
2892  mul_v3_m4v3(vec, ob->obmat, pchan_tx->pose_tail);
2893  minmax_v3v3_v3(r_min, r_max, vec);
2894  }
2895 
2896  changed = true;
2897  }
2898  }
2899  }
2900 
2901  return changed;
2902 }
2903 
2906 /* -------------------------------------------------------------------- */
2911 {
2912  bPoseChannel *rootchan = pchan;
2913  if (!(data->flag & CONSTRAINT_IK_TIP)) {
2914  /* Exclude tip from chain. */
2915  rootchan = rootchan->parent;
2916  }
2917  if (rootchan != NULL) {
2918  int segcount = 0;
2919  while (rootchan->parent) {
2920  /* Continue up chain, until we reach target number of items. */
2921  segcount++;
2922  if (segcount == data->rootbone) {
2923  break;
2924  }
2925  rootchan = rootchan->parent;
2926  }
2927  }
2928  return rootchan;
2929 }
2930 
2933 {
2934  bPoseChannel *rootchan = pchan;
2935  int segcount = 0;
2936  BLI_assert(rootchan != NULL);
2937  while (rootchan->parent) {
2938  /* Continue up chain, until we reach target number of items. */
2939  segcount++;
2940  if (segcount == data->chainlen) {
2941  break;
2942  }
2943  rootchan = rootchan->parent;
2944  }
2945  return rootchan;
2946 }
2947 
typedef float(TangentPoint)[2]
void BIK_init_tree(struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob, float ctime)
Definition: ikplugin_api.c:83
void BIK_release_tree(struct Scene *scene, struct Object *ob, float ctime)
Definition: ikplugin_api.c:102
void BIK_execute_tree(struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob, struct bPoseChannel *pchan, float ctime)
Definition: ikplugin_api.c:92
Blender kernel action and pose functionality.
void BKE_pose_channels_hash_free(struct bPose *pose)
Definition: action.c:960
void BKE_pose_channel_runtime_reset_on_copy(struct bPoseChannel_Runtime *runtime)
Definition: action.c:1094
struct bPoseChannel * BKE_pose_channel_find_name(const struct bPose *pose, const char *name)
void BKE_pose_channels_hash_make(struct bPose *pose)
Definition: action.c:948
void BKE_pose_channel_free(struct bPoseChannel *pchan)
Definition: action.c:1117
void BKE_pose_channel_free_ex(struct bPoseChannel *pchan, bool do_id_user)
Definition: action.c:1059
struct bPoseChannel * BKE_pose_channel_verify(struct bPose *pose, const char *name)
Definition: action.c:638
void BKE_pose_update_constraint_flags(struct bPose *pose)
Definition: action.c:1247
void BKE_pose_channel_free_bbone_cache(struct bPoseChannel_Runtime *runtime)
Definition: action.c:1108
void BKE_pose_rest(struct bPose *pose, bool selected_bones_only)
Definition: action.c:1636
void BKE_animdata_blend_read_data(struct BlendDataReader *reader, struct AnimData *adt)
Definition: anim_data.c:1574
void BKE_animdata_blend_write(struct BlendWriter *writer, struct AnimData *adt)
Definition: anim_data.c:1552
void animviz_settings_init(struct bAnimVizSettings *avs)
void BKE_pose_splineik_init_tree(struct Scene *scene, struct Object *ob, float ctime)
#define MAX_BBONE_SUBDIV
Definition: BKE_armature.h:277
void BKE_splineik_execute_tree(struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob, struct bPoseChannel *pchan_root, float ctime)
#define PBONE_VISIBLE(arm, bone)
Definition: BKE_armature.h:338
void BKE_constraints_solve(struct Depsgraph *depsgraph, struct ListBase *conlist, struct bConstraintOb *cob, float ctime)
Definition: constraint.c:6177
void BKE_constraints_proxylocal_extract(struct ListBase *dst, struct ListBase *src)
Definition: constraint.c:5978
void BKE_constraints_copy(struct ListBase *dst, const struct ListBase *src, bool do_extern)
struct bConstraintOb * BKE_constraints_make_evalob(struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob, void *subdata, short datatype)
Definition: constraint.c:133
const bConstraintTypeInfo * BKE_constraint_typeinfo_get(struct bConstraint *con)
Definition: constraint.c:5435
void BKE_constraints_clear_evalob(struct bConstraintOb *cob)
Definition: constraint.c:211
void BKE_curve_forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
Definition: curve.c:1804
void IDP_BlendReadExpand(struct BlendExpander *expander, struct IDProperty *prop)
Definition: idprop.c:1336
void IDP_FreeProperty_ex(struct IDProperty *prop, const bool do_id_user)
Definition: idprop.c:1034
void IDP_BlendWrite(struct BlendWriter *writer, const struct IDProperty *prop)
void IDP_BlendReadLib(struct BlendLibReader *reader, struct IDProperty *prop)
Definition: idprop.c:1300
#define IDP_BlendDataRead(reader, prop)
Definition: BKE_idprop.h:208
void IDP_foreach_property(struct IDProperty *id_property_root, const int type_filter, IDPForeachPropertyCallback callback, void *user_data)
Definition: idprop.c:1072
void IDP_FreeProperty(struct IDProperty *prop)
Definition: idprop.c:1040
void IDP_SyncGroupValues(struct IDProperty *dest, const struct IDProperty *src) ATTR_NONNULL()
struct IDProperty * IDP_CopyProperty_ex(const struct IDProperty *prop, const int flag) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
struct IDProperty * IDP_CopyProperty(const struct IDProperty *prop) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
@ LIB_ID_CREATE_NO_USER_REFCOUNT
Definition: BKE_lib_id.h:92
void id_us_plus(struct ID *id)
Definition: lib_id.c:288
void BKE_id_blend_write(struct BlendWriter *writer, struct ID *id)
Definition: lib_id.c:2395
void * BKE_id_new(struct Main *bmain, const short type, const char *name)
Definition: lib_id.c:1177
void BKE_lib_query_idpropertiesForeachIDLink_callback(struct IDProperty *id_prop, void *user_data)
Definition: lib_query.c:150
General operations, lookup, etc. for blender objects.
void BKE_object_copy_proxy_drivers(struct Object *ob, struct Object *target)
Definition: object.c:2838
void BKE_boundbox_init_from_minmax(struct BoundBox *bb, const float min[3], const float max[3])
Definition: object.c:3778
struct BoundBox * BKE_object_boundbox_get(struct Object *ob)
Definition: object.c:3817
void BKE_boundbox_minmax(const struct BoundBox *bb, const float obmat[4][4], float r_min[3], float r_max[3])
float BKE_scene_frame_get(const struct Scene *scene)
#define BLI_array_alloca(arr, realsize)
Definition: BLI_alloca.h:36
#define BLI_assert_unreachable()
Definition: BLI_assert.h:96
#define BLI_assert(a)
Definition: BLI_assert.h:58
GHash * BLI_ghash_str_new_ex(const char *info, const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
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
void * BLI_ghash_lookup(GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:803
BLI_INLINE bool BLI_listbase_is_empty(const struct ListBase *lb)
Definition: BLI_listbase.h:124
#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 void void void void BLI_duplicatelist(struct ListBase *dst, const struct ListBase *src) ATTR_NONNULL(1
#define LISTBASE_FOREACH_MUTABLE(type, var, list)
Definition: BLI_listbase.h:188
void BLI_insertlinkafter(struct ListBase *listbase, void *vprevlink, void *vnewlink) ATTR_NONNULL(1)
Definition: listbase.c:352
void void void BLI_movelisttolist(struct ListBase *dst, struct ListBase *src) ATTR_NONNULL(1
bool BLI_remlink_safe(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:159
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:547
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:133
int BLI_findindex(const struct ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
#define BLI_ASSERT_UNIT_M3(m)
MINLINE float interpf(float a, float b, float t)
#define BLI_ASSERT_UNIT_V3(v)
float cubic_tangent_factor_circle_v3(const float tan_l[3], const float tan_r[3])
Definition: math_geom.c:6229
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
Definition: math_matrix.c:262
void mul_mat3_m4_fl(float R[4][4], float f)
Definition: math_matrix.c:982
void size_to_mat3(float R[3][3], const float size[3])
Definition: math_matrix.c:2105
bool invert_m4(float R[4][4])
Definition: math_matrix.c:1187
void copy_m3_m3(float m1[3][3], const float m2[3][3])
Definition: math_matrix.c:89
void unit_m3(float m[3][3])
Definition: math_matrix.c:58
void copy_m3_m4(float m1[3][3], const float m2[4][4])
Definition: math_matrix.c:105
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
void mul_mat3_m4_v3(const float M[4][4], float r[3])
Definition: math_matrix.c:794
void mat4_to_size_fix_shear(float size[3], const float M[4][4])
Definition: math_matrix.c:2156
bool invert_m4_m4(float R[4][4], const float A[4][4])
Definition: math_matrix.c:1278
void mat4_to_loc_rot_size(float loc[3], float rot[3][3], float size[3], const float wmat[4][4])
Definition: math_matrix.c:2236
void normalize_m3(float R[3][3]) ATTR_NONNULL()
Definition: math_matrix.c:1919
void rescale_m4(float mat[4][4], const float scale[3])
Definition: math_matrix.c:2396
void size_to_mat4(float R[4][4], const float size[3])
Definition: math_matrix.c:2118
void mul_m4_v3(const float M[4][4], float r[3])
Definition: math_matrix.c:732
void orthogonalize_m4_stable(float R[4][4], int axis, bool normalize)
Definition: math_matrix.c:1685
#define mul_m4_series(...)
void scale_m4_fl(float R[4][4], float scale)
Definition: math_matrix.c:2309
void normalize_m4_ex(float R[4][4], float r_scale[3]) ATTR_NONNULL()
Definition: math_matrix.c:1942
bool invert_m3_m3(float R[3][3], const float A[3][3])
Definition: math_matrix.c:1161
void copy_m4_m4(float m1[4][4], const float m2[4][4])
Definition: math_matrix.c:95
float mat4_to_scale(const float M[4][4])
Definition: math_matrix.c:2196
void mul_v3_m4v3(float r[3], const float M[4][4], const float v[3])
Definition: math_matrix.c:742
bool invert_m3(float R[3][3])
Definition: math_matrix.c:1152
void mat4_to_size(float size[3], const float M[4][4])
Definition: math_matrix.c:2145
float mat4_to_volume_scale(const float M[4][4])
Definition: math_matrix.c:2177
void mul_m3_m3m3(float R[3][3], const float A[3][3], const float B[3][3])
Definition: math_matrix.c:391
void normalize_m4(float R[4][4]) ATTR_NONNULL()
Definition: math_matrix.c:1952
void quat_to_eulO(float eul[3], const short order, const float quat[4])
void eulO_to_quat(float quat[4], const float eul[3], const short order)
void eulO_to_axis_angle(float axis[3], float *angle, const float eul[3], const short order)
void mat3_to_quat(float q[4], const float mat[3][3])
void axis_angle_to_quat(float r[4], const float axis[3], const float angle)
float normalize_qt(float q[4])
void axis_angle_normalized_to_mat3(float R[3][3], const float axis[3], const float angle)
float normalize_qt_qt(float r[4], const float q[4])
void mat3_normalized_to_axis_angle(float axis[3], float *angle, const float M[3][3])
void quat_to_axis_angle(float axis[3], float *angle, const float q[4])
void mat3_normalized_to_compatible_eulO(float eul[3], const float old[3], const short order, const float mat[3][3])
void mat4_to_dquat(DualQuat *dq, const float basemat[4][4], const float mat[4][4])
void axis_angle_to_eulO(float eul[3], const short order, const float axis[3], const float angle)
void eulO_to_mat3(float mat[3][3], const float eul[3], const short order)
void axis_angle_to_mat3(float R[3][3], const float axis[3], const float angle)
void mat3_normalized_to_eulO(float eul[3], const short order, const float mat[3][3])
void quat_to_mat3(float mat[3][3], const float q[4])
float quat_split_swing_and_twist(const float q[4], int axis, float r_swing[4], float r_twist[4])
void mat3_normalized_to_quat(float q[4], const float mat[3][3])
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], const float t)
Definition: math_vector.c:49
MINLINE float len_squared_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void minmax_v3v3_v3(float min[3], float max[3], const float vec[3])
Definition: math_vector.c:1020
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
MINLINE void mul_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE float normalize_v3(float r[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void copy_v3_fl3(float v[3], float x, float y, float z)
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void negate_v3(float r[3])
MINLINE void invert_v3(float r[3])
MINLINE float normalize_v3_v3(float r[3], const float a[3])
MINLINE void copy_v3_fl(float r[3], float f)
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
MINLINE void zero_v3(float r[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
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 CLAMP_MAX(a, c)
#define INIT_MINMAX(min, max)
#define UNUSED(x)
#define UNLIKELY(x)
#define ELEM(...)
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define IS_EQF(a, b)
#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member)
#define STREQ(a, b)
#define BLO_read_data_address(reader, ptr_p)
#define BLO_write_id_struct(writer, struct_name, id_address, id)
#define BLO_write_struct(writer, struct_name, data_ptr)
void BLO_read_list(BlendDataReader *reader, struct ListBase *list)
Definition: readfile.c:5654
bool BLO_write_is_undo(BlendWriter *writer)
Definition: writefile.c:1412
#define BLT_I18NCONTEXT_ID_ARMATURE
#define CLOG_ERROR(clg_ref,...)
Definition: CLG_log.h:204
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:51
bool DEG_is_active(const struct Depsgraph *depsgraph)
Definition: depsgraph.cc:331
void DEG_relations_tag_update(struct Main *bmain)
struct ID * DEG_get_original_id(struct ID *id)
@ LIB_TAG_NO_MAIN
Definition: DNA_ID.h:572
#define FILTER_ID_AR
Definition: DNA_ID.h:706
@ IDP_TYPE_FILTER_ID
Definition: DNA_ID.h:115
@ INDEX_ID_AR
Definition: DNA_ID.h:826
@ ID_AR
Definition: DNA_ID_enums.h:78
@ ROT_MODE_QUAT
@ ROT_MODE_AXISANGLE
#define PCHAN_CUSTOM_DRAW_SIZE(pchan)
@ POSE_DONE
@ POSE_IKTREE
@ POSE_IKSPLINE
@ POSE_CHAIN
@ POSE_WAS_REBUILT
@ POSE_RECALC
#define MAXBONENAME
@ BBONE_HANDLE_AUTO
@ BBONE_HANDLE_TANGENT
@ BBONE_HANDLE_RELATIVE
@ BONE_DRAW_LOCKED_WEIGHT
@ BONE_SELECTED
@ BONE_NO_CYCLICOFFSET
@ BONE_NO_LOCAL_LOCATION
@ BONE_ADD_PARENT_END_ROLL
@ BONE_DRAW_ACTIVE
@ BONE_CONNECTED
@ BONE_HINGE
@ ARM_NO_CUSTOM
@ ARM_RESTPOS
struct bArmature bArmature
@ BONE_INHERIT_SCALE_FULL
@ BONE_INHERIT_SCALE_NONE
@ BONE_INHERIT_SCALE_FIX_SHEAR
@ BONE_INHERIT_SCALE_NONE_LEGACY
@ BONE_INHERIT_SCALE_ALIGNED
@ BONE_INHERIT_SCALE_AVERAGE
@ CONSTRAINT_IK_TIP
@ CONSTRAINT_OBTYPE_BONE
#define DNA_struct_default_get(struct_name)
Definition: DNA_defaults.h:44
These structs are the foundation for all linked lists in the library system.
Object is a sort of wrapper for general info.
@ BOUNDBOX_DIRTY
@ OB_ARMATURE
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble z
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
Read Guarded memory(de)allocation.
static void init_data(ModifierData *md)
Group RGB to Bright Vector Camera CLAMP
static void armature_copy_data(Main *UNUSED(bmain), ID *id_dst, const ID *id_src, const int flag)
Definition: armature.c:110
void BKE_pchan_rot_to_mat3(const bPoseChannel *pchan, float r_mat[3][3])
Definition: armature.c:1923
void BKE_armature_bone_hash_make(bArmature *arm)
Definition: armature.c:644
void BKE_pose_where_is(struct Depsgraph *depsgraph, Scene *scene, Object *ob)
Definition: armature.c:2739
static void copy_bone_transform(Bone *bone_dst, const Bone *bone_src)
Definition: armature.c:447
void vec_roll_to_mat3(const float vec[3], const float roll, float r_mat[3][3])
Definition: armature.c:2212
bArmature * BKE_armature_from_object(Object *ob)
Definition: armature.c:353
static void make_bbone_spline_matrix(BBoneSplineParameters *param, const float scalemats[2][4][4], const float pos[3], const float axis[3], float roll, float scalex, float scaley, float result[4][4])
Definition: armature.c:1263
static void ease_handle_axis(const float deriv1[3], const float deriv2[3], float r_axis[3])
Definition: armature.c:1290
IDTypeInfo IDType_ID_AR
Definition: armature.c:311
static void allocate_bbone_cache(bPoseChannel *pchan, int segments)
Definition: armature.c:1405
void BKE_pchan_bbone_deform_segment_index(const bPoseChannel *pchan, float pos, int *r_index, float *r_blend_next)
Definition: armature.c:1504
bool bone_autoside_name(char name[MAXBONENAME], int UNUSED(strip_number), short axis, float head, float tail)
Definition: armature.c:717
static int minmax_armature(Object *ob, float r_min[3], float r_max[3])
Definition: armature.c:2823
static bPoseChannel * pose_channel_find_bone(bPose *pose, Bone *bone)
Definition: armature.c:2506
static void equalize_cubic_bezier(const float control[4][3], int temp_segments, int final_segments, float *r_t_points)
Definition: armature.c:853
void BKE_armature_refresh_layer_used(struct Depsgraph *depsgraph, struct bArmature *arm)
Definition: armature.c:690
void BKE_armature_loc_pose_to_bone(bPoseChannel *pchan, const float inloc[3], float outloc[3])
Definition: armature.c:1853
void BKE_armature_bonelist_free(ListBase *lb, const bool do_id_user)
Definition: armature.c:371
void BKE_pose_channels_clear_with_null_bone(bPose *pose, const bool do_id_user)
Definition: armature.c:2518
void BKE_pose_ensure(Main *bmain, Object *ob, bArmature *arm, const bool do_id_user)
Definition: armature.c:2608
void BKE_armature_where_is_bone(Bone *bone, const Bone *bone_parent, const bool use_recursion)
Definition: armature.c:2230
void BKE_pose_rebuild(Main *bmain, Object *ob, bArmature *arm, const bool do_id_user)
Definition: armature.c:2536
void mat3_vec_to_roll(const float mat[3][3], const float vec[3], float *r_roll)
Definition: armature.c:2072
static void write_bone(BlendWriter *writer, Bone *bone)
Definition: armature.c:192
static void armature_transform_recurse(ListBase *bonebase, const float mat[4][4], const bool do_props, const float mat3[3][3], const float scale, const Bone *bone_parent, const float arm_mat_parent_inv[4][4])
Definition: armature.c:485
void BKE_armature_mat_bone_to_pose(bPoseChannel *pchan, const float inmat[4][4], float outmat[4][4])
Definition: armature.c:1840
bArmature * BKE_armature_add(Main *bmain, const char *name)
Definition: armature.c:345
void BKE_pchan_calc_mat(bPoseChannel *pchan)
Definition: armature.c:2651
int BKE_armature_bonelist_count(ListBase *lb)
Definition: armature.c:361
void BKE_pose_where_is_bone_tail(bPoseChannel *pchan)
Definition: armature.c:2660
static void expand_bones(BlendExpander *expander, Bone *bone)
Definition: armature.c:294
void BKE_pchan_apply_mat4(bPoseChannel *pchan, const float mat[4][4], bool use_compat)
Definition: armature.c:1951
void BKE_armature_mat_pose_to_bone_ex(struct Depsgraph *depsgraph, Object *ob, bPoseChannel *pchan, const float inmat[4][4], float outmat[4][4])
Definition: armature.c:1875
void BKE_armature_mat_pose_to_bone(bPoseChannel *pchan, const float inmat[4][4], float outmat[4][4])
Definition: armature.c:1828
void BKE_bone_parent_transform_combine(const struct BoneParentTransform *in1, const struct BoneParentTransform *in2, struct BoneParentTransform *result)
Definition: armature.c:1803
static void armature_foreach_id_bone(Bone *bone, LibraryForeachIDData *data)
Definition: armature.c:162
Bone * BKE_armature_find_bone_name(bArmature *arm, const char *name)
Definition: armature.c:609
void BKE_pchan_rebuild_bbone_handles(bPose *pose, bPoseChannel *pchan)
Definition: armature.c:2512
static void boundbox_armature(Object *ob)
Definition: armature.c:2837
void BKE_bone_parent_transform_invert(struct BoneParentTransform *bpt)
Definition: armature.c:1796
void BKE_pose_clear_pointers(bPose *pose)
Definition: armature.c:2490
bool BKE_armature_bone_flag_test_recursive(const Bone *bone, int flag)
Definition: armature.c:665
void BKE_pchan_to_mat4(const bPoseChannel *pchan, float r_chanmat[4][4])
Definition: armature.c:2626
bool BKE_pose_minmax(Object *ob, float r_min[3], float r_max[3], bool use_hidden, bool use_select)
Definition: armature.c:2865
static void pose_proxy_sync(Object *ob, Object *from, int layer_protected)
Definition: armature.c:2288
static void copy_bonechildren(Bone *bone_dst, const Bone *bone_src, const Bone *bone_src_act, Bone **r_bone_dst_act, const int flag)
Definition: armature.c:396
void BKE_armature_copy_bone_transforms(bArmature *armature_dst, const bArmature *armature_src)
Definition: armature.c:464
static void armature_blend_write(BlendWriter *writer, ID *id, const void *id_address)
Definition: armature.c:212
bPoseChannel * BKE_armature_splineik_solver_find_root(bPoseChannel *pchan, bSplineIKConstraint *data)
Definition: armature.c:2931
static void armature_foreach_id_editbone(EditBone *edit_bone, LibraryForeachIDData *data)
Definition: armature.c:172
static int rebuild_pose_bone(bPose *pose, Bone *bone, bPoseChannel *parchan, int counter, Bone **r_last_visited_bone_p)
Definition: armature.c:2444
void BKE_rotMode_change_values(float quat[4], float eul[3], float axis[3], float *angle, short oldMode, short newMode)
Definition: armature.c:1984
static void copy_bonechildren_custom_handles(Bone *bone_dst, bArmature *arm_dst)
Definition: armature.c:424
void BKE_bone_parent_transform_clear(struct BoneParentTransform *bpt)
Definition: armature.c:1789
void BKE_pchan_bbone_segments_cache_copy(bPoseChannel *pchan, bPoseChannel *pchan_from)
Definition: armature.c:1477
void BKE_armature_mat_pose_to_delta(float delta_mat[4][4], float pose_mat[4][4], float arm_mat[4][4])
Definition: armature.c:1963
static void lib_link_bones(BlendLibReader *reader, Bone *bone)
Definition: armature.c:277
BoundBox * BKE_armature_boundbox_get(Object *ob)
Definition: armature.c:2858
void BKE_bone_parent_transform_calc_from_pchan(const bPoseChannel *pchan, BoneParentTransform *r_bpt)
Definition: armature.c:1609
static void armature_refresh_layer_used_recursive(bArmature *arm, ListBase *bones)
Definition: armature.c:682
int BKE_pchan_bbone_spline_compute(BBoneSplineParameters *param, const bool for_deform, Mat4 *result_array)
Definition: armature.c:1306
void BKE_pchan_mat3_to_rot(bPoseChannel *pchan, const float mat[3][3], bool use_compat)
Definition: armature.c:1898
void mat3_to_vec_roll(const float mat[3][3], float r_vec[3], float *r_roll)
Definition: armature.c:2059
static void armature_blend_read_lib(BlendLibReader *reader, ID *id)
Definition: armature.c:286
static void armature_bone_from_name_insert_recursive(GHash *bone_hash, ListBase *lb)
Definition: armature.c:622
static void armature_free_data(struct ID *id)
Definition: armature.c:147
void BKE_armature_transform(bArmature *arm, const float mat[4][4], const bool do_props)
Definition: armature.c:567
void BKE_pchan_bbone_segments_cache_compute(bPoseChannel *pchan)
Definition: armature.c:1425
void BKE_armature_editbonelist_free(ListBase *lb, const bool do_id_user)
Definition: armature.c:385
static CLG_LogRef LOG
Definition: armature.c:72
void BKE_bone_parent_transform_calc_from_matrices(int bone_flag, int inherit_scale_mode, const float offs_bone[4][4], const float parent_arm_mat[4][4], const float parent_pose_mat[4][4], BoneParentTransform *r_bpt)
Definition: armature.c:1644
void BKE_pchan_bbone_handles_get(bPoseChannel *pchan, bPoseChannel **r_prev, bPoseChannel **r_next)
Definition: armature.c:921
static void armature_foreach_id(ID *id, LibraryForeachIDData *data)
Definition: armature.c:178
void BKE_bone_offset_matrix_get(const Bone *bone, float offs_bone[4][4])
Definition: armature.c:1577
static void armature_init_data(ID *id)
Definition: armature.c:92
void BKE_armature_bone_hash_free(bArmature *arm)
Definition: armature.c:651
void BKE_armature_where_is(bArmature *arm)
Definition: armature.c:2270
static void direct_link_bones(BlendDataReader *reader, Bone *bone)
Definition: armature.c:237
static Bone * get_named_bone_bonechildren(ListBase *lb, const char *name)
Definition: armature.c:587
void BKE_armature_mat_world_to_pose(Object *ob, const float inmat[4][4], float outmat[4][4])
Definition: armature.c:1536
void BKE_pchan_bbone_spline_setup(bPoseChannel *pchan, const bool rest, const bool for_deform, Mat4 *result_array)
Definition: armature.c:1134
void BKE_bone_parent_transform_apply(const struct BoneParentTransform *bpt, const float inmat[4][4], float outmat[4][4])
Definition: armature.c:1812
static GHash * armature_bone_from_name_map(bArmature *arm)
Definition: armature.c:636
static void armature_blend_read_data(BlendDataReader *reader, ID *id)
Definition: armature.c:255
bPoseChannel * BKE_armature_ik_solver_find_root(bPoseChannel *pchan, bKinematicConstraint *data)
Definition: armature.c:2910
static void evaluate_cubic_bezier(const float control[4][3], float t, float r_pos[3], float r_tangent[3])
Definition: armature.c:902
void BKE_pose_remap_bone_pointers(bArmature *armature, bPose *pose)
Definition: armature.c:2498
void BKE_armature_loc_world_to_pose(Object *ob, const float inloc[3], float outloc[3])
Definition: armature.c:1555
static void armature_blend_read_expand(BlendExpander *expander, ID *id)
Definition: armature.c:303
void BKE_pose_where_is_bone(struct Depsgraph *depsgraph, Scene *scene, Object *ob, bPoseChannel *pchan, float ctime, bool do_extra)
Definition: armature.c:2673
void BKE_pchan_bbone_handles_compute(const BBoneSplineParameters *param, float h1[3], float *r_roll1, float h2[3], float *r_roll2, bool ease, bool offsets)
Definition: armature.c:1147
void BKE_pchan_bbone_spline_params_get(struct bPoseChannel *pchan, const bool rest, struct BBoneSplineParameters *param)
Definition: armature.c:948
void vec_roll_to_mat3_normalized(const float nor[3], const float roll, float r_mat[3][3])
Definition: armature.c:2155
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
SIMD_FORCE_INLINE btScalar length(const btQuaternion &q)
Return the length of a quaternion.
Definition: btQuaternion.h:895
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition: btVector3.h:356
static char * basename(char *string)
Definition: datatoc.c:33
StackEntry * from
Scene scene
const Depsgraph * depsgraph
#define rot(x, k)
uint pos
uint nor
#define GS(x)
Definition: iris.c:241
#define atan2f(x, y)
#define floorf(x)
#define fabsf(x)
#define sqrtf(x)
void *(* MEM_malloc_arrayN)(size_t len, size_t size, const char *str)
Definition: mallocn.c:48
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
static ulong * next
static void error(const char *str)
Definition: meshlaplacian.c:65
static unsigned a[3]
Definition: RandGen.cpp:92
static double epsilon
#define min(a, b)
Definition: sort.c:51
float loc_mat[4][4]
Definition: BKE_armature.h:247
float rotscale_mat[4][4]
Definition: BKE_armature.h:246
float curve_out_y
float ease1
float roll
float zwidth
float curve_in_y
struct Bone * parent
struct Bone * bbone_prev
float roll1
float arm_head[3]
float roll2
char name[64]
float xwidth
float tail[3]
float arm_tail[3]
struct Bone * bbone_next
char inherit_scale_mode
char bbone_prev_type
float scale_in_x
float scale_in_y
short segments
float curve_in_x
float size[3]
IDProperty * prop
float length
float scale_out_x
float ease2
float arm_mat[4][4]
float bone_mat[3][3]
float head[3]
float scale_out_y
char bbone_next_type
struct Bone * next
float curve_out_x
ListBase childbase
float arm_roll
struct IDProperty * prop
Definition: BKE_armature.h:51
short id_code
Definition: BKE_idtype.h:120
Definition: DNA_ID.h:273
int tag
Definition: DNA_ID.h:292
int us
Definition: DNA_ID.h:293
char name[66]
Definition: DNA_ID.h:283
void * first
Definition: DNA_listBase.h:47
Definition: BKE_main.h:116
float mat[4][4]
Definition: BKE_armature.h:280
struct BoundBox * bb
struct bPose * pose
struct Object * proxy
float imat[4][4]
Object_Runtime runtime
float obmat[4][4]
void * data
struct AnimData * adt
unsigned int layer_used
ListBase bonebase
struct GHash * bonehash
struct EditBone * act_edbone
unsigned int layer_protected
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
struct Mat4 * bbone_deform_mats
struct DualQuat * bbone_dual_quats
struct Mat4 * bbone_pose_mats
struct Mat4 * bbone_rest_mats
ListBase constraints
IDProperty * prop
struct Bone * bone
struct bPoseChannel * parent
struct bPoseChannel * custom_tx
bMotionPath * mpath
float pose_head[3]
float chan_mat[4][4]
struct bPoseChannel * bbone_next
float pose_tail[3]
struct Object * custom
struct bPoseChannel * prev
struct bPoseChannel * next
struct bPoseChannel_Runtime runtime
struct bPoseChannel * bbone_prev
float pose_mat[4][4]
struct bPoseChannel * child
ListBase chanbase
ListBase agroups
short flag
bAnimVizSettings avs
int active_group
float cyclic_offset[3]
static int blend(const Tex *tex, const float texvec[3], TexResult *texres)
float max
uint len