Blender V5.3
blenkernel/intern/armature.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cctype>
10#include <cfloat>
11#include <cmath>
12#include <cstdlib>
13#include <cstring>
14#include <limits>
15#include <optional>
16
17#include <fmt/format.h>
18
19#include "BLI_alloca.hh"
20#include "BLI_assert.hh"
21#include "BLI_bounds.hh"
22#include "BLI_ghash.hh"
23#include "BLI_listbase.hh"
24#include "BLI_math_geom_c.hh"
25#include "BLI_math_matrix.hh"
26#include "BLI_math_matrix_c.hh"
28#include "BLI_math_vector_c.hh"
29#include "BLI_span.hh"
30#include "BLI_string_utf8.hh"
31#include "BLI_utildefines.hh"
32#include "BLT_translation.hh"
33
34#include "DNA_action_types.h"
35#include "DNA_armature_types.h"
37#include "DNA_listBase.h"
38#include "DNA_object_types.h"
39#include "DNA_scene_types.h"
40
41#include "RNA_prototypes.hh"
42
43#include "BKE_action.hh"
44#include "BKE_anim_data.hh"
46#include "BKE_animsys.hh"
47#include "BKE_armature.hh"
48#include "BKE_constraint.h"
49#include "BKE_curve.hh"
50#include "BKE_idprop.hh"
51#include "BKE_idtype.hh"
52#include "BKE_lib_id.hh"
53#include "BKE_lib_query.hh"
54#include "BKE_main.hh"
55#include "BKE_object.hh"
56#include "BKE_pose.hh"
57#include "BKE_scene.hh"
58
59#include "ANIM_armature.hh"
61
64
65#include "MEM_guardedalloc.h"
66
67#include "BIK_api.h"
68
69#include "BLO_read_write.hh"
70
71#include "CLG_log.h"
72
73namespace blender {
74
75static CLG_LogRef LOG = {"bke.armature"};
76
77/* -------------------------------------------------------------------- */
80
81static void copy_bonechildren(Bone *bone_dst,
82 const Bone *bone_src,
83 const Bone *bone_src_act,
84 Bone **r_bone_dst_act,
85 const int flag);
86
87static void copy_bonechildren_custom_handles(Bone *bone_dst, bArmature *arm_dst);
88
89static void rebuild_bone_array(bArmature &armature);
90
92
93/* -------------------------------------------------------------------- */
96
97static void armature_init_data(ID *id)
98{
99 bArmature *armature = id_cast<bArmature *>(id);
100 INIT_DEFAULT_STRUCT_AFTER(armature, id);
101 armature->runtime = MEM_new<bke::bArmature_Runtime>(__func__);
102
103 /* Initialize the counter at something that's unique to this ID. That way swapping out Armatures
104 * will force the Pose Object to reconstruct its bone indices. If the counter would start at 0
105 * for all data-blocks, detecting such swaps would require more than just this counter. */
106 armature->runtime->bones_generation_count = uint64_t(id->session_uid) << 32;
107}
108
118static void copy_bone_collection(bArmature *armature_dst,
119 BoneCollection *&bcoll_dst,
120 const BoneCollection *bcoll_src,
121 const int lib_id_flag)
122{
123 bcoll_dst = MEM_dupalloc(bcoll_src);
124
125 /* ID properties. */
126 if (bcoll_dst->prop) {
127 bcoll_dst->prop = IDP_CopyProperty_ex(bcoll_dst->prop, lib_id_flag);
128 }
129 if (bcoll_dst->system_properties) {
130 bcoll_dst->system_properties = IDP_CopyProperty_ex(bcoll_dst->system_properties, lib_id_flag);
131 }
132
133 /* Bone references. */
134 BLI_duplicatelist(&bcoll_dst->bones, &bcoll_dst->bones);
135 for (BoneCollectionMember &member : bcoll_dst->bones) {
136 member.bone = BKE_armature_find_bone_name(armature_dst, member.bone->name);
137 }
138}
139
150static void armature_copy_data(Main * /*bmain*/,
151 std::optional<Library *> /*owner_library*/,
152 ID *id_dst,
153 const ID *id_src,
154 const int flag)
155{
156 bArmature *armature_dst = id_cast<bArmature *>(id_dst);
157 const bArmature *armature_src = id_cast<const bArmature *>(id_src);
158 armature_dst->runtime = MEM_new<bke::bArmature_Runtime>(__func__);
159
160 Bone *bone_src, *bone_dst;
161 Bone *bone_dst_act = nullptr;
162
163 /* We never handle user-count here for own data. */
164 const int flag_subdata = flag | LIB_ID_CREATE_NO_USER_REFCOUNT;
165
166 armature_dst->bonehash = nullptr;
167
168 BLI_duplicatelist(&armature_dst->bonebase, &armature_src->bonebase);
169
170 /* Duplicate the children's lists. */
171 bone_dst = armature_dst->bonebase.first();
172 for (bone_src = armature_src->bonebase.first(); bone_src; bone_src = bone_src->next) {
173 bone_dst->parent = nullptr;
174 copy_bonechildren(bone_dst, bone_src, armature_src->act_bone, &bone_dst_act, flag_subdata);
175 bone_dst = bone_dst->next;
176 }
177
178 armature_dst->act_bone = bone_dst_act;
179
180 /* Build the flat bone array. */
182 /* For copy-on-evaluate copies, start with the same generation counter as the Armature we're
183 * copying from. Since this is an exact duplicate, bone indices should remain valid and thus
184 * pose objects won't need to adjust. */
185 rebuild_bone_array(*armature_dst);
186 armature_dst->runtime->bones_generation_count = armature_src->runtime->bones_generation_count;
187 }
188 else {
189 /* Other copies need their own counter value, to ensure that swapping between the original and
190 * the copy is detected even after they have been edited an equal number of times. */
191 armature_dst->runtime->bones_generation_count = uint64_t(id_dst->session_uid) << 32;
192 rebuild_bone_array(*armature_dst);
193 }
194
195 BKE_armature_bone_hash_make(armature_dst);
196
197 /* Fix custom handle references. */
198 for (bone_dst = armature_dst->bonebase.first(); bone_dst; bone_dst = bone_dst->next) {
199 copy_bonechildren_custom_handles(bone_dst, armature_dst);
200 }
201
202 armature_dst->edbo = nullptr;
203 armature_dst->act_edbone = nullptr;
204
205 /* Duplicate bone collections & assignments. */
206 if (armature_src->collection_array) {
207 armature_dst->collection_array = static_cast<BoneCollection **>(
208 MEM_dupalloc(armature_src->collection_array));
209 armature_dst->collection_array_num = armature_src->collection_array_num;
210 for (int i = 0; i < armature_src->collection_array_num; i++) {
211 copy_bone_collection(armature_dst,
212 armature_dst->collection_array[i],
213 armature_src->collection_array[i],
214 flag);
215 }
216 }
217 else {
218 armature_dst->collection_array = nullptr;
219 armature_dst->collection_array_num = 0;
220 }
221
223 armature_src->runtime->active_collection_index);
224 ANIM_armature_runtime_refresh(armature_dst);
225}
226
228static void armature_free_data(ID *id)
229{
230 bArmature *armature = id_cast<bArmature *>(id);
232 MEM_delete(armature->runtime);
233
234 /* Free all BoneCollectionMembership objects. */
235 if (armature->collection_array) {
236 for (BoneCollection *bcoll : armature->collections_span()) {
237 bcoll->bones.free_no_destruct();
238 ANIM_bonecoll_free(bcoll, false);
239 }
240 MEM_delete(armature->collection_array);
241 }
242 armature->collection_array = nullptr;
243 armature->collection_array_num = 0;
244
246 BKE_armature_bonelist_free(&armature->bonebase, false);
247
248 /* free editmode data */
249 if (armature->edbo) {
250 BKE_armature_editbonelist_free(armature->edbo, false);
251 MEM_delete(armature->edbo);
252 armature->edbo = nullptr;
253 }
254}
255
257{
260 BKE_lib_query_idpropertiesForeachIDLink_callback(prop, data);
261 }));
263 data,
265 BKE_lib_query_idpropertiesForeachIDLink_callback(prop, data);
266 }));
267
268 for (Bone &curbone : bone->childbase) {
270 }
271}
272
274{
277 BKE_lib_query_idpropertiesForeachIDLink_callback(prop, data);
278 }));
280 data,
282 edit_bone->system_properties, IDP_TYPE_FILTER_ID, [&](IDProperty *prop) {
283 BKE_lib_query_idpropertiesForeachIDLink_callback(prop, data);
284 }));
285}
286
288{
291 BKE_lib_query_idpropertiesForeachIDLink_callback(prop, data);
292 }));
294 data,
296 BKE_lib_query_idpropertiesForeachIDLink_callback(prop, data);
297 }));
298}
299
301{
302 bArmature *arm = id_cast<bArmature *>(id);
303 for (Bone &bone : arm->bonebase) {
305 }
306
307 if (arm->edbo != nullptr) {
308 for (EditBone &edit_bone : *arm->edbo) {
311 }
312 }
313
314 for (BoneCollection *bcoll : arm->collections_span()) {
317 }
318}
319
320static void write_bone(BlendWriter *writer, Bone *bone)
321{
322 /* PATCH for upward compatibility after 2.37+ armature recode */
323 bone->size[0] = bone->size[1] = bone->size[2] = 1.0f;
324
325 /* Write this bone, except for its runtime data. */
326 const Bone_Runtime runtime_backup = bone->runtime;
327 bone->runtime = Bone_Runtime{};
328 writer->write_struct(bone);
329 bone->runtime = runtime_backup;
330
331 /* Write ID Properties -- and copy this comment EXACTLY for easy finding
332 * of library blocks that implement this. */
333 if (bone->prop) {
334 IDP_BlendWrite(writer, bone->prop);
335 }
336 if (bone->system_properties) {
337 IDP_BlendWrite(writer, bone->system_properties);
338 }
339
340 /* Write Children */
341 for (Bone &cbone : bone->childbase) {
342 write_bone(writer, &cbone);
343 }
344}
345
347{
348 /* Write this bone collection. */
349 writer->write_struct(bcoll);
350
351 /* Write ID Properties -- and copy this comment EXACTLY for easy finding
352 * of library blocks that implement this. */
353 if (bcoll->prop) {
354 IDP_BlendWrite(writer, bcoll->prop);
355 }
356 if (bcoll->system_properties) {
357 IDP_BlendWrite(writer, bcoll->system_properties);
358 }
359
360 writer->write_struct_list(&bcoll->bones);
361}
362
363static void armature_blend_write(BlendWriter *writer, ID *id, const void *id_address)
364{
365 bArmature *arm = id_cast<bArmature *>(id);
366
367 /* Clean up, important in undo case to reduce false detection of changed datablocks. */
368 arm->bonehash = nullptr;
369 arm->edbo = nullptr;
370 /* Must always be cleared (armatures don't have their own edit-data). */
371 arm->needs_flush_to_id = 0;
372 arm->act_edbone = nullptr;
373
374 bke::bArmature_Runtime *runtime_backup = arm->runtime;
375 arm->runtime = nullptr;
376
377 /* Convert BoneCollections over to a listbase for writing. */
378 BoneCollection **collection_array_backup = arm->collection_array;
379 if (arm->collection_array_num > 0) {
380 for (int i = 0; i < arm->collection_array_num - 1; i++) {
381 arm->collection_array[i]->next = arm->collection_array[i + 1];
382 arm->collection_array[i + 1]->prev = arm->collection_array[i];
383 }
384 arm->collections_legacy.first_ = arm->collection_array[0];
385 arm->collections_legacy.last_ = arm->collection_array[arm->collection_array_num - 1];
386 arm->collection_array = nullptr;
387 }
388
389 writer->write_id_struct(id_address, arm);
390 BKE_id_blend_write(writer, &arm->id);
391
392 /* Direct data */
393 for (Bone &bone : arm->bonebase) {
394 write_bone(writer, &bone);
395 }
396
397 for (BoneCollection &bcoll : arm->collections_legacy) {
398 write_bone_collection(writer, &bcoll);
399 }
400
401 /* Restore the BoneCollection array and clear the listbase. */
402 arm->collection_array = collection_array_backup;
403 for (int i = 0; i < arm->collection_array_num - 1; i++) {
404 arm->collection_array[i]->next = nullptr;
405 arm->collection_array[i + 1]->prev = nullptr;
406 }
407 arm->collections_legacy.clear_no_delete();
408
409 arm->runtime = runtime_backup;
410}
411
412static void direct_link_bones(BlendDataReader *reader, Bone *bone, bool *all_bones_are_named)
413{
414 if (bone->name[0] == '\0') {
415 *all_bones_are_named = false;
416 }
417 BLO_read_struct(reader, Bone, &bone->parent);
418
419 BLO_read_struct(reader, IDProperty, &bone->prop);
420 IDP_BlendDataRead(reader, &bone->prop);
422 IDP_BlendDataRead(reader, &bone->system_properties);
423
424 BLO_read_struct(reader, Bone, &bone->bbone_next);
425 BLO_read_struct(reader, Bone, &bone->bbone_prev);
426
428
429 BLO_read_struct_list(reader, Bone, &bone->childbase);
430
431 for (Bone &child : bone->childbase) {
432 direct_link_bones(reader, &child, all_bones_are_named);
433 }
434
435 bone->runtime = Bone_Runtime{};
436}
437
439{
440 BLO_read_struct(reader, IDProperty, &bcoll->prop);
441 IDP_BlendDataRead(reader, &bcoll->prop);
443 IDP_BlendDataRead(reader, &bcoll->system_properties);
444
446 for (BoneCollectionMember &member : bcoll->bones) {
447 BLO_read_struct(reader, Bone, &member.bone);
448 }
449}
450
452{
453 /* Read as listbase, but convert to an array on the armature. */
455 arm->collection_array_num = arm->collections_legacy.count();
457 size_t(arm->collection_array_num), __func__);
458 {
459
460 int min_child_index = 0;
461 for (const auto [i, bcoll] : arm->collections_legacy.enumerate()) {
462 arm->collection_array[i] = &bcoll;
463
464 if (bcoll.child_index > 0) {
465 min_child_index = min_ii(min_child_index, bcoll.child_index);
466 }
467 }
468
469 if (arm->collection_root_count == 0 && arm->collection_array_num > 0) {
470 /* There cannot be zero roots when there are any bone collections. This means the root count
471 * likely got lost for some reason, and should be reconstructed to avoid data corruption when
472 * modifying the array. */
473 if (min_child_index == 0) {
474 /* None of the bone collections had any children, so all are roots. */
476 }
477 else {
478 arm->collection_root_count = min_child_index;
479 }
480 }
481 }
482
483 /* We don't need the listbase or prev/next pointers because the
484 * collections are stored in an array. */
485 for (int i = 0; i < arm->collection_array_num - 1; i++) {
486 arm->collection_array[i]->next = nullptr;
487 arm->collection_array[i + 1]->prev = nullptr;
488 }
489 arm->collections_legacy.clear_no_delete();
490
491 /* Bone collections added via an override can be edited, but ones that already exist in another
492 * blend file (so on the linked Armature) should not be touched. */
493 const bool reset_bcoll_override_flag = ID_IS_LINKED(&arm->id);
494 for (BoneCollection *bcoll : arm->collections_span()) {
495 direct_link_bone_collection(reader, bcoll);
496 if (reset_bcoll_override_flag) {
497 /* The linked Armature may have overrides in the library file already, and
498 * those should *not* be editable here. */
500 }
501 }
502}
503
508static void fix_empty_bone_names(bArmature &armature)
509{
510 constexpr const char *name_prefix = "unnamed.";
511 constexpr const char *format_string = "unnamed.{:0>3}";
512
513 Set<StringRefNull> potential_duplicates;
514 Vector<Bone *> no_name_bones;
515 BKE_armature_foreach_bone(armature, [&](const int /* bone_index */, const Bone &bone) {
516 if (bone.name[0] == '\0') {
517 no_name_bones.append(const_cast<Bone *>(&bone));
518 }
519 else {
520 if (StringRefNull(bone.name).startswith(name_prefix)) {
521 potential_duplicates.add(bone.name);
522 }
523 }
524 });
525 /* Custom unique bone name logic because the actual logic is editor code and relies on the
526 * hashmap already built. */
527 int unique_index = 0;
528 std::string bone_name;
529 for (Bone *bone : no_name_bones) {
530 bone_name = fmt::format(format_string, unique_index);
531 while (potential_duplicates.contains(bone_name)) {
532 unique_index++;
533 bone_name = fmt::format(format_string, unique_index);
534 }
535 STRNCPY_UTF8(bone->name, bone_name.c_str());
536 unique_index++;
537 }
538}
539
541{
542 bArmature *arm = id_cast<bArmature *>(id);
543 BLO_read_struct_list(reader, Bone, &arm->bonebase);
544 arm->bonehash = nullptr;
545 arm->edbo = nullptr;
546 /* Must always be cleared (armatures don't have their own edit-data). */
547 arm->needs_flush_to_id = 0;
548 bool all_bones_are_named = true;
549 for (Bone &bone : arm->bonebase) {
550 direct_link_bones(reader, &bone, &all_bones_are_named);
551 }
552 if (!all_bones_are_named) {
553 CLOG_WARN(&LOG, "Found bones with empty names. Fixing automatically.\n");
555 }
556
557 read_bone_collections(reader, arm);
558
559 BLO_read_struct(reader, Bone, &arm->act_bone);
560 arm->act_edbone = nullptr;
561
563
564 arm->runtime = MEM_new<bke::bArmature_Runtime>(__func__);
565
566 /* Same approach as in armature_init_data(). */
567 BLI_assert(id->session_uid != MAIN_ID_SESSION_UID_UNSET);
568 arm->runtime->bones_generation_count = uint64_t(id->session_uid) << 32;
569
571}
572
573static void armature_undo_preserve(BlendLibReader * /*reader*/, ID *id_new, ID *id_old)
574{
575 bArmature *arm_new = id_cast<bArmature *>(id_new);
576 bArmature *arm_old = id_cast<bArmature *>(id_old);
577
578 animrig::bonecolls_copy_expanded_flag(arm_new->collections_span(), arm_old->collections_span());
579}
580
582 .id_code = bArmature::id_type,
583 .id_filter = FILTER_ID_AR,
584 /* IDProps of armature bones can use any type of ID. */
585 .dependencies_id_types = FILTER_ID_ALL,
586 .main_listbase_index = INDEX_ID_AR,
587 .struct_size = sizeof(bArmature),
588 .name = "Armature",
589 .name_plural = N_("armatures"),
590 .translation_context = BLT_I18NCONTEXT_ID_ARMATURE,
592 .asset_type_info = nullptr,
593
594 .init_data = armature_init_data,
595 .copy_data = armature_copy_data,
596 .free_data = armature_free_data,
597 .make_local = nullptr,
598 .foreach_id = armature_foreach_id,
599 .foreach_cache = nullptr,
600 .foreach_path = nullptr,
601 .foreach_working_space_color = nullptr,
602 .foreach_asset_weak_reference = nullptr,
603 .owner_pointer_get = nullptr,
604
605 .blend_write = armature_blend_write,
606 .blend_read_data = armature_blend_read_data,
607 .blend_read_after_liblink = nullptr,
608
609 .blend_read_undo_preserve = armature_undo_preserve,
610
611 .lib_override_apply_post = nullptr,
612};
613
615
616/* -------------------------------------------------------------------- */
619
620bArmature *BKE_armature_add(Main *bmain, const char *name)
621{
622 bArmature *arm;
623
624 arm = BKE_id_new<bArmature>(bmain, name);
625 return arm;
626}
627
629{
630 if (ob->type == OB_ARMATURE) {
631 return id_cast<bArmature *>(ob->data);
632 }
633 return nullptr;
634}
635
636int BKE_armature_bonelist_count(const ListBaseT<Bone> *lb)
637{
638 int i = 0;
639 for (Bone &bone : *lb) {
640 i += 1 + BKE_armature_bonelist_count(&bone.childbase);
641 }
642
643 return i;
644}
645
646void BKE_armature_bonelist_free(ListBaseT<Bone> *lb, const bool do_id_user)
647{
648 for (Bone &bone : *lb) {
649 if (bone.prop) {
650 IDP_FreeProperty_ex(bone.prop, do_id_user);
651 }
652 if (bone.system_properties) {
653 IDP_FreeProperty_ex(bone.system_properties, do_id_user);
654 }
655 bone.runtime.collections.free_no_destruct();
656 BKE_armature_bonelist_free(&bone.childbase, do_id_user);
657 }
658
659 lb->free_no_destruct();
660}
661
662void BKE_armature_editbonelist_free(ListBaseT<EditBone> *lb, const bool do_id_user)
663{
664 for (EditBone &edit_bone : lb->items_mutable()) {
665 if (edit_bone.prop) {
666 IDP_FreeProperty_ex(edit_bone.prop, do_id_user);
667 }
668 if (edit_bone.system_properties) {
669 IDP_FreeProperty_ex(edit_bone.system_properties, do_id_user);
670 }
671 BLI_remlink_safe(lb, &edit_bone);
672 MEM_delete(&edit_bone);
673 }
674}
675
676static void copy_bonechildren(Bone *bone_dst,
677 const Bone *bone_src,
678 const Bone *bone_src_act,
679 Bone **r_bone_dst_act,
680 const int flag)
681{
682 Bone *bone_src_child, *bone_dst_child;
683
684 if (bone_src == bone_src_act) {
685 *r_bone_dst_act = bone_dst;
686 }
687
688 if (bone_src->prop) {
689 bone_dst->prop = IDP_CopyProperty_ex(bone_src->prop, flag);
690 }
691 if (bone_src->system_properties) {
692 bone_dst->system_properties = IDP_CopyProperty_ex(bone_src->system_properties, flag);
693 }
694
695 /* Clear the runtime cache of the collection relations, these will be
696 * reconstructed after the entire armature duplication is done. Don't free,
697 * just clear, as these pointers refer to the original and not the copy. */
698 bone_dst->runtime.collections.clear_no_delete();
699
700 /* Copy this bone's list */
701 BLI_duplicatelist(&bone_dst->childbase, &bone_src->childbase);
702
703 /* For each child in the list, update its children */
704 for (bone_src_child = bone_src->childbase.first(), bone_dst_child = bone_dst->childbase.first();
705 bone_src_child;
706 bone_src_child = bone_src_child->next, bone_dst_child = bone_dst_child->next)
707 {
708 bone_dst_child->parent = bone_dst;
709 copy_bonechildren(bone_dst_child, bone_src_child, bone_src_act, r_bone_dst_act, flag);
710 }
711}
712
713static void copy_bonechildren_custom_handles(Bone *bone_dst, bArmature *arm_dst)
714{
715 Bone *bone_dst_child;
716
717 if (bone_dst->bbone_prev) {
718 bone_dst->bbone_prev = BKE_armature_find_bone_name(arm_dst, bone_dst->bbone_prev->name);
719 }
720 if (bone_dst->bbone_next) {
721 bone_dst->bbone_next = BKE_armature_find_bone_name(arm_dst, bone_dst->bbone_next->name);
722 }
723
724 for (bone_dst_child = bone_dst->childbase.first(); bone_dst_child;
725 bone_dst_child = bone_dst_child->next)
726 {
727 copy_bonechildren_custom_handles(bone_dst_child, arm_dst);
728 }
729}
730
732
733/* -------------------------------------------------------------------- */
736
737static void copy_bone_transform(Bone *bone_dst, const Bone *bone_src)
738{
739 bone_dst->roll = bone_src->roll;
740
741 copy_v3_v3(bone_dst->head, bone_src->head);
742 copy_v3_v3(bone_dst->tail, bone_src->tail);
743
744 copy_m3_m3(bone_dst->bone_mat, bone_src->bone_mat);
745
746 copy_v3_v3(bone_dst->arm_head, bone_src->arm_head);
747 copy_v3_v3(bone_dst->arm_tail, bone_src->arm_tail);
748
749 copy_m4_m4(bone_dst->arm_mat, bone_src->arm_mat);
750
751 bone_dst->arm_roll = bone_src->arm_roll;
752}
753
754void BKE_armature_copy_bone_transforms(bArmature *armature_dst, const bArmature *armature_src)
755{
756 Bone *bone_dst = armature_dst->bonebase.first();
757 const Bone *bone_src = armature_src->bonebase.first();
758 while (bone_dst != nullptr) {
759 BLI_assert(bone_src != nullptr);
760 copy_bone_transform(bone_dst, bone_src);
761 bone_dst = bone_dst->next;
762 bone_src = bone_src->next;
763 }
764}
765
767
768/* -------------------------------------------------------------------- */
773
775static void armature_transform_recurse(ListBaseT<Bone> *bonebase,
776 const float mat[4][4],
777 const bool do_props,
778 /* Cached from 'mat'. */
779 const float mat3[3][3],
780 const float scale,
781 /* Child bones. */
782 const Bone *bone_parent,
783 const float arm_mat_parent_inv[4][4])
784{
785 for (Bone &bone : *bonebase) {
786
787 /* Store the initial bone roll in a matrix, this is needed even for child bones
788 * so any change in head/tail doesn't cause the roll to change.
789 *
790 * Logic here is different to edit-mode because
791 * this is calculated in relative to the parent. */
792 float roll_mat3_pre[3][3];
793 {
794 float delta[3];
795 sub_v3_v3v3(delta, bone.tail, bone.head);
796 vec_roll_to_mat3(delta, bone.roll, roll_mat3_pre);
797 if (bone.parent == nullptr) {
798 mul_m3_m3m3(roll_mat3_pre, mat3, roll_mat3_pre);
799 }
800 }
801 /* Optional, use this for predictable results since the roll is re-calculated below anyway. */
802 bone.roll = 0.0f;
803
804 mul_m4_v3(mat, bone.arm_head);
805 mul_m4_v3(mat, bone.arm_tail);
806
807 /* Get the new head and tail */
808 if (bone_parent) {
809 sub_v3_v3v3(bone.head, bone.arm_head, bone_parent->arm_tail);
810 sub_v3_v3v3(bone.tail, bone.arm_tail, bone_parent->arm_tail);
811
812 mul_mat3_m4_v3(arm_mat_parent_inv, bone.head);
813 mul_mat3_m4_v3(arm_mat_parent_inv, bone.tail);
814 }
815 else {
816 copy_v3_v3(bone.head, bone.arm_head);
817 copy_v3_v3(bone.tail, bone.arm_tail);
818 }
819
820 /* Now the head/tail have been updated, set the roll back, matching 'roll_mat3_pre'. */
821 {
822 float roll_mat3_post[3][3], delta_mat3[3][3];
823 float delta[3];
824 sub_v3_v3v3(delta, bone.tail, bone.head);
825 vec_roll_to_mat3(delta, 0.0f, roll_mat3_post);
826 invert_m3(roll_mat3_post);
827 mul_m3_m3m3(delta_mat3, roll_mat3_post, roll_mat3_pre);
828 bone.roll = atan2f(delta_mat3[2][0], delta_mat3[2][2]);
829 }
830
831 BKE_armature_where_is_bone(&bone, bone_parent, false);
832
833 {
834 float arm_mat3[3][3];
835 copy_m3_m4(arm_mat3, bone.arm_mat);
836 mat3_to_vec_roll(arm_mat3, nullptr, &bone.arm_roll);
837 }
838
839 if (do_props) {
840 bone.rad_head *= scale;
841 bone.rad_tail *= scale;
842 bone.dist *= scale;
843
844 /* we could be smarter and scale by the matrix along the x & z axis */
845 bone.xwidth *= scale;
846 bone.zwidth *= scale;
847 }
848
849 if (!bone.childbase.is_empty()) {
850 float arm_mat_inv[4][4];
851 invert_m4_m4(arm_mat_inv, bone.arm_mat);
852 armature_transform_recurse(&bone.childbase, mat, do_props, mat3, scale, &bone, arm_mat_inv);
853 }
854 }
855}
856
857void BKE_armature_transform(bArmature *arm, const float mat[4][4], const bool do_props)
858{
859 /* Store the scale of the matrix here to use on envelopes. */
860 float scale = mat4_to_scale(mat);
861 float mat3[3][3];
862
863 copy_m3_m4(mat3, mat);
864 normalize_m3(mat3);
865
866 armature_transform_recurse(&arm->bonebase, mat, do_props, mat3, scale, nullptr, nullptr);
867}
868
870
871/* -------------------------------------------------------------------- */
876
877static Bone *get_named_bone_bonechildren(ListBaseT<Bone> *lb, const char *name)
878{
879 for (Bone &curBone : *lb) {
880 if (STREQ(curBone.name, name)) {
881 return &curBone;
882 }
883
884 Bone *rbone = get_named_bone_bonechildren(&curBone.childbase, name);
885 if (rbone) {
886 return rbone;
887 }
888 }
889
890 return nullptr;
891}
892
894{
895 if (!arm) {
896 return nullptr;
897 }
898
899 if (arm->bonehash) {
900 return static_cast<Bone *>(BLI_ghash_lookup(arm->bonehash, name));
901 }
902
903 return get_named_bone_bonechildren(&arm->bonebase, name);
904}
905
906static void armature_bone_from_name_insert_recursive(GHash *bone_hash, ListBaseT<Bone> *lb)
907{
908 for (Bone &bone : *lb) {
909 BLI_ghash_insert(bone_hash, bone.name, &bone);
910 armature_bone_from_name_insert_recursive(bone_hash, &bone.childbase);
911 }
912}
913
921{
922 const int bones_count = BKE_armature_bonelist_count(&arm->bonebase);
923 GHash *bone_hash = BLI_ghash_str_new_ex(__func__, bones_count);
925 return bone_hash;
926}
927
929{
930 if (!arm->bonehash) {
932 }
933}
934
936{
937 if (arm->bonehash) {
938 BLI_ghash_free(arm->bonehash, nullptr, nullptr);
939 arm->bonehash = nullptr;
940 }
941}
942
944
945/* -------------------------------------------------------------------- */
948
950{
951 if (bone->flag & flag) {
952 return true;
953 }
954 if (bone->parent) {
956 }
957 return false;
958}
959
961
962/* -------------------------------------------------------------------- */
965
967 char name[MAXBONENAME], int /*strip_number*/, short axis, float head, float tail)
968{
969 char basename[MAXBONENAME];
970 uint len = STRNCPY_UTF8_RLEN(basename, name);
971 if (len == 0) {
972 return false;
973 }
974
975 /* Figure out extension to append:
976 * - The extension to append is based upon the axis that we are working on.
977 * - If head happens to be on 0, then we must consider the tail position as well to decide
978 * which side the bone is on
979 * -> If tail is 0, then its bone is considered to be on axis, so no extension should be added
980 * -> Otherwise, extension is added from perspective of object based on which side tail goes to
981 * - If head is non-zero, extension is added from perspective of object based on side head is on
982 */
983 const char *extension = nullptr;
984 if (axis == 2) {
985 /* z-axis - vertical (top/bottom) */
986 if (IS_EQF(head, 0.0f)) {
987 if (tail < 0) {
988 extension = "Bot";
989 }
990 else if (tail > 0) {
991 extension = "Top";
992 }
993 }
994 else {
995 if (head < 0) {
996 extension = "Bot";
997 }
998 else {
999 extension = "Top";
1000 }
1001 }
1002 }
1003 else if (axis == 1) {
1004 /* y-axis - depth (front/back) */
1005 if (IS_EQF(head, 0.0f)) {
1006 if (tail < 0) {
1007 extension = "Fr";
1008 }
1009 else if (tail > 0) {
1010 extension = "Bk";
1011 }
1012 }
1013 else {
1014 if (head < 0) {
1015 extension = "Fr";
1016 }
1017 else {
1018 extension = "Bk";
1019 }
1020 }
1021 }
1022 else {
1023 /* x-axis - horizontal (left/right) */
1024 if (IS_EQF(head, 0.0f)) {
1025 if (tail < 0) {
1026 extension = "R";
1027 }
1028 else if (tail > 0) {
1029 extension = "L";
1030 }
1031 }
1032 else {
1033 if (head < 0) {
1034 extension = "R";
1035 /* XXX Shouldn't this be simple else, as for z and y axes? */
1036 }
1037 else if (head > 0) {
1038 extension = "L";
1039 }
1040 }
1041 }
1042
1043 /* Simple name truncation
1044 * - truncate if there is an extension and it wouldn't be able to fit
1045 * - otherwise, just append to end
1046 */
1047 if (extension) {
1048 bool changed = true;
1049
1050 while (changed) { /* remove extensions */
1051 changed = false;
1052 if (len > 2 && basename[len - 2] == '.') {
1053 if (ELEM(basename[len - 1], 'L', 'R')) { /* L R */
1054 basename[len - 2] = '\0';
1055 len -= 2;
1056 changed = true;
1057 }
1058 }
1059 else if (len > 3 && basename[len - 3] == '.') {
1060 if ((basename[len - 2] == 'F' && basename[len - 1] == 'r') || /* Fr */
1061 (basename[len - 2] == 'B' && basename[len - 1] == 'k')) /* Bk */
1062 {
1063 basename[len - 3] = '\0';
1064 len -= 3;
1065 changed = true;
1066 }
1067 }
1068 else if (len > 4 && basename[len - 4] == '.') {
1069 if ((basename[len - 3] == 'T' && basename[len - 2] == 'o' &&
1070 basename[len - 1] == 'p') || /* Top */
1071 (basename[len - 3] == 'B' && basename[len - 2] == 'o' &&
1072 basename[len - 1] == 't')) /* Bot */
1073 {
1074 basename[len - 4] = '\0';
1075 len -= 4;
1076 changed = true;
1077 }
1078 }
1079 }
1080
1081 /* Subtract 1 from #MAXBONENAME for the null byte. Add 1 to the extension for the '.' */
1082 const int basename_maxncpy = (MAXBONENAME - 1) - (1 + strlen(extension));
1083 BLI_str_utf8_truncate_at_size(basename, basename_maxncpy);
1084 BLI_snprintf_utf8(name, MAXBONENAME, "%s.%s", basename, extension);
1085
1086 return true;
1087 }
1088 return false;
1089}
1090
1092
1093/* -------------------------------------------------------------------- */
1096
1097/* Compute a set of bezier parameter values that produce approximately equally spaced points. */
1098static void equalize_cubic_bezier(const float control[4][3],
1099 int temp_segments,
1100 int final_segments,
1101 const float *segment_scales,
1102 float *r_t_points)
1103{
1104 float (*coords)[3] = static_cast<float (*)[3]>(BLI_array_alloca(coords, temp_segments + 1));
1105 float *pdist = static_cast<float *>(BLI_array_alloca(pdist, temp_segments + 1));
1106
1107 /* Compute the first pass of bezier point coordinates. */
1108 for (int i = 0; i < 3; i++) {
1109 BKE_curve_forward_diff_bezier(control[0][i],
1110 control[1][i],
1111 control[2][i],
1112 control[3][i],
1113 &coords[0][i],
1114 temp_segments,
1115 sizeof(*coords));
1116 }
1117
1118 /* Calculate the length of the polyline at each point. */
1119 pdist[0] = 0.0f;
1120
1121 for (int i = 0; i < temp_segments; i++) {
1122 pdist[i + 1] = pdist[i] + len_v3v3(coords[i], coords[i + 1]);
1123 }
1124
1125 /* Go over distances and calculate new parameter values. */
1126 float dist_step = pdist[temp_segments];
1127 float dist = 0, sum = 0;
1128
1129 for (int i = 0; i < final_segments; i++) {
1130 sum += segment_scales[i];
1131 }
1132
1133 dist_step /= sum;
1134
1135 r_t_points[0] = 0.0f;
1136
1137 for (int i = 1, nr = 1; i <= final_segments; i++) {
1138 dist += segment_scales[i - 1] * dist_step;
1139
1140 /* We're looking for location (distance) 'dist' in the array. */
1141 while ((nr < temp_segments) && (dist >= pdist[nr])) {
1142 nr++;
1143 }
1144
1145 float fac = (pdist[nr] - dist) / (pdist[nr] - pdist[nr - 1]);
1146
1147 r_t_points[i] = (nr - fac) / temp_segments;
1148 }
1149
1150 r_t_points[final_segments] = 1.0f;
1151}
1152
1153/* Evaluate bezier position and tangent at a specific parameter value
1154 * using the De Casteljau algorithm. */
1155static void evaluate_cubic_bezier(const float control[4][3],
1156 float t,
1157 float r_pos[3],
1158 float r_tangent[3])
1159{
1160 float layer1[3][3];
1161 interp_v3_v3v3(layer1[0], control[0], control[1], t);
1162 interp_v3_v3v3(layer1[1], control[1], control[2], t);
1163 interp_v3_v3v3(layer1[2], control[2], control[3], t);
1164
1165 float layer2[2][3];
1166 interp_v3_v3v3(layer2[0], layer1[0], layer1[1], t);
1167 interp_v3_v3v3(layer2[1], layer1[1], layer1[2], t);
1168
1169 sub_v3_v3v3(r_tangent, layer2[1], layer2[0]);
1170 madd_v3_v3v3fl(r_pos, layer2[0], r_tangent, t);
1171}
1172
1174 bPoseChannel **r_prev,
1175 bPoseChannel **r_next)
1176{
1177 if (pchanbone.bone->bbone_prev_type == BBONE_HANDLE_AUTO) {
1178 /* Use connected parent. */
1179 if (pchanbone.bone->flag & BONE_CONNECTED) {
1180 *r_prev = pchanbone.pchan->parent;
1181 }
1182 else {
1183 *r_prev = nullptr;
1184 }
1185 }
1186 else {
1187 /* Use the provided bone as prev - leave blank to eliminate this effect altogether. */
1188 *r_prev = pchanbone.pchan->bbone_prev;
1189 }
1190
1191 if (pchanbone.bone->bbone_next_type == BBONE_HANDLE_AUTO) {
1192 /* Use connected child. */
1193 *r_next = pchanbone.pchan->child;
1194 }
1195 else {
1196 /* Use the provided bone as next - leave blank to eliminate this effect altogether. */
1197 *r_next = pchanbone.pchan->bbone_next;
1198 }
1199}
1200
1202 const bArmature &armature,
1203 const bool rest,
1204 BBoneSplineParameters *param)
1205{
1206 const bPoseChannel *pchan = pchanbone.pchan;
1207 const Bone *bone = pchanbone.bone;
1208
1209 float imat[4][4], posemat[4][4], tmpmat[4][4];
1210 float delta[3];
1211
1212 memset(param, 0, sizeof(*param));
1213
1214 param->segments = pchanbone.bone->segments;
1215 param->length = pchanbone.bone->length;
1216
1217 if (!rest) {
1218 float scale[3];
1219
1220 /* Check if we need to take non-uniform bone scaling into account. */
1221 mat4_to_size(scale, pchan->pose_mat);
1222
1223 if (fabsf(scale[0] - scale[1]) > 1e-6f || fabsf(scale[1] - scale[2]) > 1e-6f) {
1224 param->do_scale = true;
1225 copy_v3_v3(param->scale, scale);
1226 }
1227 }
1228
1229 bPoseChannel *next, *prev;
1230 BKE_pchan_bbone_handles_get(pchanbone, &prev, &next);
1231 const Bone *next_bone = next ? next->bone_get(armature) : nullptr;
1232 const Bone *prev_bone = prev ? prev->bone_get(armature) : nullptr;
1233
1234 /* Find the handle points, since this is inside bone space, the
1235 * first point = (0, 0, 0)
1236 * last point = (0, length, 0) */
1237 if (rest) {
1238 invert_m4_m4(imat, pchanbone.bone->arm_mat);
1239 }
1240 else if (param->do_scale) {
1241 copy_m4_m4(posemat, pchan->pose_mat);
1242 normalize_m4(posemat);
1243 invert_m4_m4(imat, posemat);
1244 }
1245 else {
1246 invert_m4_m4(imat, pchan->pose_mat);
1247 }
1248
1249 float prev_scale[3], next_scale[3];
1250
1251 copy_v3_fl(prev_scale, 1.0f);
1252 copy_v3_fl(next_scale, 1.0f);
1253
1254 if (prev) {
1255 float h1[3];
1256 bool done = false;
1257 const Bone *bone = pchanbone.bone;
1258
1259 param->use_prev = true;
1260
1261 /* Transform previous point inside this bone space. */
1263 /* Use delta movement (from rest-pose),
1264 * and apply this relative to the current bone's head. */
1265 if (rest) {
1266 /* In rest-pose, arm_head == pose_head */
1267 zero_v3(param->prev_h);
1268 done = true;
1269 }
1270 else {
1271 sub_v3_v3v3(delta, prev->pose_head, prev_bone->arm_head);
1272 sub_v3_v3v3(h1, pchan->pose_head, delta);
1273 }
1274 }
1275 else if (bone->bbone_prev_type == BBONE_HANDLE_TANGENT) {
1276 /* Use bone direction by offsetting so that its tail meets current bone's head */
1277 if (rest) {
1278 sub_v3_v3v3(delta, prev_bone->arm_tail, prev_bone->arm_head);
1279 sub_v3_v3v3(h1, bone->arm_head, delta);
1280 }
1281 else {
1282 sub_v3_v3v3(delta, prev->pose_tail, prev->pose_head);
1283 sub_v3_v3v3(h1, pchan->pose_head, delta);
1284 }
1285 }
1286 else {
1287 /* Apply special handling for smoothly joining B-Bone chains */
1288 param->prev_bbone = (prev_bone->segments > 1);
1289
1290 /* Use bone head as absolute position. */
1291 copy_v3_v3(h1, rest ? prev_bone->arm_head : prev->pose_head);
1292 }
1293
1294 if (!done) {
1295 mul_v3_m4v3(param->prev_h, imat, h1);
1296 }
1297
1298 if (!param->prev_bbone) {
1299 /* Find the previous roll to interpolate. */
1300 mul_m4_m4m4(param->prev_mat, imat, rest ? prev_bone->arm_mat : prev->pose_mat);
1301
1302 /* Retrieve the local scale of the bone if necessary. */
1303 if ((bone->bbone_prev_flag & BBONE_HANDLE_SCALE_ANY) && !rest) {
1304 BKE_armature_mat_pose_to_bone({prev, prev_bone}, prev->pose_mat, tmpmat);
1305 mat4_to_size(prev_scale, tmpmat);
1306 }
1307 }
1308 }
1309
1310 if (next) {
1311 float h2[3];
1312 bool done = false;
1313
1314 param->use_next = true;
1315
1316 /* Transform next point inside this bone space. */
1318 /* Use delta movement (from rest-pose),
1319 * and apply this relative to the current bone's tail. */
1320 if (rest) {
1321 /* In rest-pose, arm_head == pose_head */
1322 copy_v3_fl3(param->next_h, 0.0f, param->length, 0.0);
1323 done = true;
1324 }
1325 else {
1326 sub_v3_v3v3(delta, next->pose_head, next_bone->arm_head);
1327 add_v3_v3v3(h2, pchan->pose_tail, delta);
1328 }
1329 }
1330 else if (bone->bbone_next_type == BBONE_HANDLE_TANGENT) {
1331 /* Use bone direction by offsetting so that its head meets current bone's tail */
1332 if (rest) {
1333 sub_v3_v3v3(delta, next_bone->arm_tail, next_bone->arm_head);
1334 add_v3_v3v3(h2, bone->arm_tail, delta);
1335 }
1336 else {
1337 sub_v3_v3v3(delta, next->pose_tail, next->pose_head);
1338 add_v3_v3v3(h2, pchan->pose_tail, delta);
1339 }
1340 }
1341 else {
1342 /* Apply special handling for smoothly joining B-Bone chains */
1343 param->next_bbone = (next_bone->segments > 1);
1344
1345 /* Use bone tail as absolute position. */
1346 copy_v3_v3(h2, rest ? next_bone->arm_tail : next->pose_tail);
1347 }
1348
1349 if (!done) {
1350 mul_v3_m4v3(param->next_h, imat, h2);
1351 }
1352
1353 /* Find the next roll to interpolate as well. */
1354 mul_m4_m4m4(param->next_mat, imat, rest ? next_bone->arm_mat : next->pose_mat);
1355
1356 /* Retrieve the local scale of the bone if necessary. */
1357 if ((bone->bbone_next_flag & BBONE_HANDLE_SCALE_ANY) && !rest) {
1358 BKE_armature_mat_pose_to_bone({next, next_bone}, next->pose_mat, tmpmat);
1359 mat4_to_size(next_scale, tmpmat);
1360 }
1361 }
1362
1363 /* Add effects from bbone properties over the top
1364 * - These properties allow users to hand-animate the
1365 * bone curve/shape, without having to resort to using
1366 * extra bones
1367 * - The "bone" level offsets are for defining the rest-pose
1368 * shape of the bone (e.g. for curved eyebrows for example).
1369 * -> In the viewport, it's needed to define what the rest pose
1370 * looks like
1371 * -> For "rest == 0", we also still need to have it present
1372 * so that we can "cancel out" this rest-pose when it comes
1373 * time to deform some geometry, it won't cause double transforms.
1374 * - The "pchan" level offsets are the ones that animators actually
1375 * end up animating
1376 */
1377 {
1378 param->ease1 = bone->ease1 + (!rest ? pchan->ease1 : 0.0f);
1379 param->ease2 = bone->ease2 + (!rest ? pchan->ease2 : 0.0f);
1380
1381 param->roll1 = bone->roll1 + (!rest ? pchan->roll1 : 0.0f);
1382 param->roll2 = bone->roll2 + (!rest ? pchan->roll2 : 0.0f);
1383
1385 if (prev) {
1386 if (prev_bone) {
1387 param->roll1 += prev_bone->roll2;
1388 }
1389
1390 if (!rest) {
1391 param->roll1 += prev->roll2;
1392 }
1393 }
1394 }
1395
1396 copy_v3_v3(param->scale_in, bone->scale_in);
1397 copy_v3_v3(param->scale_out, bone->scale_out);
1398
1399 if (!rest) {
1400 mul_v3_v3(param->scale_in, pchan->scale_in);
1401 mul_v3_v3(param->scale_out, pchan->scale_out);
1402 }
1403
1404 /* Extra curve x / z */
1405 param->curve_in_x = bone->curve_in_x + (!rest ? pchan->curve_in_x : 0.0f);
1406 param->curve_in_z = bone->curve_in_z + (!rest ? pchan->curve_in_z : 0.0f);
1407
1408 param->curve_out_x = bone->curve_out_x + (!rest ? pchan->curve_out_x : 0.0f);
1409 param->curve_out_z = bone->curve_out_z + (!rest ? pchan->curve_out_z : 0.0f);
1410
1411 if (bone->bbone_flag & BBONE_SCALE_EASING) {
1412 param->ease1 *= param->scale_in[1];
1413 param->curve_in_x *= param->scale_in[1];
1414 param->curve_in_z *= param->scale_in[1];
1415
1416 param->ease2 *= param->scale_out[1];
1417 param->curve_out_x *= param->scale_out[1];
1418 param->curve_out_z *= param->scale_out[1];
1419 }
1420
1421 /* Custom handle scale. */
1423 param->scale_in[0] *= prev_scale[0];
1424 }
1426 param->scale_in[1] *= prev_scale[1];
1427 }
1429 param->scale_in[2] *= prev_scale[2];
1430 }
1432 param->ease1 *= prev_scale[1];
1433 param->curve_in_x *= prev_scale[1];
1434 param->curve_in_z *= prev_scale[1];
1435 }
1436
1438 param->scale_out[0] *= next_scale[0];
1439 }
1441 param->scale_out[1] *= next_scale[1];
1442 }
1444 param->scale_out[2] *= next_scale[2];
1445 }
1447 param->ease2 *= next_scale[1];
1448 param->curve_out_x *= next_scale[1];
1449 param->curve_out_z *= next_scale[1];
1450 }
1451 }
1452}
1453
1455 const bArmature &armature,
1456 const bool rest,
1457 const bool for_deform,
1458 Mat4 *result_array)
1459{
1461
1462 BKE_pchan_bbone_spline_params_get(pchanbone, armature, rest, &param);
1463
1464 pchanbone.bone->segments = BKE_pchan_bbone_spline_compute(&param, for_deform, result_array);
1465}
1466
1468 float h1[3],
1469 float *r_roll1,
1470 float h2[3],
1471 float *r_roll2,
1472 bool ease,
1473 bool offsets)
1474{
1475 float mat3[3][3];
1476 float length = param->length;
1477 float epsilon = 1e-5 * length;
1478
1479 if (param->do_scale) {
1480 length *= param->scale[1];
1481 }
1482
1483 *r_roll1 = *r_roll2 = 0.0f;
1484
1485 if (param->use_prev) {
1486 copy_v3_v3(h1, param->prev_h);
1487
1488 if (param->prev_bbone) {
1489 /* If previous bone is B-bone too, use average handle direction. */
1490 h1[1] -= length;
1491 }
1492
1493 if (normalize_v3(h1) < epsilon) {
1494 copy_v3_fl3(h1, 0.0f, -1.0f, 0.0f);
1495 }
1496
1497 negate_v3(h1);
1498
1499 if (!param->prev_bbone) {
1500 /* Find the previous roll to interpolate. */
1501 copy_m3_m4(mat3, param->prev_mat);
1502 mat3_vec_to_roll(mat3, h1, r_roll1);
1503 }
1504 }
1505 else {
1506 h1[0] = 0.0f;
1507 h1[1] = 1.0;
1508 h1[2] = 0.0f;
1509 }
1510
1511 if (param->use_next) {
1512 copy_v3_v3(h2, param->next_h);
1513
1514 /* If next bone is B-bone too, use average handle direction. */
1515 if (param->next_bbone) {
1516 /* pass */
1517 }
1518 else {
1519 h2[1] -= length;
1520 }
1521
1522 if (normalize_v3(h2) < epsilon) {
1523 copy_v3_fl3(h2, 0.0f, 1.0f, 0.0f);
1524 }
1525
1526 /* Find the next roll to interpolate as well. */
1527 copy_m3_m4(mat3, param->next_mat);
1528 mat3_vec_to_roll(mat3, h2, r_roll2);
1529 }
1530 else {
1531 h2[0] = 0.0f;
1532 h2[1] = 1.0f;
1533 h2[2] = 0.0f;
1534 }
1535
1536 if (ease) {
1537 const float circle_factor = length * (cubic_tangent_factor_circle_v3(h1, h2) / 0.75f);
1538
1539 const float hlength1 = param->ease1 * circle_factor;
1540 const float hlength2 = param->ease2 * circle_factor;
1541
1542 /* and only now negate h2 */
1543 mul_v3_fl(h1, hlength1);
1544 mul_v3_fl(h2, -hlength2);
1545 }
1546
1547 /* Add effects from bbone properties over the top
1548 * - These properties allow users to hand-animate the
1549 * bone curve/shape, without having to resort to using
1550 * extra bones
1551 * - The "bone" level offsets are for defining the rest-pose
1552 * shape of the bone (e.g. for curved eyebrows for example).
1553 * -> In the viewport, it's needed to define what the rest pose
1554 * looks like
1555 * -> For "rest == 0", we also still need to have it present
1556 * so that we can "cancel out" this rest-pose when it comes
1557 * time to deform some geometry, it won't cause double transforms.
1558 * - The "pchan" level offsets are the ones that animators actually
1559 * end up animating
1560 */
1561 if (offsets) {
1562 /* Add extra rolls. */
1563 *r_roll1 += param->roll1;
1564 *r_roll2 += param->roll2;
1565
1566 /* Extra curve x / y */
1567 /* NOTE:
1568 * Scale correction factors here are to compensate for some random floating-point glitches
1569 * when scaling up the bone or its parent by a factor of approximately 8.15/6, which results
1570 * in the bone length getting scaled up too (from 1 to 8), causing the curve to flatten out.
1571 */
1572 const float xscale_correction = (param->do_scale) ? param->scale[0] : 1.0f;
1573 const float zscale_correction = (param->do_scale) ? param->scale[2] : 1.0f;
1574
1575 h1[0] += param->curve_in_x * xscale_correction;
1576 h1[2] += param->curve_in_z * zscale_correction;
1577
1578 h2[0] += param->curve_out_x * xscale_correction;
1579 h2[2] += param->curve_out_z * zscale_correction;
1580 }
1581}
1582
1584 const float scalemats[2][4][4],
1585 const float pos[3],
1586 const float axis[3],
1587 float roll,
1588 float scalex,
1589 float scalez,
1590 float result[4][4])
1591{
1592 float mat3[3][3];
1593
1594 vec_roll_to_mat3(axis, roll, mat3);
1595
1596 copy_m4_m3(result, mat3);
1597 copy_v3_v3(result[3], pos);
1598
1599 if (param->do_scale) {
1600 /* Correct for scaling when this matrix is used in scaled space. */
1601 mul_m4_series(result, scalemats[0], result, scalemats[1]);
1602 }
1603
1604 /* BBone scale... */
1605 mul_v3_fl(result[0], scalex);
1606 mul_v3_fl(result[2], scalez);
1607}
1608
1609/* Fade from first to second derivative when the handle is very short. */
1610static void ease_handle_axis(const float deriv1[3], const float deriv2[3], float r_axis[3])
1611{
1612 const float gap = 0.1f;
1613
1614 copy_v3_v3(r_axis, deriv1);
1615
1616 const float len2 = len_squared_v3(deriv2);
1617 if (len2 == 0.0f) [[unlikely]] {
1618 return;
1619 }
1620 const float len1 = len_squared_v3(deriv1);
1621 const float ratio = len1 / len2;
1622 if (ratio < gap * gap) {
1623 madd_v3_v3fl(r_axis, deriv2, gap - sqrtf(ratio));
1624 }
1625}
1626
1628 const bool for_deform,
1629 Mat4 *result_array)
1630{
1631 float scalemats[2][4][4];
1632 float bezt_controls[4][3];
1633 float h1[3], roll1, h2[3], roll2, prev[3], cur[3], axis[3];
1634 float length = param->length;
1635
1636 if (param->do_scale) {
1637 size_to_mat4(scalemats[1], param->scale);
1638 invert_m4_m4(scalemats[0], scalemats[1]);
1639
1640 length *= param->scale[1];
1641 }
1642
1643 BKE_pchan_bbone_handles_compute(param, h1, &roll1, h2, &roll2, true, true);
1644
1645 /* Make curve. */
1647
1648 copy_v3_fl3(bezt_controls[3], 0.0f, length, 0.0f);
1649 add_v3_v3v3(bezt_controls[2], bezt_controls[3], h2);
1650 copy_v3_v3(bezt_controls[1], h1);
1651 zero_v3(bezt_controls[0]);
1652
1653 /* Compute lengthwise segment scale. */
1654 float segment_scales[MAX_BBONE_SUBDIV];
1655
1656 CLAMP_MIN(param->scale_in[1], 0.0001f);
1657 CLAMP_MIN(param->scale_out[1], 0.0001f);
1658
1659 const float log_scale_in_len = logf(param->scale_in[1]);
1660 const float log_scale_out_len = logf(param->scale_out[1]);
1661
1662 for (int i = 0; i < param->segments; i++) {
1663 const float fac = float(i) / (param->segments - 1);
1664 segment_scales[i] = expf(interpf(log_scale_out_len, log_scale_in_len, fac));
1665 }
1666
1667 /* Compute segment vertex offsets along the curve length. */
1668 float bezt_points[MAX_BBONE_SUBDIV + 1];
1669
1671 bezt_controls, MAX_BBONE_SUBDIV, param->segments, segment_scales, bezt_points);
1672
1673 /* Deformation uses N+1 matrices computed at points between the segments. */
1674 if (for_deform) {
1675 /* Bezier derivatives. */
1676 float bezt_deriv1[3][3], bezt_deriv2[2][3];
1677
1678 for (int i = 0; i < 3; i++) {
1679 sub_v3_v3v3(bezt_deriv1[i], bezt_controls[i + 1], bezt_controls[i]);
1680 }
1681 for (int i = 0; i < 2; i++) {
1682 sub_v3_v3v3(bezt_deriv2[i], bezt_deriv1[i + 1], bezt_deriv1[i]);
1683 }
1684
1685 /* End points require special handling to fix zero length handles. */
1686 ease_handle_axis(bezt_deriv1[0], bezt_deriv2[0], axis);
1688 scalemats,
1689 bezt_controls[0],
1690 axis,
1691 roll1,
1692 param->scale_in[0],
1693 param->scale_in[2],
1694 result_array[0].mat);
1695
1696 for (int a = 1; a < param->segments; a++) {
1697 evaluate_cubic_bezier(bezt_controls, bezt_points[a], cur, axis);
1698
1699 float fac = float(a) / param->segments;
1700 float roll = interpf(roll2, roll1, fac);
1701 float scalex = interpf(param->scale_out[0], param->scale_in[0], fac);
1702 float scalez = interpf(param->scale_out[2], param->scale_in[2], fac);
1703
1705 param, scalemats, cur, axis, roll, scalex, scalez, result_array[a].mat);
1706 }
1707
1708 negate_v3(bezt_deriv2[1]);
1709 ease_handle_axis(bezt_deriv1[2], bezt_deriv2[1], axis);
1711 scalemats,
1712 bezt_controls[3],
1713 axis,
1714 roll2,
1715 param->scale_out[0],
1716 param->scale_out[2],
1717 result_array[param->segments].mat);
1718 }
1719 /* Other code (e.g. display) uses matrices for the segments themselves. */
1720 else {
1721 zero_v3(prev);
1722
1723 for (int a = 0; a < param->segments; a++) {
1724 evaluate_cubic_bezier(bezt_controls, bezt_points[a + 1], cur, axis);
1725
1726 sub_v3_v3v3(axis, cur, prev);
1727
1728 float fac = (a + 0.5f) / param->segments;
1729 float roll = interpf(roll2, roll1, fac);
1730 float scalex = interpf(param->scale_out[0], param->scale_in[0], fac);
1731 float scalez = interpf(param->scale_out[2], param->scale_in[2], fac);
1732
1734 param, scalemats, prev, axis, roll, scalex, scalez, result_array[a].mat);
1735 copy_v3_v3(prev, cur);
1736 }
1737 }
1738
1739 return param->segments;
1740}
1741
1743 const int segments,
1744 const bool use_boundaries)
1745{
1746 bPoseChannel_Runtime *runtime = &pchan->runtime;
1747
1748 if (runtime->bbone_segments != segments) {
1750
1751 runtime->bbone_segments = segments;
1753 1 + uint(segments), "bPoseChannel_Runtime::bbone_rest_mats");
1755 1 + uint(segments), "bPoseChannel_Runtime::bbone_pose_mats");
1757 2 + uint(segments), "bPoseChannel_Runtime::bbone_deform_mats");
1758 runtime->bbone_dual_quats = MEM_new_array<DualQuat>(1 + uint(segments),
1759 "bPoseChannel_Runtime::bbone_dual_quats");
1760 }
1761
1762 /* If the segment count changed, the array was deallocated and nulled above. */
1763 if (use_boundaries && !runtime->bbone_segment_boundaries) {
1764 runtime->bbone_segment_boundaries = MEM_new_array<bPoseChannel_BBoneSegmentBoundary>(
1765 1 + uint(segments), "bPoseChannel_Runtime::bbone_segment_boundaries");
1766 }
1767 else if (!use_boundaries) {
1768 MEM_SAFE_DELETE(runtime->bbone_segment_boundaries);
1769 }
1770}
1771
1774{
1775 bPoseChannel *pchan = pchanbone.pchan;
1776 const Bone *bone = pchanbone.bone;
1777 bPoseChannel_Runtime *runtime = &pchan->runtime;
1778 const Mat4 *b_bone_rest = runtime->bbone_rest_mats;
1780
1781 /* Convert joints to pose space. */
1782 for (int i = 0; i <= bone->segments; i++) {
1783 mul_v3_m4v3(boundaries[i].point, bone->arm_mat, b_bone_rest[i].mat[3]);
1784 mul_v3_mat3_m4v3(boundaries[i].plane_normal, bone->arm_mat, b_bone_rest[i].mat[1]);
1785 normalize_v3(boundaries[i].plane_normal);
1786 }
1787
1788 /* Precompute coefficients for the mapping calculations. */
1789 for (int i = 0; i <= bone->segments; i++) {
1790 boundaries[i].plane_offset = dot_v3v3(boundaries[i].point, boundaries[i].plane_normal);
1791 }
1792
1793 /* Precompute the inverted length of the curve. */
1794 float arc_length = 0.0f;
1795
1796 for (int i = 0; i < bone->segments; i++) {
1797 arc_length += len_v3v3(boundaries[i + 1].point, boundaries[i].point);
1798 }
1799
1800 runtime->bbone_arc_length_reciprocal = 1.0f / arc_length;
1801
1802 /* Precompute the BSP depth based widening coefficients.
1803 * The actual space partitioning includes two extra virtual segments for the ends. */
1804 const int bsp_depth = int(ceilf(log2f(bone->segments + 2)));
1805
1806 BLI_assert(bsp_depth <= bone->segments);
1807
1808 /* Maximum half-width of the smoothing band at the bsp tree root plane, in segments.
1809 * The tuning coefficient was chosen by trial and error (see PR #110758). */
1810 const float tuning_factor = 0.222f;
1811 const float straight_length = len_v3v3(boundaries[0].point, boundaries[bone->segments].point);
1812 const float max_depth_scale = bone->segments * (straight_length / arc_length) * tuning_factor;
1813
1814 /* Per tree layer scaling factor, aiming to reduce the radius to 1 segment at the leaf level.
1815 * Since depth_scale is actually a reciprocal of the width, this factor is >= 1. */
1816 const float scale_factor = powf(max_ff(max_depth_scale, 1.0f), 1.0f / (bsp_depth - 1));
1817
1818 boundaries[0].depth_scale = bone->segments / max_depth_scale;
1819
1820 for (int i = 1; i < bsp_depth; i++) {
1821 boundaries[i].depth_scale = boundaries[i - 1].depth_scale * scale_factor;
1822 }
1823}
1824
1826 const bArmature &armature)
1827{
1828 bPoseChannel *pchan = pchanbone.pchan;
1829 Bone *bone = pchanbone.bone;
1830 bPoseChannel_Runtime *runtime = &pchan->runtime;
1831 int segments = bone->segments;
1832
1833 BLI_assert(segments > 1);
1834
1835 /* Allocate the cache if needed. */
1836 const bool use_curved_mapping = bone->bbone_mapping_mode == BBONE_MAPPING_CURVED;
1837
1838 allocate_bbone_cache(pchan, segments, use_curved_mapping);
1839
1840 /* Compute the shape. */
1841 Mat4 *b_bone = runtime->bbone_pose_mats;
1842 Mat4 *b_bone_rest = runtime->bbone_rest_mats;
1843 Mat4 *b_bone_mats = runtime->bbone_deform_mats;
1844 DualQuat *b_bone_dual_quats = runtime->bbone_dual_quats;
1845 int a;
1846
1847 BKE_pchan_bbone_spline_setup(pchanbone, armature, false, true, b_bone);
1848 BKE_pchan_bbone_spline_setup(pchanbone, armature, true, true, b_bone_rest);
1849
1850 /* Compute segment boundaries. */
1851 if (runtime->bbone_segment_boundaries) {
1852 BLI_assert(use_curved_mapping);
1854 }
1855
1856 /* Compute deform matrices. */
1857 /* first matrix is the inverse arm_mat, to bring points in local bone space
1858 * for finding out which segment it belongs to */
1859 invert_m4_m4(b_bone_mats[0].mat, bone->arm_mat);
1860
1861 /* then we make the b_bone_mats:
1862 * - first transform to local bone space
1863 * - translate over the curve to the bbone mat space
1864 * - transform with b_bone matrix
1865 * - transform back into global space */
1866
1867 for (a = 0; a <= bone->segments; a++) {
1868 float tmat[4][4];
1869
1870 invert_m4_m4(tmat, b_bone_rest[a].mat);
1871 mul_m4_series(b_bone_mats[a + 1].mat,
1872 pchan->chan_mat,
1873 bone->arm_mat,
1874 b_bone[a].mat,
1875 tmat,
1876 b_bone_mats[0].mat);
1877
1878 /* Compute the orthonormal object space rest matrix of the segment. */
1879 mul_m4_m4m4(tmat, bone->arm_mat, b_bone_rest[a].mat);
1880 normalize_m4(tmat);
1881
1882 mat4_to_dquat(&b_bone_dual_quats[a], tmat, b_bone_mats[a + 1].mat);
1883 }
1884}
1885
1887{
1888 bPoseChannel_Runtime *runtime = &pchan->runtime;
1889 bPoseChannel_Runtime *runtime_from = &pchan_from->runtime;
1890 int segments = runtime_from->bbone_segments;
1891
1892 if (segments <= 1) {
1894 }
1895 else {
1896 const bool use_curved_mapping = runtime_from->bbone_segment_boundaries != nullptr;
1897
1898 allocate_bbone_cache(pchan, segments, use_curved_mapping);
1899
1900 memcpy(runtime->bbone_rest_mats, runtime_from->bbone_rest_mats, sizeof(Mat4) * (1 + segments));
1901 memcpy(runtime->bbone_pose_mats, runtime_from->bbone_pose_mats, sizeof(Mat4) * (1 + segments));
1902 memcpy(runtime->bbone_deform_mats,
1903 runtime_from->bbone_deform_mats,
1904 sizeof(Mat4) * (2 + segments));
1905 memcpy(runtime->bbone_dual_quats,
1906 runtime_from->bbone_dual_quats,
1907 sizeof(DualQuat) * (1 + segments));
1908
1909 if (use_curved_mapping) {
1911
1912 memcpy(runtime->bbone_segment_boundaries,
1913 runtime_from->bbone_segment_boundaries,
1914 sizeof(bPoseChannel_BBoneSegmentBoundary) * (1 + segments));
1915 }
1916 else {
1917 BLI_assert(runtime->bbone_segment_boundaries == nullptr);
1918 }
1919 }
1920}
1921
1923 float head_tail,
1924 int *r_index,
1925 float *r_blend_next)
1926{
1927 int segments = bone.segments;
1928
1929 CLAMP(head_tail, 0.0f, 1.0f);
1930
1931 /* Calculate the indices of the 2 affecting b_bone segments.
1932 * Integer part is the first segment's index.
1933 * Integer part plus 1 is the second segment's index.
1934 * Fractional part is the blend factor. */
1935 float pre_blend = head_tail * float(segments);
1936
1937 int index = int(floorf(pre_blend));
1938 CLAMP(index, 0, segments - 1);
1939
1940 float blend = pre_blend - index;
1941 CLAMP(blend, 0.0f, 1.0f);
1942
1943 *r_index = index;
1944 *r_blend_next = blend;
1945}
1946
1949 const float *co,
1950 int *r_index,
1951 float *r_blend_next)
1952{
1953 const Mat4 *mats = pchanbone.pchan->runtime.bbone_deform_mats;
1954 const float (*mat)[4] = mats[0].mat;
1955
1956 /* Transform co to bone space and get its y component. */
1957 const float y = mat[0][1] * co[0] + mat[1][1] * co[1] + mat[2][1] * co[2] + mat[3][1];
1958
1959 /* Calculate the indices of the 2 affecting b_bone segments. */
1961 *pchanbone.bone, y / pchanbone.bone->length, r_index, r_blend_next);
1962}
1963
1966 const float *co)
1967{
1968 return dot_v3v3(co, boundary.plane_normal) - boundary.plane_offset;
1969}
1970
1973 const float *co,
1974 int *r_index,
1975 float *r_blend_next)
1976{
1977 const bPoseChannel *pchan = pchanbone.pchan;
1979 const int segments = pchan->runtime.bbone_segments;
1980
1981 /* Saved signed distances from co to each checked boundary plane. */
1982 float boundary_dist[MAX_BBONE_SUBDIV + 1];
1983
1984 /* Stack of BSP plane indices that were checked in the binary search. */
1985 int boundary_idx_stack[MAX_BBONE_SUBDIV];
1986 int stack_top = -1;
1987
1988 /* Perform a BSP binary search to narrow things down to one segment.
1989 * Checked BSP planes are stored for the smoothing pass later. */
1990 int start = -1, end = segments + 1, bias = 0;
1991
1992 while (end - start > 1) {
1993 const int mid = (start + end + bias) / 2;
1994
1995 BLI_assert(start < mid && mid < end);
1996
1997 const float dist = bbone_segment_bsp_signed_distance(boundaries[mid], co);
1998
1999 boundary_idx_stack[++stack_top] = mid;
2000 boundary_dist[mid] = dist;
2001
2002 if (dist < 0) {
2003 end = mid;
2004 /* Bias division of odd numbers toward the previous split. This should produce
2005 * a slightly smoother and more symmetrical evolute boundary near the ends. */
2006 bias = 1;
2007 }
2008 else {
2009 start = mid;
2010 bias = 0;
2011 }
2012 }
2013
2014 /* Compute the mapping from the individual segment, or the curve ends. */
2015 const float segment_size = 1.0f / segments;
2016 float head_tail;
2017
2018 if (end <= 0) {
2019 head_tail = 0;
2020 }
2021 else if (start >= segments) {
2022 head_tail = 1;
2023 }
2024 else {
2025 /* Linear interpolation between the innermost two planes. */
2026 const float d1 = fabsf(boundary_dist[start]);
2027 const float d2 = fabsf(boundary_dist[end]);
2028 const float t = d1 / (d1 + d2);
2029
2030 head_tail = segment_size * (start + t);
2031 }
2032
2033 /* Smooth the mapping to suppress discontinuities by using BSP boundaries up the stack.
2034 *
2035 * This works basically by pulling the mapped position towards the boundary in order to
2036 * reduce the gradient slope to the ideal value (the one you get for points directly on
2037 * the curve), using heuristic blend strength falloff coefficients based on the distances
2038 * to the boundary plane before and after mapping. See PR #110758 for more details, or
2039 * https://developer.blender.org/docs/features/animation/b-bone_vertex_mapping/#curved-mapping */
2040 const float segment_scale = pchan->runtime.bbone_arc_length_reciprocal;
2041
2042 for (int i = stack_top; i >= 0; --i) {
2043 const int boundary_idx = boundary_idx_stack[i];
2044
2045 /* Boundary in the head-tail space. */
2046 const float boundary_pos = boundary_idx * segment_size;
2047
2048 /* Distance of the original 3d point (co) from the boundary plane,
2049 * mapped to the head-tail space using the ideal slope ratio. */
2050 const float point_dist = boundary_dist[boundary_idx] * segment_scale;
2051 const float point_dist_abs = fabsf(point_dist);
2052
2053 /* Distance of the current mapped position from the boundary in the head-tail space. */
2054 const float mapped_dist = fabsf(head_tail - boundary_pos);
2055
2056 /* Only reduce the local gradient slope, don't increase it. This basically limits
2057 * smoothing to the inside of the curve, leaving outside as is. */
2058 const float slope_gap = mapped_dist - point_dist_abs;
2059
2060 if (slope_gap <= 0) {
2061 continue;
2062 }
2063
2064 /* Only affect points close to the split line; the radius depends on the depth
2065 * in the stack using precomputed coefficients. */
2066 const float dist_coeff = 1.0f - point_dist_abs * boundaries[i].depth_scale;
2067
2068 if (dist_coeff <= 0) {
2069 continue;
2070 }
2071
2072 /* Asymptotically clamp the slope coefficient to 1. The tune coefficients here and
2073 * below control the sharpness of the transition and were chosen by trial and error. */
2074 const float slope_tune_coeff = 3.0f;
2075 const float scaled_gap = slope_gap * slope_tune_coeff;
2076 const float slope_coeff = scaled_gap / (scaled_gap + point_dist_abs);
2077
2078 /* Smooth the distance based coefficient around zero. */
2079 const float dist_tune_coeff = 7.0f;
2080 const float dist_coeff_smooth = dist_coeff * dist_coeff * (dist_tune_coeff + 1.0f) /
2081 (dist_tune_coeff * dist_coeff + 1.0f);
2082
2083 /* Blend towards the point on the ideal slope. */
2084 const float target_pos = boundary_pos + point_dist;
2085
2086 head_tail = interpf(target_pos, head_tail, slope_coeff * dist_coeff_smooth);
2087 }
2088
2089 /* Calculate the indices of the 2 affecting b_bone segments. */
2090 BKE_pchan_bbone_deform_clamp_segment_index(*pchanbone.bone, head_tail, r_index, r_blend_next);
2091}
2092
2094 const float *co,
2095 int *r_index,
2096 float *r_blend_next)
2097{
2098 if (pchanbone.pchan->runtime.bbone_segment_boundaries) {
2099 find_bbone_segment_index_curved(pchanbone, co, r_index, r_blend_next);
2100 }
2101 else {
2102 find_bbone_segment_index_straight(pchanbone, co, r_index, r_blend_next);
2103 }
2104}
2105
2107
2108/* -------------------------------------------------------------------- */
2111
2112void BKE_armature_mat_world_to_pose(Object *ob, const float inmat[4][4], float outmat[4][4])
2113{
2114 float obmat[4][4];
2115
2116 /* prevent crashes */
2117 if (ob == nullptr) {
2118 return;
2119 }
2120
2121 /* Get inverse of (armature) object's matrix. */
2122 invert_m4_m4(obmat, ob->object_to_world().ptr());
2123
2124 /* multiply given matrix by object's-inverse to find pose-space matrix */
2125 mul_m4_m4m4(outmat, inmat, obmat);
2126}
2127
2128void BKE_armature_loc_world_to_pose(Object *ob, const float inloc[3], float outloc[3])
2129{
2130 float xLocMat[4][4];
2131 float nLocMat[4][4];
2132
2133 /* build matrix for location */
2134 unit_m4(xLocMat);
2135 copy_v3_v3(xLocMat[3], inloc);
2136
2137 /* get bone-space cursor matrix and extract location */
2138 BKE_armature_mat_world_to_pose(ob, xLocMat, nLocMat);
2139 copy_v3_v3(outloc, nLocMat[3]);
2140}
2141
2143
2144/* -------------------------------------------------------------------- */
2147
2148void BKE_bone_offset_matrix_get(const Bone *bone, float offs_bone[4][4])
2149{
2150 BLI_assert(bone->parent != nullptr);
2151
2152 /* Bone transform itself. */
2153 copy_m4_m3(offs_bone, bone->bone_mat);
2154
2155 /* The bone's root offset (is in the parent's coordinate system). */
2156 copy_v3_v3(offs_bone[3], bone->head);
2157
2158 /* Get the length translation of parent (length along y axis). */
2159 offs_bone[3][1] += bone->parent->length;
2160}
2161
2163 BoneParentTransform *r_bpt)
2164{
2165
2166 /* set up variables for quicker access below */
2167 const Bone *bone = pchanbone.bone;
2168 const Bone *parbone = bone->parent;
2169 const bPoseChannel *pchan = pchanbone.pchan;
2170 const bPoseChannel *parchan = pchan->parent;
2171
2172 if (parchan) {
2173 float offs_bone[4][4];
2174 /* yoffs(b-1) + root(b) + bonemat(b). */
2175 BKE_bone_offset_matrix_get(bone, offs_bone);
2176
2178 bone->inherit_scale_mode,
2179 offs_bone,
2180 parbone->arm_mat,
2181 parchan->pose_mat,
2182 r_bpt);
2183 }
2184 else {
2186 bone->flag, bone->inherit_scale_mode, bone->arm_mat, nullptr, nullptr, r_bpt);
2187 }
2188}
2189
2191 int inherit_scale_mode,
2192 const float offs_bone[4][4],
2193 const float parent_arm_mat[4][4],
2194 const float parent_pose_mat[4][4],
2195 BoneParentTransform *r_bpt)
2196{
2197 copy_v3_fl(r_bpt->post_scale, 1.0f);
2198
2199 if (parent_pose_mat) {
2200 const bool use_rotation = (bone_flag & BONE_HINGE) == 0;
2201 const bool full_transform = use_rotation && inherit_scale_mode == BONE_INHERIT_SCALE_FULL;
2202
2203 /* Compose the rotscale matrix for this bone. */
2204 if (full_transform) {
2205 /* Parent pose rotation and scale. */
2206 mul_m4_m4m4(r_bpt->rotscale_mat, parent_pose_mat, offs_bone);
2207 }
2208 else {
2209 float tmat[4][4], tscale[3];
2210
2211 /* If using parent pose rotation: */
2212 if (use_rotation) {
2213 copy_m4_m4(tmat, parent_pose_mat);
2214
2215 /* Normalize the matrix when needed. */
2216 switch (inherit_scale_mode) {
2219 /* Keep scale and shear. */
2220 break;
2221
2224 /* Remove scale and shear from parent. */
2225 orthogonalize_m4_stable(tmat, 1, true);
2226 break;
2227
2229 /* Remove shear and extract scale. */
2230 orthogonalize_m4_stable(tmat, 1, false);
2231 normalize_m4_ex(tmat, r_bpt->post_scale);
2232 break;
2233
2235 /* Remove only scale - bad legacy way. */
2236 normalize_m4(tmat);
2237 break;
2238
2239 default:
2241 }
2242 }
2243 /* If removing parent pose rotation: */
2244 else {
2245 copy_m4_m4(tmat, parent_arm_mat);
2246
2247 /* Copy the parent scale when needed. */
2248 switch (inherit_scale_mode) {
2250 /* Ignore effects of shear. */
2251 mat4_to_size(tscale, parent_pose_mat);
2252 rescale_m4(tmat, tscale);
2253 break;
2254
2256 /* Take the effects of parent shear into account to get exact volume. */
2257 mat4_to_size_fix_shear(tscale, parent_pose_mat);
2258 rescale_m4(tmat, tscale);
2259 break;
2260
2262 mat4_to_size_fix_shear(r_bpt->post_scale, parent_pose_mat);
2263 break;
2264
2268 /* Keep unscaled. */
2269 break;
2270
2271 default:
2273 }
2274 }
2275
2276 /* Apply the average parent scale when needed. */
2277 if (inherit_scale_mode == BONE_INHERIT_SCALE_AVERAGE) {
2278 mul_mat3_m4_fl(tmat, cbrtf(fabsf(mat4_to_volume_scale(parent_pose_mat))));
2279 }
2280
2281 mul_m4_m4m4(r_bpt->rotscale_mat, tmat, offs_bone);
2282
2283 /* Remove remaining shear when needed, preserving volume. */
2284 if (inherit_scale_mode == BONE_INHERIT_SCALE_FIX_SHEAR) {
2285 orthogonalize_m4_stable(r_bpt->rotscale_mat, 1, false);
2286 }
2287 }
2288
2289 /* Compose the loc matrix for this bone. */
2290 /* NOTE: That version does not modify bone's loc when HINGE/NO_SCALE options are set. */
2291
2292 /* In this case, use the object's space *orientation*. */
2293 if (bone_flag & BONE_NO_LOCAL_LOCATION) {
2294 /* XXX I'm sure that code can be simplified! */
2295 float bone_loc[4][4], bone_rotscale[3][3], tmat4[4][4], tmat3[3][3];
2296 unit_m4(bone_loc);
2297 unit_m4(r_bpt->loc_mat);
2298 unit_m4(tmat4);
2299
2300 mul_v3_m4v3(bone_loc[3], parent_pose_mat, offs_bone[3]);
2301
2302 unit_m3(bone_rotscale);
2303 copy_m3_m4(tmat3, parent_pose_mat);
2304 mul_m3_m3m3(bone_rotscale, tmat3, bone_rotscale);
2305
2306 copy_m4_m3(tmat4, bone_rotscale);
2307 mul_m4_m4m4(r_bpt->loc_mat, bone_loc, tmat4);
2308 }
2309 /* Those flags do not affect position, use plain parent transform space! */
2310 else if (!full_transform) {
2311 mul_m4_m4m4(r_bpt->loc_mat, parent_pose_mat, offs_bone);
2312 }
2313 /* Else (i.e. default, usual case),
2314 * just use the same matrix for rotation/scaling, and location. */
2315 else {
2316 copy_m4_m4(r_bpt->loc_mat, r_bpt->rotscale_mat);
2317 }
2318 }
2319 /* Root bones. */
2320 else {
2321 /* Rotation/scaling. */
2322 copy_m4_m4(r_bpt->rotscale_mat, offs_bone);
2323 /* Translation. */
2324 if (bone_flag & BONE_NO_LOCAL_LOCATION) {
2325 /* Translation of arm_mat, without the rotation. */
2326 unit_m4(r_bpt->loc_mat);
2327 copy_v3_v3(r_bpt->loc_mat[3], offs_bone[3]);
2328 }
2329 else {
2330 copy_m4_m4(r_bpt->loc_mat, r_bpt->rotscale_mat);
2331 }
2332 }
2333}
2334
2336{
2337 unit_m4(bpt->rotscale_mat);
2338 unit_m4(bpt->loc_mat);
2339 copy_v3_fl(bpt->post_scale, 1.0f);
2340}
2341
2348
2350 const BoneParentTransform *in2,
2352{
2353 mul_m4_m4m4(result->rotscale_mat, in1->rotscale_mat, in2->rotscale_mat);
2354 mul_m4_m4m4(result->loc_mat, in1->loc_mat, in2->loc_mat);
2355 mul_v3_v3v3(result->post_scale, in1->post_scale, in2->post_scale);
2356}
2357
2359 const float inmat[4][4],
2360 float outmat[4][4])
2361{
2362 /* in case inmat == outmat */
2363 float tmploc[3];
2364 copy_v3_v3(tmploc, inmat[3]);
2365
2366 mul_m4_m4m4(outmat, bpt->rotscale_mat, inmat);
2367 mul_v3_m4v3(outmat[3], bpt->loc_mat, tmploc);
2368 rescale_m4(outmat, bpt->post_scale);
2369}
2370
2372 const float inmat[4][4],
2373 float outmat[4][4])
2374{
2376
2379 BKE_bone_parent_transform_apply(&bpt, inmat, outmat);
2380}
2381
2383 const float inmat[4][4],
2384 float outmat[4][4])
2385{
2387
2389 BKE_bone_parent_transform_apply(&bpt, inmat, outmat);
2390}
2391
2393 const float inloc[3],
2394 float outloc[3])
2395{
2396 float xLocMat[4][4];
2397 float nLocMat[4][4];
2398
2399 /* build matrix for location */
2400 unit_m4(xLocMat);
2401 copy_v3_v3(xLocMat[3], inloc);
2402
2403 /* get bone-space cursor matrix and extract location */
2404 BKE_armature_mat_pose_to_bone(pchanbone, xLocMat, nLocMat);
2405 copy_v3_v3(outloc, nLocMat[3]);
2406}
2407
2409
2410/* -------------------------------------------------------------------- */
2415
2416void BKE_armature_mat_pose_to_bone_ex(Depsgraph *depsgraph,
2417 Object *ob,
2418 const bPoseChannel *pchan,
2419 const float inmat[4][4],
2420 float outmat[4][4])
2421{
2422 bPoseChannel work_pchan = dna::shallow_copy(*pchan);
2423
2424 /* Recalculate pose matrix with only parent transformations,
2425 * bone location/scale/rotation is ignored, scene and frame are not used. */
2426 BKE_pose_where_is_bone(depsgraph, nullptr, ob, &work_pchan, 0.0f, false);
2427
2428 /* Find the matrix, need to remove the bone transforms first so this is calculated
2429 * as a matrix to set rather than a difference on top of what's already there. */
2430 unit_m4(outmat);
2431 BKE_pchan_apply_mat4(&work_pchan, outmat, false);
2432
2433 BKE_armature_mat_pose_to_bone({&work_pchan, work_pchan.bone_get(*ob)}, inmat, outmat);
2434}
2435
2436void BKE_pchan_mat3_to_rot(bPoseChannel *pchan, const float mat[3][3], bool use_compat)
2437{
2438 BLI_ASSERT_UNIT_M3(mat);
2439
2440 switch (pchan->rotmode) {
2441 case ROT_MODE_QUAT:
2442 mat3_normalized_to_quat(pchan->quat, mat);
2443 break;
2444 case ROT_MODE_AXISANGLE:
2445 mat3_normalized_to_axis_angle(pchan->rotAxis, &pchan->rotAngle, mat);
2446 break;
2447 default: /* euler */
2448 if (use_compat) {
2449 mat3_normalized_to_compatible_eulO(pchan->eul, pchan->eul, pchan->rotmode, mat);
2450 }
2451 else {
2452 mat3_normalized_to_eulO(pchan->eul, pchan->rotmode, mat);
2453 }
2454 break;
2455 }
2456}
2457
2458void BKE_pchan_rot_to_mat3(const bPoseChannel *pchan, float r_mat[3][3])
2459{
2460 /* rotations may either be quats, eulers (with various rotation orders), or axis-angle */
2461 if (pchan->rotmode > 0) {
2462 /* Euler rotations (will cause gimbal lock,
2463 * but this can be alleviated a bit with rotation orders) */
2464 eulO_to_mat3(r_mat, pchan->eul, pchan->rotmode);
2465 }
2466 else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
2467 /* axis-angle - not really that great for 3D-changing orientations */
2468 axis_angle_to_mat3(r_mat, pchan->rotAxis, pchan->rotAngle);
2469 }
2470 else {
2471 /* quats are normalized before use to eliminate scaling issues */
2472 float quat[4];
2473
2474 /* NOTE: we now don't normalize the stored values anymore,
2475 * since this was kind of evil in some cases but if this proves to be too problematic,
2476 * switch back to the old system of operating directly on the stored copy. */
2477 normalize_qt_qt(quat, pchan->quat);
2478 quat_to_mat3(r_mat, quat);
2479 }
2480}
2481
2483{
2484 float4 quat;
2485 if (pchan.rotmode > 0) {
2486 eulO_to_quat(quat, pchan.eul, pchan.rotmode);
2487 }
2488 else if (pchan.rotmode == ROT_MODE_AXISANGLE) {
2489 axis_angle_to_quat(quat, pchan.rotAxis, pchan.rotAngle);
2490 }
2491 else {
2492 /* Normalized quaternion to stay consistent with `BKE_pchan_rot_to_mat3`. */
2493 normalize_qt_qt(quat, pchan.quat);
2494 }
2495 return quat;
2496}
2497
2499{
2500 switch (pchan.rotmode) {
2501 case ROT_MODE_QUAT:
2502 normalize_qt_qt(pchan.quat, quat);
2503 break;
2504 case ROT_MODE_AXISANGLE:
2505 quat_to_axis_angle(pchan.rotAxis, &pchan.rotAngle, quat);
2506 break;
2507 default: /* euler */
2508 quat_to_eulO(pchan.eul, pchan.rotmode, quat);
2509 break;
2510 }
2511}
2512
2513void BKE_pchan_apply_mat4(bPoseChannel *pchan, const float mat[4][4], bool use_compat)
2514{
2515 float rot[3][3];
2516 mat4_to_loc_rot_size(pchan->loc, rot, pchan->scale, mat);
2517 BKE_pchan_mat3_to_rot(pchan, rot, use_compat);
2518}
2519
2520void BKE_armature_mat_pose_to_delta(float delta_mat[4][4],
2521 float pose_mat[4][4],
2522 float arm_mat[4][4])
2523{
2524 float imat[4][4];
2525
2526 invert_m4_m4(imat, arm_mat);
2527 mul_m4_m4m4(delta_mat, imat, pose_mat);
2528}
2529
2531
2532/* -------------------------------------------------------------------- */
2537
2538void BKE_rotMode_change_values(float quat[4],
2539 float eul[3],
2540 float axis[3],
2541 float *angle,
2542 eRotationModes oldMode,
2543 eRotationModes newMode)
2544{
2545 /* check if any change - if so, need to convert data */
2546 if (newMode > 0) { /* to euler */
2547 if (oldMode == ROT_MODE_AXISANGLE) {
2548 /* axis-angle to euler */
2549 axis_angle_to_eulO(eul, newMode, axis, *angle);
2550 }
2551 else if (oldMode == ROT_MODE_QUAT) {
2552 /* quat to euler */
2553 normalize_qt(quat);
2554 quat_to_eulO(eul, newMode, quat);
2555 }
2556 /* else { no conversion needed } */
2557 }
2558 else if (newMode == ROT_MODE_QUAT) { /* to quat */
2559 if (oldMode == ROT_MODE_AXISANGLE) {
2560 /* axis angle to quat */
2561 axis_angle_to_quat(quat, axis, *angle);
2562 }
2563 else if (oldMode > 0) {
2564 /* euler to quat */
2565 eulO_to_quat(quat, eul, oldMode);
2566 }
2567 /* else { no conversion needed } */
2568 }
2569 else if (newMode == ROT_MODE_AXISANGLE) { /* to axis-angle */
2570 if (oldMode > 0) {
2571 /* euler to axis angle */
2572 eulO_to_axis_angle(axis, angle, eul, oldMode);
2573 }
2574 else if (oldMode == ROT_MODE_QUAT) {
2575 /* quat to axis angle */
2576 normalize_qt(quat);
2577 quat_to_axis_angle(axis, angle, quat);
2578 }
2579
2580 /* When converting to axis-angle,
2581 * we need a special exception for the case when there is no axis. */
2582 if (IS_EQF(axis[0], axis[1]) && IS_EQF(axis[1], axis[2])) {
2583 /* for now, rotate around y-axis then (so that it simply becomes the roll) */
2584 axis[1] = 1.0f;
2585 }
2586 }
2587}
2588
2590
2591/* -------------------------------------------------------------------- */
2594
2595void BKE_pchan_protected_location_set(bPoseChannel *pchan, const float location[3])
2596{
2597 if ((pchan->protectflag & OB_LOCK_LOCX) == 0) {
2598 pchan->loc[0] = location[0];
2599 }
2600 if ((pchan->protectflag & OB_LOCK_LOCY) == 0) {
2601 pchan->loc[1] = location[1];
2602 }
2603 if ((pchan->protectflag & OB_LOCK_LOCZ) == 0) {
2604 pchan->loc[2] = location[2];
2605 }
2606}
2607
2608void BKE_pchan_protected_rotation_set(bPoseChannel *pchan, const float mat[3][3])
2609{
2610 switch (pchan->rotmode) {
2611 case ROT_MODE_QUAT: {
2612 float quat[4];
2613 mat3_to_quat(quat, mat);
2615 break;
2616 }
2617 case ROT_MODE_AXISANGLE:
2618 float angle, axis[3];
2619 mat3_to_axis_angle(axis, &angle, mat);
2621 break;
2622
2623 default:
2624 float euler[3];
2625 mat3_to_compatible_eulO(euler, pchan->eul, pchan->rotmode, mat);
2627 break;
2628 }
2629}
2630
2632{
2633 if ((pchan->protectflag & OB_LOCK_SCALEX) == 0) {
2634 pchan->scale[0] = scale[0];
2635 }
2636 if ((pchan->protectflag & OB_LOCK_SCALEY) == 0) {
2637 pchan->scale[1] = scale[1];
2638 }
2639 if ((pchan->protectflag & OB_LOCK_SCALEZ) == 0) {
2640 pchan->scale[2] = scale[2];
2641 }
2642}
2643
2645{
2646 if ((pchan->protectflag & OB_LOCK_ROTW) == 0) {
2647 pchan->quat[0] = quat[0];
2648 }
2649 if ((pchan->protectflag & OB_LOCK_ROTX) == 0) {
2650 pchan->quat[1] = quat[1];
2651 }
2652 if ((pchan->protectflag & OB_LOCK_ROTY) == 0) {
2653 pchan->quat[2] = quat[2];
2654 }
2655 if ((pchan->protectflag & OB_LOCK_ROTZ) == 0) {
2656 pchan->quat[3] = quat[3];
2657 }
2658}
2659
2661{
2662 if ((pchan->protectflag & OB_LOCK_ROTX) == 0) {
2663 pchan->eul[0] = euler[0];
2664 }
2665 if ((pchan->protectflag & OB_LOCK_ROTY) == 0) {
2666 pchan->eul[1] = euler[1];
2667 }
2668 if ((pchan->protectflag & OB_LOCK_ROTZ) == 0) {
2669 pchan->eul[2] = euler[2];
2670 }
2671}
2672
2674 const float axis[3],
2675 float angle)
2676{
2677 if ((pchan->protectflag & OB_LOCK_ROTX) == 0) {
2678 pchan->rotAxis[0] = axis[0];
2679 }
2680 if ((pchan->protectflag & OB_LOCK_ROTY) == 0) {
2681 pchan->rotAxis[1] = axis[1];
2682 }
2683 if ((pchan->protectflag & OB_LOCK_ROTZ) == 0) {
2684 pchan->rotAxis[2] = axis[2];
2685 }
2686 if ((pchan->protectflag & OB_LOCK_ROTW) == 0) {
2687 pchan->rotAngle = angle;
2688 }
2689}
2690
2692
2693/* -------------------------------------------------------------------- */
2716
2717void mat3_to_vec_roll(const float mat[3][3], float r_vec[3], float *r_roll)
2718{
2719 if (r_vec) {
2720 copy_v3_v3(r_vec, mat[1]);
2721 }
2722
2723 if (r_roll) {
2724 mat3_vec_to_roll(mat, mat[1], r_roll);
2725 }
2726}
2727
2728void mat3_vec_to_roll(const float mat[3][3], const float vec[3], float *r_roll)
2729{
2730 float vecmat[3][3], vecmatinv[3][3], rollmat[3][3], q[4];
2731
2732 /* Compute the orientation relative to the vector with zero roll. */
2733 vec_roll_to_mat3(vec, 0.0f, vecmat);
2734 invert_m3_m3(vecmatinv, vecmat);
2735 mul_m3_m3m3(rollmat, vecmatinv, mat);
2736
2737 /* Extract the twist angle as the roll value. */
2738 mat3_to_quat(q, rollmat);
2739
2740 *r_roll = quat_split_swing_and_twist(q, 1, nullptr, nullptr);
2741}
2742
2743void vec_roll_to_mat3_normalized(const float nor[3], const float roll, float r_mat[3][3])
2744{
2817
2818 const float SAFE_THRESHOLD = 6.1e-3f; /* Theta above this value has good enough precision. */
2819 const float CRITICAL_THRESHOLD = 2.5e-4f; /* True singularity if XZ distance is below this. */
2820 const float THRESHOLD_SQUARED = CRITICAL_THRESHOLD * CRITICAL_THRESHOLD;
2821
2822 const float x = nor[0];
2823 const float y = nor[1];
2824 const float z = nor[2];
2825
2826 float theta = 1.0f + y; /* Remapping Y from [-1,+1] to [0,2]. */
2827 const float theta_alt = x * x + z * z; /* Squared distance from origin in x,z plane. */
2828 float rMatrix[3][3], bMatrix[3][3];
2829
2830 BLI_ASSERT_UNIT_V3(nor);
2831
2832 /* Determine if the input is far enough from the true singularity of this type of
2833 * transformation at (0,-1,0), where roll becomes 0/0 undefined without a limit.
2834 *
2835 * When theta is close to zero (nor is aligned close to negative Y Axis),
2836 * we have to check we do have non-null X/Z components as well.
2837 * Also, due to float precision errors, nor can be (0.0, -0.99999994, 0.0) which results
2838 * in theta being close to zero. This will cause problems when theta is used as divisor.
2839 */
2840 if (theta > SAFE_THRESHOLD || theta_alt > THRESHOLD_SQUARED) {
2841 /* nor is *not* aligned to negative Y-axis (0,-1,0). */
2842
2843 bMatrix[0][1] = -x;
2844 bMatrix[1][0] = x;
2845 bMatrix[1][1] = y;
2846 bMatrix[1][2] = z;
2847 bMatrix[2][1] = -z;
2848
2849 if (theta <= SAFE_THRESHOLD) {
2850 /* When nor is close to negative Y axis (0,-1,0) the theta precision is very bad,
2851 * so recompute it from x and z instead, using the series expansion for `sqrt`. */
2852 theta = theta_alt * 0.5f + theta_alt * theta_alt * 0.125f;
2853 }
2854
2855 bMatrix[0][0] = 1 - x * x / theta;
2856 bMatrix[2][2] = 1 - z * z / theta;
2857 bMatrix[2][0] = bMatrix[0][2] = -x * z / theta;
2858 }
2859 else {
2860 /* nor is very close to negative Y axis (0,-1,0): use simple symmetry by Z axis. */
2861 unit_m3(bMatrix);
2862 bMatrix[0][0] = bMatrix[1][1] = -1.0;
2863 }
2864
2865 /* Make Roll matrix */
2866 axis_angle_normalized_to_mat3(rMatrix, nor, roll);
2867
2868 /* Combine and output result */
2869 mul_m3_m3m3(r_mat, rMatrix, bMatrix);
2870}
2871
2872void vec_roll_to_mat3(const float vec[3], const float roll, float r_mat[3][3])
2873{
2874 float nor[3];
2875
2876 normalize_v3_v3(nor, vec);
2877 vec_roll_to_mat3_normalized(nor, roll, r_mat);
2878}
2879
2881
2882/* -------------------------------------------------------------------- */
2885
2886void BKE_armature_where_is_bone(Bone *bone, const Bone *bone_parent, const bool use_recursion)
2887{
2888 float vec[3];
2889
2890 /* Bone Space */
2891 sub_v3_v3v3(vec, bone->tail, bone->head);
2892 bone->length = len_v3(vec);
2893 vec_roll_to_mat3(vec, bone->roll, bone->bone_mat);
2894
2895 /* this is called on old file reading too... */
2896 if (bone->xwidth == 0.0f) {
2897 bone->xwidth = 0.1f;
2898 bone->zwidth = 0.1f;
2899 bone->segments = 1;
2900 }
2901
2902 if (bone_parent) {
2903 float offs_bone[4][4];
2904 /* yoffs(b-1) + root(b) + bonemat(b) */
2905 BKE_bone_offset_matrix_get(bone, offs_bone);
2906
2907 /* Compose the matrix for this bone. */
2908 mul_m4_m4m4(bone->arm_mat, bone_parent->arm_mat, offs_bone);
2909 }
2910 else {
2911 copy_m4_m3(bone->arm_mat, bone->bone_mat);
2912 copy_v3_v3(bone->arm_mat[3], bone->head);
2913 }
2914
2915 /* and the kiddies */
2916 if (use_recursion) {
2917 bone_parent = bone;
2918 for (bone = bone->childbase.first(); bone; bone = bone->next) {
2919 BKE_armature_where_is_bone(bone, bone_parent, use_recursion);
2920 }
2921 }
2922}
2923
2925{
2926 /* hierarchical from root to children */
2927 for (Bone &bone : arm->bonebase) {
2928 BKE_armature_where_is_bone(&bone, nullptr, true);
2929 }
2930}
2931
2933
2934/* -------------------------------------------------------------------- */
2937
2938static void rebuild_pose_from_armature(Object &pose_ob, const bArmature &armature)
2939{
2940 bPose *pose = pose_ob.pose;
2941 bPoseChannel *prev_pchan = nullptr;
2942 BKE_armature_foreach_bone(armature, [&](const int bone_index, const Bone &bone) {
2944 pchan->runtime.bone_index = bone_index;
2945
2946 /* Bones are visited depth-first, so the parent pchan is guaranteed to exist. */
2947 pchan->parent = bone.parent ? BKE_pose_channel_find_name(pose, bone.parent->name) : nullptr;
2948
2949 /* Prevent custom bone colors from having alpha zero.
2950 * Part of the fix for issue #115434. */
2951 pchan->color.custom.solid[3] = 255;
2952 pchan->color.custom.select[3] = 255;
2953 pchan->color.custom.active[3] = 255;
2954
2955 /* BONE_CONNECTED child tracking for b-bones. */
2956 if (bone.flag & BONE_CONNECTED && pchan->parent) {
2957 pchan->parent->child = pchan;
2958 }
2959
2960 /* Ensure that newly-created pose channels are inserted in the right order. This ensures the
2961 * pose channels are in the same order as the bones. See #82758. */
2962 if (pchan->prev != prev_pchan) {
2963 BLI_remlink(&pose->chanbase, pchan);
2964 BLI_insertlinkafter(&pose->chanbase, prev_pchan, pchan);
2965 }
2966
2967 prev_pchan = pchan;
2968 });
2969}
2970
2972{
2973 for (bPoseChannel &pchan : pose->chanbase) {
2974 pchan.runtime.bone_index = BONE_INDEX_UNKNOWN;
2975 pchan.child = nullptr;
2976 }
2977}
2978
2981{
2982 return (bone != nullptr) ? BKE_pose_channel_find_name(pose, bone->name) : nullptr;
2983}
2984
2990
2991void BKE_pose_channels_clear_with_null_bone(Object *armature_ob, const bool do_id_user)
2992{
2993 BLI_assert(armature_ob->pose);
2994 bPose *pose = armature_ob->pose;
2995 BKE_pose_ensure_bone_indices(*armature_ob);
2996 for (bPoseChannel &pchan : pose->chanbase.items_mutable()) {
2997 Bone *bone = pchan.bone_get(*armature_ob);
2998 if (bone == nullptr) {
2999 /* If `do_id_user` is false, we are working with copy on write data in which case we should
3000 * not be deleting any drivers of missing bones. See #158665. */
3001 if (do_id_user) {
3002 BKE_animdata_drivers_remove_for_rna_struct(armature_ob->id, *RNA_PoseBone, &pchan);
3003 }
3004 BKE_pose_channel_free_ex(&pchan, do_id_user);
3006 BLI_freelinkN(&pose->chanbase, &pchan);
3007 }
3008 }
3009}
3010
3011void BKE_pose_rebuild(Main *bmain, Object *ob, bArmature *arm, const bool do_id_user)
3012{
3013 bPose *pose;
3014
3015 /* only done here */
3016 if (ob->pose == nullptr) {
3017 /* create new pose */
3018 ob->pose = MEM_new<bPose>("new pose");
3019 ob->pose->runtime = MEM_new<bke::bPoseRuntime>(__func__);
3020
3021 /* set default settings for animviz */
3023 }
3024 pose = ob->pose;
3025
3026 /* clear */
3028
3029 /* First step, ensure that all pose channels are there. */
3030 rebuild_pose_from_armature(*ob, *arm);
3031
3032 /* and a check for garbage */
3034
3036
3037 for (bPoseChannel &pchan : pose->chanbase) {
3038 /* Find the custom B-Bone handles. */
3039 BKE_pchan_rebuild_bbone_handles(pose, {&pchan, pchan.bone_get(*arm)});
3040 /* Re-validate that we are still using a valid pchan form custom transform. */
3041 /* Note that we could store pointers of freed pchan in a set to speed this up, however this
3042 * is supposed to be a rarely used feature, so for now assuming that always building that
3043 * set would be less optimal. */
3044 if (pchan.custom_tx != nullptr && BLI_findindex(&pose->chanbase, pchan.custom_tx) == -1) {
3045 pchan.custom_tx = nullptr;
3046 }
3047 }
3048
3049 BKE_pose_update_constraint_flags(*ob); /* for IK detection for example */
3050
3051 pose->flag &= ~POSE_RECALC;
3052 pose->flag |= POSE_WAS_REBUILT;
3053
3054 /* Rebuilding poses forces us to also rebuild the dependency graph,
3055 * since there is one node per pose/bone. */
3056 if (bmain != nullptr) {
3058 }
3059}
3060
3061void BKE_pose_ensure(Main *bmain, Object *ob, bArmature *arm, const bool do_id_user)
3062{
3063 BLI_assert(!ELEM(nullptr, arm, ob));
3064 if (ob->type == OB_ARMATURE && ((ob->pose == nullptr) || (ob->pose->flag & POSE_RECALC))) {
3065 BLI_assert(GS(arm->id.name) == ID_AR);
3066 BKE_pose_rebuild(bmain, ob, arm, do_id_user);
3067 }
3068}
3069
3077static void rebuild_pose_bone_indices(const Object &pose_ob)
3078{
3079 const bArmature &armature = *id_cast<const bArmature *>(pose_ob.data);
3080 const bPose *pose = pose_ob.pose;
3081
3082 for (bPoseChannel &pchan : pose->chanbase) {
3083 pchan.runtime.bone_index = BONE_INDEX_UNKNOWN;
3084 }
3085
3086 BKE_armature_foreach_bone(armature, [&pose](const int index, const Bone &bone) {
3088 if (!pchan) {
3089 return;
3090 }
3091 pchan->runtime.bone_index = index;
3092 });
3093
3094 /* If the Armature's bone array needs rebuilding, this has to happen before we grab the
3095 * bones_generation_count value. Otherwise the current indices will be invalidated immediately on
3096 * the next use. */
3097 if (!armature.runtime->is_bones_array_valid()) {
3098 rebuild_bone_array(const_cast<bArmature &>(armature));
3099 }
3101}
3102
3104{
3105 BLI_assert(pose_ob.type == OB_ARMATURE);
3106 BLI_assert(GS(pose_ob.data->name) == ID_AR);
3107 bArmature *armature = id_cast<bArmature *>(pose_ob.data);
3108
3109 const uint64_t armature_generation_count = armature->runtime->bones_generation_count;
3110 const uint64_t object_generation_count = pose_ob.runtime->pose_bones_generation_count;
3111 if (armature_generation_count != object_generation_count ||
3112 !armature->runtime->is_bones_array_valid())
3113 {
3115 }
3116}
3117
3119
3120/* -------------------------------------------------------------------- */
3123
3124void BKE_pchan_to_mat4(const bke::PChanBoneConst pchanbone, float r_chanmat[4][4])
3125{
3126 float smat[3][3];
3127 float rmat[3][3];
3128 float tmat[3][3];
3129
3130 const bPoseChannel *pchan = pchanbone.pchan;
3131 const Bone *bone = pchanbone.bone;
3132
3133 /* get scaling matrix */
3134 size_to_mat3(smat, pchan->scale);
3135
3136 /* get rotation matrix */
3137 BKE_pchan_rot_to_mat3(pchan, rmat);
3138
3139 /* calculate matrix of bone (as 3x3 matrix, but then copy the 4x4) */
3140 mul_m3_m3m3(tmat, rmat, smat);
3141 copy_m4_m3(r_chanmat, tmat);
3142
3143 /* prevent action channels breaking chains */
3144 /* need to check for bone here, CONSTRAINT_TYPE_ACTION uses this call */
3145 if ((bone == nullptr) || !(bone->flag & BONE_CONNECTED)) {
3146 copy_v3_v3(r_chanmat[3], pchan->loc);
3147 }
3148}
3149
3151{
3152 /* this is just a wrapper around the copy of this function which calculates the matrix
3153 * and stores the result in any given channel
3154 */
3155 BKE_pchan_to_mat4(pchanbone, pchanbone.pchan->chan_mat);
3156}
3157
3159{
3160 float vec[3];
3161
3162 copy_v3_v3(vec, pchanbone.pchan->pose_mat[1]);
3163 mul_v3_fl(vec, pchanbone.bone->length);
3164 add_v3_v3v3(pchanbone.pchan->pose_tail, pchanbone.pchan->pose_head, vec);
3165}
3166
3167void BKE_pose_where_is_bone(Depsgraph *depsgraph,
3168 Scene *scene,
3169 Object *ob,
3170 bPoseChannel *pchan,
3171 float ctime,
3172 bool do_extra)
3173{
3174 /* This gives a chan_mat with actions (F-Curve) results. */
3175 Bone *bone = pchan->bone_get(*ob);
3176 if (do_extra) {
3177 BKE_pchan_calc_mat({pchan, bone});
3178 }
3179 else {
3180 unit_m4(pchan->chan_mat);
3181 }
3182
3183 /* Construct the posemat based on PoseChannels, that we do before applying constraints. */
3184 /* pose_mat(b) = pose_mat(b-1) * yoffs(b-1) * d_root(b) * bone_mat(b) * chan_mat(b) */
3185 BKE_armature_mat_bone_to_pose({pchan, bone}, pchan->chan_mat, pchan->pose_mat);
3186
3187 /* Only root-bones get the cyclic offset (unless user doesn't want that). */
3188 /* XXX That could be a problem for snapping and other "reverse transform" features... */
3189 if (!pchan->parent) {
3190 if ((bone->flag & BONE_NO_CYCLICOFFSET) == 0) {
3191 add_v3_v3(pchan->pose_mat[3], ob->pose->cyclic_offset);
3192 }
3193 }
3194
3195 if (do_extra) {
3196 /* Do constraints */
3197 if (pchan->constraints.first()) {
3198 bConstraintOb *cob;
3199 float vec[3];
3200
3201 /* make a copy of location of PoseChannel for later */
3202 copy_v3_v3(vec, pchan->pose_mat[3]);
3203
3204 /* prepare PoseChannel for Constraint solving
3205 * - makes a copy of matrix, and creates temporary struct to use
3206 */
3207 cob = BKE_constraints_make_evalob(depsgraph, scene, ob, pchan, CONSTRAINT_OBTYPE_BONE);
3208
3209 /* Solve PoseChannel's Constraints */
3210
3211 /* ctime doesn't alter objects. */
3212 BKE_constraints_solve(depsgraph, &pchan->constraints, cob, ctime);
3213
3214 /* cleanup after Constraint Solving
3215 * - applies matrix back to pchan, and frees temporary struct used
3216 */
3218
3219 /* prevent constraints breaking a chain */
3220 if (bone->flag & BONE_CONNECTED) {
3221 copy_v3_v3(pchan->pose_mat[3], vec);
3222 }
3223 }
3224 }
3225
3226 /* calculate head */
3227 copy_v3_v3(pchan->pose_head, pchan->pose_mat[3]);
3228 /* calculate tail */
3229 BKE_pose_where_is_bone_tail({pchan, bone});
3230}
3231
3232void BKE_pose_where_is(Depsgraph *depsgraph, Scene *scene, Object *ob)
3233{
3234 bArmature *arm;
3235 float imat[4][4];
3236 float ctime;
3237
3238 if (ob->type != OB_ARMATURE) {
3239 return;
3240 }
3241 arm = id_cast<bArmature *>(ob->data);
3242
3243 if (ELEM(nullptr, arm, scene)) {
3244 return;
3245 }
3246 /* WARNING! passing nullptr bmain here means we won't tag depsgraph's as dirty -
3247 * hopefully this is OK. */
3248 BKE_pose_ensure(nullptr, ob, arm, true);
3249
3250 ctime = BKE_scene_ctime_get(scene); /* not accurate... */
3251
3252 /* In edit-mode or rest-position we read the data from the bones. */
3253 if (arm->edbo || (arm->flag & ARM_RESTPOS)) {
3254 for (bPoseChannel &pchan : ob->pose->chanbase) {
3255 const Bone *bone = pchan.bone_get(*arm);
3256 if (bone) {
3257 copy_m4_m4(pchan.pose_mat, bone->arm_mat);
3258 copy_v3_v3(pchan.pose_head, bone->arm_head);
3259 copy_v3_v3(pchan.pose_tail, bone->arm_tail);
3260 }
3261 }
3262 }
3263 else {
3264 invert_m4_m4(ob->runtime->world_to_object.ptr(), ob->object_to_world().ptr());
3265
3266 /* 1. clear flags */
3267 for (bPoseChannel &pchan : ob->pose->chanbase) {
3269 }
3270
3271 /* 2a. construct the IK tree (standard IK) */
3272 BIK_init_tree(depsgraph, scene, ob, ctime);
3273
3274 /* 2b. construct the Spline IK trees
3275 * - this is not integrated as an IK plugin, since it should be able
3276 * to function in conjunction with standard IK
3277 */
3278 BKE_pose_splineik_init_tree(scene, ob, ctime);
3279
3280 /* 3. the main loop, channels are already hierarchical sorted from root to children */
3281 for (bPoseChannel &pchan : ob->pose->chanbase) {
3282 /* 4a. if we find an IK root, we handle it separated */
3283 if (pchan.flag & POSE_IKTREE) {
3284 BIK_execute_tree(depsgraph, scene, ob, &pchan, ctime);
3285 }
3286 /* 4b. if we find a Spline IK root, we handle it separated too */
3287 else if (pchan.flag & POSE_IKSPLINE) {
3288 BKE_splineik_execute_tree(depsgraph, scene, ob, &pchan, ctime);
3289 }
3290 /* 5. otherwise just call the normal solver */
3291 else if (!(pchan.flag & POSE_DONE)) {
3292 BKE_pose_where_is_bone(depsgraph, scene, ob, &pchan, ctime, true);
3293 }
3294 }
3295 /* 6. release the IK tree */
3296 BIK_release_tree(scene, ob, ctime);
3297 }
3298
3299 /* calculating deform matrices */
3300 for (bPoseChannel &pchan : ob->pose->chanbase) {
3301 const Bone *bone = pchan.bone_get(*arm);
3302 if (bone) {
3303 invert_m4_m4(imat, bone->arm_mat);
3304 mul_m4_m4m4(pchan.chan_mat, pchan.pose_mat, imat);
3305 }
3306 }
3307}
3308
3310
3311/* -------------------------------------------------------------------- */
3314
3315std::optional<Bounds<float3>> BKE_armature_min_max(const Object *ob)
3316{
3317 return BKE_pose_minmax(ob, false);
3318}
3319
3321 const bke::PChanBoneConst pchanbone,
3322 const bool use_empty_drawtype,
3323 float3 &r_min,
3324 float3 &r_max)
3325{
3326 const bPoseChannel *pchan = pchanbone.pchan;
3327 const bArmature *arm = id_cast<const bArmature *>(ob->data);
3328
3329 Object *ob_custom = nullptr;
3330 if (!(arm->flag & ARM_NO_CUSTOM) && pchan->custom) {
3331 /* This should not be possible, protected against in RNA code and
3332 * BKE_pose_blend_read_after_liblink(). Just for safety do another check
3333 * here, as otherwise this code can end in an infinite loop. */
3334 BLI_assert(pchan->custom->type != OB_ARMATURE);
3335
3336 if (pchan->custom->type != OB_ARMATURE) {
3337 ob_custom = pchan->custom;
3338 }
3339 }
3340
3341 const bPoseChannel *pchan_tx = (ob_custom && pchan->custom_tx) ? pchan->custom_tx : pchan;
3342
3343 std::optional<Bounds<float3>> bb_custom;
3344 if (ob_custom) {
3345 float3 min, max;
3346 if (use_empty_drawtype && (ob_custom->type == OB_EMPTY) &&
3348 {
3349 bb_custom.emplace(Bounds<float3>{min, max});
3350 }
3351 else {
3352 bb_custom = BKE_object_boundbox_get(ob_custom);
3353 }
3354 }
3355
3356 if (bb_custom) {
3357 float4x4 mat, smat, rmat, tmp;
3358 scale_m4_fl(smat.ptr(), PCHAN_CUSTOM_BONE_LENGTH(pchanbone));
3359 rescale_m4(smat.ptr(), pchan->custom_scale_xyz);
3361 copy_m4_m4(tmp.ptr(), pchan_tx->pose_mat);
3362 translate_m4(tmp.ptr(),
3363 pchan->custom_translation[0],
3364 pchan->custom_translation[1],
3365 pchan->custom_translation[2]);
3366 mul_m4_series(mat.ptr(), tmp.ptr(), rmat.ptr(), smat.ptr());
3367 const Bounds tranformed_bounds = bounds::transform_bounds(mat, *bb_custom);
3368 r_min = math::min(tranformed_bounds.min, r_min);
3369 r_max = math::max(tranformed_bounds.max, r_max);
3370 }
3371 else {
3372 minmax_v3v3_v3(r_min, r_max, pchan_tx->pose_head);
3373 minmax_v3v3_v3(r_min, r_max, pchan_tx->pose_tail);
3374 }
3375}
3376
3377std::optional<Bounds<float3>> BKE_pose_minmax(const Object *ob, const bool use_select)
3378{
3379 if (!ob->pose) {
3380 return std::nullopt;
3381 }
3382
3383 float3 min(std::numeric_limits<float>::max());
3384 float3 max(std::numeric_limits<float>::lowest());
3385
3386 BLI_assert(ob->type == OB_ARMATURE);
3387 const bArmature *arm = id_cast<const bArmature *>(ob->data);
3388
3389 bool found_pchan = false;
3390 for (const bPoseChannel &pchan : ob->pose->chanbase) {
3391 /* XXX pchan->bone_get(*ob) may be nullptr for duplicated bones, see
3392 * duplicateEditBoneObjects() comment (editarmature.c:2592)... Skip in this case too! */
3393 const Bone *bone = pchan.bone_get(*ob);
3394 if (!bone) {
3395 continue;
3396 }
3397 /* Despite `bone_is_selected` also checking for visibility we need to check visibility
3398 * manually due to `use_select` potentially ignoring selection state. */
3399 if (!animrig::bone_is_visible(arm, {&pchan, bone})) {
3400 continue;
3401 }
3402 if (use_select && !animrig::bone_is_selected(arm, {&pchan, bone})) {
3403 continue;
3404 }
3405
3406 BKE_pchan_minmax(ob, {&pchan, bone}, false, min, max);
3407 found_pchan = true;
3408 }
3409
3410 if (!found_pchan) {
3411 return std::nullopt;
3412 }
3413
3414 return Bounds<float3>(min, max);
3415}
3416
3418
3419/* -------------------------------------------------------------------- */
3422
3424{
3425 bPoseChannel *rootchan = pchan;
3426 if (!(data->flag & CONSTRAINT_IK_TIP)) {
3427 /* Exclude tip from chain. */
3428 rootchan = rootchan->parent;
3429 }
3430 if (rootchan != nullptr) {
3431 int segcount = 0;
3432 while (rootchan->parent) {
3433 /* Continue up chain, until we reach target number of items. */
3434 segcount++;
3435 if (segcount == data->rootbone) {
3436 break;
3437 }
3438 rootchan = rootchan->parent;
3439 }
3440 }
3441 return rootchan;
3442}
3443
3446{
3447 bPoseChannel *rootchan = pchan;
3448 int segcount = 0;
3449 BLI_assert(rootchan != nullptr);
3450 while (rootchan->parent) {
3451 /* Continue up chain, until we reach target number of items. */
3452 segcount++;
3453 if (segcount == data->chainlen) {
3454 break;
3455 }
3456 rootchan = rootchan->parent;
3457 }
3458 return rootchan;
3459}
3460
3462
3463/* -------------------------------------------------------------------- */
3466
3467Span<const BoneCollection *> bArmature::collections_span() const
3468{
3470}
3471
3472Span<BoneCollection *> bArmature::collections_span()
3473{
3475}
3476
3477Span<const BoneCollection *> bArmature::collections_roots() const
3478{
3480}
3481Span<BoneCollection *> bArmature::collections_roots()
3482{
3484}
3485
3486Span<const BoneCollection *> bArmature::collection_children(const BoneCollection *parent) const
3487{
3488 return Span(&collection_array[parent->child_index], parent->child_count);
3489}
3490
3491Span<BoneCollection *> bArmature::collection_children(BoneCollection *parent)
3492{
3493 return Span(&collection_array[parent->child_index], parent->child_count);
3494}
3495
3496bool BoneCollection::is_visible() const
3497{
3498 return this->flags & BONE_COLLECTION_VISIBLE;
3499}
3500bool BoneCollection::is_visible_ancestors() const
3501{
3503}
3504bool BoneCollection::is_visible_with_ancestors() const
3505{
3506 return this->is_visible() && this->is_visible_ancestors();
3507}
3508bool BoneCollection::is_solo() const
3509{
3510 return this->flags & BONE_COLLECTION_SOLO;
3511}
3512bool BoneCollection::is_expanded() const
3513{
3514 return this->flags & BONE_COLLECTION_EXPANDED;
3515}
3516
3520static void rebuild_bone_array(bArmature &armature)
3521{
3522 bke::bArmature_Runtime &runtime = *armature.runtime;
3523 std::scoped_lock lock{runtime.bones_mutex};
3524
3525 /* Re-check the reason this function was called, now that the lock has been obtained. */
3526 if (runtime.is_bones_array_valid()) {
3527 return;
3528 }
3529
3530 const int num_bones = BKE_armature_bonelist_count(&armature.bonebase);
3531
3532 Array<Bone *> bones(num_bones);
3533
3534 BKE_armature_foreach_bone(armature, [&](const int bone_index, const Bone &bone) {
3535 /* const_cast: the bone ref is const because BKE_armature_foreach_bone() is only
3536 * implemented for const types. */
3537 bones[bone_index] = const_cast<Bone *>(&bone);
3538 });
3539
3540 runtime.bones = std::move(bones);
3541 runtime.bones_generation_count++;
3542}
3543
3545{
3546 this->bones.clear_without_destruct();
3547}
3548
3550{
3551 return !this->bones.is_empty();
3552}
3553
3554const Bone *bArmature::bone_get_indexed(const int64_t bone_index) const
3555{
3556 /* The logic 'if runtime->bones is empty, the array needs rebuilding' is only valid when calling
3557 * this function implies there is at least one bone. */
3558 BLI_assert(!this->bonebase.is_empty());
3559 if (this->bonebase.is_empty()) {
3560 return nullptr;
3561 }
3562
3563 if (!this->runtime->is_bones_array_valid()) {
3564 /* const_cast: allow the function to write to the runtime data. */
3565 rebuild_bone_array(const_cast<bArmature &>(*this));
3566 }
3567
3568 BLI_assert(bone_index >= 0);
3569 BLI_assert(bone_index < this->runtime->bones.size());
3570 return this->runtime->bones[bone_index];
3571}
3572
3573Bone *bArmature::bone_get_indexed(const int64_t bone_index)
3574{
3575 return const_cast<Bone *>(std::as_const(*this).bone_get_indexed(bone_index));
3576}
3577
3579
3580static int visit_bone_and_descendants(const Bone &bone,
3581 int bone_index,
3582 const ForeachBoneFn callback)
3583{
3584 callback(bone_index++, bone);
3585 for (const Bone &child : bone.childbase) {
3586 bone_index = visit_bone_and_descendants(child, bone_index, callback);
3587 }
3588 return bone_index;
3589}
3590
3591void BKE_armature_foreach_bone(const bArmature &armature, const ForeachBoneFn callback)
3592{
3593 int bone_index = 0;
3594 for (const Bone &bone : armature.bonebase) {
3595 bone_index = visit_bone_and_descendants(bone, bone_index, callback);
3596 }
3597
3598 BLI_assert(bone_index == BKE_armature_bonelist_count(&armature.bonebase));
3599}
3600
3601} // namespace blender
Functions to deal with Armatures.
C++ functions to deal with Armature collections (i.e. the successor of bone layers).
Blender kernel action and pose functionality.
#define MAX_BBONE_SUBDIV
#define IDP_BlendDataRead(reader, prop)
#define MAIN_ID_SESSION_UID_UNSET
#define BKE_LIB_FOREACHID_PROCESS_FUNCTION_CALL(data_, func_call_)
General operations, lookup, etc. for blender objects.
#define BLI_array_alloca(arr, realsize)
Definition BLI_alloca.hh:18
#define BLI_ASSERT_UNIT_M3(m)
#define BLI_ASSERT_UNIT_V3(v)
#define mul_m4_series(...)
#define STRNCPY_UTF8_RLEN(dst, src)
#define STRNCPY_UTF8(dst, src)
#define BLO_read_struct_list(reader, struct_name, list)
#define BLO_read_struct(reader, struct_name, ptr_p)
#define BLT_I18NCONTEXT_ID_ARMATURE
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define FILTER_ID_ALL
Definition DNA_ID.h:1241
#define ID_IS_LINKED(_id)
Definition DNA_ID.h:697
#define FILTER_ID_AR
Definition DNA_ID.h:1200
#define PCHAN_CUSTOM_BONE_LENGTH(pchanbone)
#define MAXBONENAME
These structs are the foundation for all linked lists in the library system.
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
volatile int lock
#define CLAMP(a, b, c)
#define CLAMP_MAX(a, c)
#define BLI_assert_unreachable()
#define BLI_assert(a)
#define INIT_DEFAULT_STRUCT_AFTER(struct_dst, member)
#define ELEM(...)
#define IS_EQF(a, b)
#define STREQ(a, b)
#define CLAMP_MIN(a, b)
ccl_device_inline float arc_length(const float e2, const float gamma)
btScalar max
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr Span()=default
constexpr StringRefNull()
void append(const T &value)
#define logf(x)
#define expf(x)
#define powf(x, y)
#define rot(x, k)
float length(VecOp< float, D >) RET
float3x3 scale(float3x3 mat, float2 scale)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
bool bone_is_visible(const bArmature *armature, const Bone *bone)
void bonecolls_copy_expanded_flag(Span< BoneCollection * > bcolls_dest, Span< const BoneCollection * > bcolls_source)
bool bone_is_selected(const bArmature *armature, const Bone *bone)
void settings_init(struct bAnimVizSettings *avs)
PChanBoneT< bPoseChannel, Bone > PChanBone
Definition BKE_pose.hh:88
PChanBoneT< const bPoseChannel, const Bone > PChanBoneConst
Definition BKE_pose.hh:89
Bounds< VecBase< T, 3 > > transform_bounds(const MatBase< T, D, D > &matrix, const Bounds< VecBase< T, 3 > > &bounds)
internal::ShallowDataConstRef< T > shallow_copy(const T &other)
Definition DNA_defs.h:147
T min(const T &a, const T &b)
T max(const T &a, const T &b)
BMesh Iterators.
static void rebuild_pose_from_armature(Object &pose_ob, const bArmature &armature)
MINLINE void madd_v3_v3v3fl(float r[3], const float a[3], const float b[3], float f)
bPoseChannel * BKE_pose_channel_ensure(bPose *pose, const char *name) ATTR_NONNULL(2)
void BIK_execute_tree(struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob, struct bPoseChannel *pchan, float ctime)
void ANIM_armature_runtime_free(bArmature *armature)
static void ease_handle_axis(const float deriv1[3], const float deriv2[3], float r_axis[3])
static void armature_copy_data(Main *, std::optional< Library * >, ID *id_dst, const ID *id_src, const int flag)
void mat3_vec_to_roll(const float mat[3][3], const float vec[3], float *r_roll)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void BKE_bone_parent_transform_combine(const BoneParentTransform *in1, const BoneParentTransform *in2, BoneParentTransform *result)
size_t size_t size_t BLI_snprintf_utf8(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
void BKE_pchan_calc_mat(bke::PChanBone pchanbone)
bArmature * BKE_armature_from_object(Object *ob)
unsigned __int64 uint64_t
Definition BKE_text.h:91
void BKE_pchan_mat3_to_rot(bPoseChannel *pchan, const float mat[3][3], bool use_compat)
__int64 int64_t
Definition BKE_text.h:90
bArmature * BKE_armature_add(Main *bmain, const char *name)
void IDP_FreeProperty_ex(IDProperty *prop, bool do_id_user)
Definition idprop.cc:1398
static void copy_bone_collection(bArmature *armature_dst, BoneCollection *&bcoll_dst, const BoneCollection *bcoll_src, const int lib_id_flag)
void mat4_to_loc_rot_size(float loc[3], float rot[3][3], float size[3], const float wmat[4][4])
static void armature_init_data(ID *id)
static void armature_undo_preserve(BlendLibReader *, ID *id_new, ID *id_old)
void vec_roll_to_mat3(const float vec[3], float roll, float r_mat[3][3])
void BKE_pose_rebuild(Main *bmain, Object *ob, bArmature *arm, bool do_id_user)
void axis_angle_to_eulO(float eul[3], short order, const float axis[3], float angle)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void copy_m3_m4(float m1[3][3], const float m2[4][4])
void mat3_to_vec_roll(const float mat[3][3], float r_vec[3], float *r_roll)
void BKE_pose_channel_free_bbone_cache(bPoseChannel_Runtime *runtime) ATTR_NONNULL(1)
void mat3_normalized_to_axis_angle(float axis[3], float *angle, const float mat[3][3])
float quat_split_swing_and_twist(const float q_in[4], int axis, float r_swing[4], float r_twist[4])
void minmax_v3v3_v3(float min[3], float max[3], const float vec[3])
void BKE_constraints_solve(struct Depsgraph *depsgraph, ListBaseT< bConstraint > *conlist, struct bConstraintOb *cob, float ctime)
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
void copy_m3_m3(float m1[3][3], const float m2[3][3])
void ANIM_armature_bonecoll_active_index_set(bArmature *armature, int bone_collection_index)
void * BLI_ghash_lookup(const GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition BLI_ghash.cc:735
void void void void void void BLI_duplicatelist(ListBase *dst, const ListBase *src) ATTR_NONNULL(1
Definition listbase.cc:834
void copy_m4_m3(float m1[4][4], const float m2[3][3])
void mat3_normalized_to_quat(float q[4], const float mat[3][3])
static void armature_bone_from_name_insert_recursive(GHash *bone_hash, ListBaseT< Bone > *lb)
void BKE_pose_ensure(Main *bmain, Object *ob, bArmature *arm, bool do_id_user)
float mat4_to_scale(const float mat[4][4])
bool invert_m3_m3(float inverse[3][3], const float mat[3][3])
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
struct bConstraintOb * BKE_constraints_make_evalob(struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob, void *subdata, short datatype)
void BKE_armature_foreach_bone(const bArmature &armature, ForeachBoneFn callback)
void BKE_pose_channels_hash_free(bPose *pose) ATTR_NONNULL(1)
void normalize_m3(float R[3][3]) ATTR_NONNULL()
static void copy_bonechildren_custom_handles(Bone *bone_dst, bArmature *arm_dst)
void BKE_pchan_protected_rotation_quaternion_set(bPoseChannel *pchan, const float quat[4])
float normalize_qt(float q[4])
void IDP_BlendWrite(BlendWriter *writer, const IDProperty *prop)
Definition idprop.cc:1696
void BKE_pchan_bbone_handles_get(bke::PChanBoneConst pchanbone, bPoseChannel **r_prev, bPoseChannel **r_next)
void BKE_pchan_bbone_deform_clamp_segment_index(const Bone &bone, float head_tail, int *r_index, float *r_blend_next)
static void direct_link_bones(BlendDataReader *reader, Bone *bone, bool *all_bones_are_named)
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
void IDP_foreach_property(IDProperty *id_property_root, int type_filter, FunctionRef< void(IDProperty *id_property)> callback)
void BKE_pose_where_is_bone(Depsgraph *depsgraph, Scene *scene, Object *ob, bPoseChannel *pchan, float ctime, bool do_extra)
void BKE_pchan_protected_location_set(bPoseChannel *pchan, const float location[3])
std::optional< Bounds< float3 > > BKE_pose_minmax(const Object *ob, bool use_select)
static Bone * get_named_bone_bonechildren(ListBaseT< Bone > *lb, const char *name)
static void armature_foreach_id_editbone(EditBone *edit_bone, LibraryForeachIDData *data)
void mul_mat3_m4_fl(float R[4][4], float f)
void BKE_pose_clear_pointers(bPose *pose)
void mat3_to_compatible_eulO(float eul[3], const float oldrot[3], short order, const float mat[3][3])
void axis_angle_to_mat3(float R[3][3], const float axis[3], float angle)
void BKE_splineik_execute_tree(Depsgraph *depsgraph, Scene *scene, Object *ob, bPoseChannel *pchan_root, float ctime)
void mul_v3_mat3_m4v3(float r[3], const float mat[4][4], const float vec[3])
void axis_angle_to_quat(float r[4], const float axis[3], float angle)
void BKE_pose_channels_hash_ensure(bPose *pose) ATTR_NONNULL(1)
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
void BKE_pchan_protected_scale_set(bPoseChannel *pchan, const float scale[3])
bool invert_m4(float mat[4][4])
void translate_m4(float mat[4][4], float Tx, float Ty, float Tz)
void BKE_pchan_quat_to_rot(bPoseChannel &pchan, const float4 &quat)
void BKE_pchan_to_mat4(bke::PChanBoneConst pchanbone, float r_chanmat[4][4])
void BKE_armature_loc_world_to_pose(Object *ob, const float inloc[3], float outloc[3])
void BKE_pose_where_is(Depsgraph *depsgraph, Scene *scene, Object *ob)
MatBase< float, 4, 4 > float4x4
void mat3_normalized_to_compatible_eulO(float eul[3], const float oldrot[3], short order, const float mat[3][3])
void ANIM_bonecoll_free(BoneCollection *bcoll, bool do_id_user_count=true)
std::optional< Bounds< float3 > > BKE_armature_min_max(const Object *ob)
size_t size_t size_t bool BLI_str_utf8_truncate_at_size(char *str, const size_t str_size)
void BKE_armature_bone_hash_free(bArmature *arm)
void BKE_pose_ensure_bone_indices(const Object &pose_object)
void BKE_id_blend_write(BlendWriter *writer, ID *id)
Definition lib_id.cc:2890
void BKE_armature_mat_bone_to_pose(bke::PChanBoneConst pchanbone, const float inmat[4][4], float outmat[4][4])
static void equalize_cubic_bezier(const float control[4][3], int temp_segments, int final_segments, const float *segment_scales, float *r_t_points)
@ LIB_ID_CREATE_NO_USER_REFCOUNT
@ LIB_ID_COPY_SET_COPIED_ON_WRITE
MINLINE void mul_v3_fl(float r[3], float f)
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition BLI_ghash.cc:711
void normalize_m4(float R[4][4]) ATTR_NONNULL()
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 scalez, float result[4][4])
MINLINE void invert_v3_safe(float r[3])
void BKE_bone_parent_transform_apply(const BoneParentTransform *bpt, const float inmat[4][4], float outmat[4][4])
void BKE_pchan_bbone_segments_cache_copy(bPoseChannel *pchan, bPoseChannel *pchan_from)
VecBase< float, 4 > float4
void eulO_to_mat3(float M[3][3], const float e[3], short order)
void BKE_armature_editbonelist_free(ListBaseT< EditBone > *lb, bool do_id_user)
void BKE_bone_parent_transform_clear(BoneParentTransform *bpt)
void mul_m4_v3(const float M[4][4], float r[3])
void quat_to_mat3(float m[3][3], const float q[4])
void BKE_pchan_minmax(const Object *ob, bke::PChanBoneConst pchanbone, bool use_empty_drawtype, float3 &r_min, float3 &r_max)
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
bool bone_autoside_name(char name[64], int strip_number, short axis, float head, float tail)
void BKE_pchan_rot_to_mat3(const bPoseChannel *pchan, float r_mat[3][3])
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
void BKE_pchan_bbone_spline_setup(bke::PChanBone pchanbone, const bArmature &armature, bool rest, bool for_deform, Mat4 *result_array)
void BKE_pchan_protected_rotation_set(bPoseChannel *pchan, const float mat[3][3])
void size_to_mat4(float R[4][4], const float size[3])
static GHash * armature_bone_from_name_map(bArmature *arm)
static void read_bone_collections(BlendDataReader *reader, bArmature *arm)
MINLINE void mul_v3_v3(float r[3], const float a[3])
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)
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition BLI_ghash.cc:866
void eulO_to_axis_angle(float axis[3], float *angle, const float eul[3], short order)
void BKE_armature_mat_pose_to_bone(bke::PChanBoneConst pchanbone, const float inmat[4][4], float outmat[4][4])
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void BKE_pchan_bbone_deform_segment_index(bke::PChanBoneConst pchanbone, const float *co, int *r_index, float *r_blend_next)
float4 BKE_pchan_rot_to_quat(const bPoseChannel &pchan)
BMesh const char void * data
void BLI_insertlinkafter(ListBase *listbase, void *vprevlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:339
void BKE_armature_loc_pose_to_bone(bke::PChanBoneConst pchanbone, const float inloc[3], float outloc[3])
static void fix_empty_bone_names(bArmature &armature)
int BKE_pchan_bbone_spline_compute(BBoneSplineParameters *param, bool for_deform, Mat4 *result_array)
void BKE_pchan_apply_mat4(bPoseChannel *pchan, const float mat[4][4], bool use_compat)
void BKE_pose_channels_clear_with_null_bone(Object *armature_ob, bool do_id_user)
static void armature_blend_read_data(BlendDataReader *reader, ID *id)
void quat_to_eulO(float e[3], short order, const float q[4])
static int visit_bone_and_descendants(const Bone &bone, int bone_index, const ForeachBoneFn callback)
static void armature_foreach_id_bone_collection(BoneCollection *bcoll, LibraryForeachIDData *data)
void BKE_bone_parent_transform_invert(BoneParentTransform *bpt)
void BIK_init_tree(struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob, float ctime)
void BLI_freelinkN(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:277
void quat_to_axis_angle(float axis[3], float *angle, const float q[4])
MINLINE void negate_v3(float r[3])
static void armature_free_data(ID *id)
void BKE_pchan_protected_rotation_euler_set(bPoseChannel *pchan, const float rotation_euler[3])
static void evaluate_cubic_bezier(const float control[4][3], float t, float r_pos[3], float r_tangent[3])
void BKE_pose_channel_free_ex(bPoseChannel *pchan, bool do_id_user) ATTR_NONNULL(1)
void unit_m4(float m[4][4])
void BKE_pchan_bbone_spline_params_get(bke::PChanBoneConst pchanbone, const bArmature &armature, bool rest, BBoneSplineParameters *param)
void BKE_armature_where_is(bArmature *arm)
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
void BKE_pose_update_constraint_flags(Object &pose_ob)
void normalize_m4_ex(float R[4][4], float r_scale[3]) ATTR_NONNULL()
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
void BKE_armature_mat_world_to_pose(Object *ob, const float inmat[4][4], float outmat[4][4])
GHash * BLI_ghash_str_new_ex(const char *info, unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
void mat3_to_axis_angle(float axis[3], float *angle, const float mat[3][3])
MINLINE void zero_v3(float r[3])
void mat4_to_size_fix_shear(float size[3], const float M[4][4])
static void armature_transform_recurse(ListBaseT< Bone > *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])
void BKE_armature_where_is_bone(Bone *bone, const Bone *bone_parent, bool use_recursion)
void BKE_armature_mat_pose_to_bone_ex(Depsgraph *depsgraph, Object *ob, const bPoseChannel *pchan, const float inmat[4][4], float outmat[4][4])
void BKE_armature_mat_pose_to_delta(float delta_mat[4][4], float pose_mat[4][4], float arm_mat[4][4])
bPoseChannel * BKE_armature_splineik_solver_find_root(bPoseChannel *pchan, bSplineIKConstraint *data)
static void find_bbone_segment_index_curved(const bke::PChanBoneConst pchanbone, const float *co, int *r_index, float *r_blend_next)
static void write_bone_collection(BlendWriter *writer, BoneCollection *bcoll)
void mul_mat3_m4_v3(const float mat[4][4], float r[3])
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)
MINLINE float normalize_v3_v3(float r[3], const float a[3])
void mat4_to_size(float size[3], const float M[4][4])
bool invert_m3(float mat[3][3])
void mat4_to_dquat(DualQuat *dq, const float basemat[4][4], const float mat[4][4])
static void rebuild_pose_bone_indices(const Object &pose_ob)
static void compute_bbone_segment_boundaries(bke::PChanBone pchanbone)
void BKE_rotMode_change_values(float quat[4], float eul[3], float axis[3], float *angle, eRotationModes oldMode, eRotationModes newMode)
bool BKE_object_minmax_empty_drawtype(const Object *ob, float r_min[3], float r_max[3])
Definition object.cc:3934
MINLINE float normalize_v3(float n[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
void BKE_pose_where_is_bone_tail(bke::PChanBone pchanbone)
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
void BKE_pchan_protected_rotation_axisangle_set(bPoseChannel *pchan, const float axis[3], float angle)
MINLINE void copy_v3_fl3(float v[3], float x, float y, float z)
static void rebuild_bone_array(bArmature &armature)
static void copy_bonechildren(Bone *bone_dst, const Bone *bone_src, const Bone *bone_src_act, Bone **r_bone_dst_act, const int flag)
void BKE_armature_bone_hash_make(bArmature *arm)
@ BONE_INHERIT_SCALE_NONE_LEGACY
@ BONE_INHERIT_SCALE_FIX_SHEAR
void unit_m3(float m[3][3])
float BKE_scene_ctime_get(const Scene *scene)
Definition scene.cc:2581
MINLINE void copy_v3_fl(float r[3], float f)
@ INDEX_ID_AR
Definition DNA_ID.h:1326
void BKE_bone_offset_matrix_get(const Bone *bone, float offs_bone[4][4])
void BKE_constraints_clear_evalob(struct bConstraintOb *cob)
static void copy_bone_transform(Bone *bone_dst, const Bone *bone_src)
MINLINE float max_ff(float a, float b)
void eulO_to_quat(float q[4], const float e[3], short order)
void BKE_pchan_bbone_segments_cache_compute(bke::PChanBone pchanbone, const bArmature &armature)
static void allocate_bbone_cache(bPoseChannel *pchan, const int segments, const bool use_boundaries)
ID_Type GS(const char *name)
Definition DNA_ID.h:410
void BKE_pchan_rebuild_bbone_handles(bPose *pose, bke::PChanBone pchanbone)
MINLINE float len_squared_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
void BKE_pose_splineik_init_tree(Scene *scene, Object *ob, float ctime)
void eulO_to_mat4(float mat[4][4], const float e[3], short order)
static void find_bbone_segment_index_straight(const bke::PChanBoneConst pchanbone, const float *co, int *r_index, float *r_blend_next)
void rescale_m4(float mat[4][4], const float scale[3])
int BKE_armature_bonelist_count(const ListBaseT< Bone > *lb)
static CLG_LogRef LOG
Definition logging.cpp:12
MINLINE float interpf(float target, float origin, float t)
void size_to_mat3(float R[3][3], const float size[3])
void BKE_armature_copy_bone_transforms(bArmature *armature_dst, const bArmature *armature_src)
void DEG_relations_tag_update(Main *bmain)
bool BLI_remlink_safe(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:161
@ BONE_COLLECTION_OVERRIDE_LIBRARY_LOCAL
@ BONE_COLLECTION_ANCESTORS_VISIBLE
std::optional< Bounds< float3 > > BKE_object_boundbox_get(const Object *ob)
Definition object.cc:3726
static void direct_link_bone_collection(BlendDataReader *reader, BoneCollection *bcoll)
void ANIM_armature_runtime_refresh(bArmature *armature)
void vec_roll_to_mat3_normalized(const float nor[3], float roll, float r_mat[3][3])
bool BKE_animdata_drivers_remove_for_rna_struct(ID &owner_id, struct StructRNA &type, void *data)
void mul_m3_m3m3(float R[3][3], const float A[3][3], const float B[3][3])
float bbone_segment_bsp_signed_distance(const bPoseChannel_BBoneSegmentBoundary &boundary, const float *co)
void copy_m4_m4(float m1[4][4], const float m2[4][4])
static void write_bone(BlendWriter *writer, Bone *bone)
VecBase< float, 3 > float3
bool BKE_armature_bone_flag_test_recursive(const Bone *bone, int flag)
void scale_m4_fl(float R[4][4], float scale)
float mat4_to_volume_scale(const float mat[4][4])
IDProperty * IDP_CopyProperty_ex(const IDProperty *prop, int flag) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:968
MINLINE float len_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
static void armature_foreach_id(ID *id, LibraryForeachIDData *data)
MINLINE void mul_v3_v3v3(float r[3], const float v1[3], const float v2[3])
bPoseChannel * BKE_armature_ik_solver_find_root(bPoseChannel *pchan, bKinematicConstraint *data)
static bPoseChannel * pose_channel_find_bone(bPose *pose, Bone *bone)
void BKE_curve_forward_diff_bezier(float q0, float q1, float q2, float q3, float *p, int it, int stride)
Definition curve.cc:1696
float cubic_tangent_factor_circle_v3(const float tan_l[3], const float tan_r[3])
void axis_angle_normalized_to_mat3(float R[3][3], const float axis[3], float angle)
MINLINE int min_ii(int a, int b)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
static void armature_blend_write(BlendWriter *writer, ID *id, const void *id_address)
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
void BKE_bone_parent_transform_calc_from_pchan(bke::PChanBoneConst pchanbone, BoneParentTransform *r_bpt)
void orthogonalize_m4_stable(float R[4][4], int axis, bool normalize)
Bone * BKE_armature_find_bone_name(bArmature *arm, const char *name)
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
static void armature_foreach_id_bone(Bone *bone, LibraryForeachIDData *data)
void mat3_to_quat(float q[4], const float mat[3][3])
void BKE_armature_bonelist_free(ListBaseT< Bone > *lb, bool do_id_user)
@ IDP_TYPE_FILTER_ID
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
void BKE_armature_transform(bArmature *arm, const float mat[4][4], bool do_props)
void mat3_normalized_to_eulO(float eul[3], short order, const float m[3][3])
FunctionRef< void(int index, const Bone &bone)> ForeachBoneFn
float normalize_qt_qt(float r[4], const float q[4])
@ IDTYPE_FLAGS_APPEND_IS_REUSABLE
Definition BKE_idtype.hh:50
void BIK_release_tree(struct Scene *scene, struct Object *ob, float ctime)
#define floorf
#define fabsf
#define sqrtf
#define ceilf
#define atan2f
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
unsigned char select[4]
unsigned char solid[4]
unsigned char active[4]
void write_id_struct(const void *id_address, const T *id, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct_list(const ListBaseT< T > *list, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
struct BoneCollection * prev
eBoneCollection_Flag flags
struct IDProperty * system_properties
ListBaseT< BoneCollectionMember > bones
struct BoneCollection * next
ListBaseT< BoneCollectionReference > collections
struct Bone * bbone_prev
eBone_BBoneFlag bbone_flag
eBone_BBoneHandleFlag bbone_next_flag
struct Bone * bbone_next
struct Bone * parent
eBone_BBoneHandleType bbone_prev_type
eBone_BBoneHandleFlag bbone_prev_flag
eBone_BBoneMappingMode bbone_mapping_mode
Bone_Runtime runtime
eBone_BBoneHandleType bbone_next_type
IDProperty * system_properties
struct Bone * next
ListBaseT< Bone > childbase
eBone_InheritScaleMode inherit_scale_mode
Bounds()=default
IDProperty * system_properties
IDProperty * prop
char name[258]
Definition DNA_ID.h:433
unsigned int session_uid
Definition DNA_ID.h:463
float mat[4][4]
const c_style_mat & ptr() const
bke::ObjectRuntime * runtime
struct bPose * pose
ListBaseT< EditBone > * edbo
struct EditBone * act_edbone
bke::bArmature_Runtime * runtime
ListBaseT< Bone > bonebase
struct BoneCollection ** collection_array
ListBaseT< BoneCollection > collections_legacy
struct bPoseChannel_BBoneSegmentBoundary * bbone_segment_boundaries
struct bPoseChannel * prev
struct bPoseChannel_Runtime runtime
struct bPoseChannel * parent
struct bPoseChannel * child
ListBaseT< struct bConstraint > constraints
struct bPoseChannel * custom_tx
struct bPoseChannel * bbone_next
struct bPoseChannel * bbone_prev
bke::bPoseRuntime * runtime
bAnimVizSettings avs
ListBaseT< bPoseChannel > chanbase
PoseChannelT * pchan
Definition BKE_pose.hh:61
#define N_(msgid)