Blender  V3.3
armature_pose.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2015 Blender Foundation. All rights reserved. */
3 
8 #include "BKE_action.hh"
9 #include "BKE_animsys.h"
10 #include "BKE_armature.hh"
11 
12 #include "BLI_function_ref.hh"
13 #include "BLI_set.hh"
14 
15 #include "DNA_action_types.h"
16 #include "DNA_anim_types.h"
17 #include "DNA_armature_types.h"
18 #include "DNA_object_types.h"
19 
20 #include "RNA_access.h"
21 
22 using namespace blender::bke;
23 
24 namespace {
25 
26 using ActionApplier =
28 
29 /* Forward declarations. */
30 void pose_apply_disable_fcurves_for_unselected_bones(bAction *action,
31  const BoneNameSet &selected_bone_names);
32 void pose_apply_restore_fcurves(bAction *action);
33 
34 void pose_apply(struct Object *ob,
35  struct bAction *action,
36  struct AnimationEvalContext *anim_eval_context,
37  ActionApplier applier);
38 
39 } // namespace
40 
42  struct bAction *action,
43  struct AnimationEvalContext *anim_eval_context)
44 {
45  auto evaluate_and_apply =
46  [](PointerRNA *ptr, bAction *act, const AnimationEvalContext *anim_eval_context) {
47  animsys_evaluate_action(ptr, act, anim_eval_context, false);
48  };
49 
50  pose_apply(ob, action, anim_eval_context, evaluate_and_apply);
51 }
52 
54  struct bAction *action,
55  struct AnimationEvalContext *anim_eval_context)
56 {
57  PointerRNA pose_owner_ptr;
58  RNA_id_pointer_create(&ob->id, &pose_owner_ptr);
59  animsys_evaluate_action(&pose_owner_ptr, action, anim_eval_context, false);
60 }
61 
63  struct bAction *action,
64  struct AnimationEvalContext *anim_eval_context,
65  const float blend_factor)
66 {
67  auto evaluate_and_blend = [blend_factor](PointerRNA *ptr,
68  bAction *act,
69  const AnimationEvalContext *anim_eval_context) {
70  animsys_blend_in_action(ptr, act, anim_eval_context, blend_factor);
71  };
72 
73  pose_apply(ob, action, anim_eval_context, evaluate_and_blend);
74 }
75 
76 namespace {
77 void pose_apply(struct Object *ob,
78  struct bAction *action,
79  struct AnimationEvalContext *anim_eval_context,
80  ActionApplier applier)
81 {
82  bPose *pose = ob->pose;
83  if (pose == nullptr) {
84  return;
85  }
86 
87  const bArmature *armature = (bArmature *)ob->data;
88  const BoneNameSet selected_bone_names = BKE_armature_find_selected_bone_names(armature);
89  const bool limit_to_selected_bones = !selected_bone_names.is_empty();
90 
91  if (limit_to_selected_bones) {
92  /* Mute all FCurves that are not associated with selected bones. This separates the concept of
93  * bone selection from the FCurve evaluation code. */
94  pose_apply_disable_fcurves_for_unselected_bones(action, selected_bone_names);
95  }
96 
97  /* Apply the Action. */
98  PointerRNA pose_owner_ptr;
99  RNA_id_pointer_create(&ob->id, &pose_owner_ptr);
100 
101  applier(&pose_owner_ptr, action, anim_eval_context);
102 
103  if (limit_to_selected_bones) {
104  pose_apply_restore_fcurves(action);
105  }
106 }
107 
108 void pose_apply_restore_fcurves(bAction *action)
109 {
110  /* TODO(Sybren): Restore the FCurve flags, instead of just erasing the 'disabled' flag. */
111  LISTBASE_FOREACH (FCurve *, fcu, &action->curves) {
112  fcu->flag &= ~FCURVE_DISABLED;
113  }
114 }
115 
116 void pose_apply_disable_fcurves_for_unselected_bones(bAction *action,
117  const BoneNameSet &selected_bone_names)
118 {
119  auto disable_unselected_fcurve = [&](FCurve *fcu, const char *bone_name) {
120  const bool is_bone_selected = selected_bone_names.contains(bone_name);
121  if (!is_bone_selected) {
122  fcu->flag |= FCURVE_DISABLED;
123  }
124  };
125  BKE_action_find_fcurves_with_bones(action, disable_unselected_fcurve);
126 }
127 
128 } // namespace
void animsys_evaluate_action(struct PointerRNA *ptr, struct bAction *act, const struct AnimationEvalContext *anim_eval_context, bool flush_to_original)
void animsys_blend_in_action(struct PointerRNA *ptr, struct bAction *act, const AnimationEvalContext *anim_eval_context, float blend_factor)
Definition: anim_sys.c:904
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:336
@ FCURVE_DISABLED
Object is a sort of wrapper for general info.
void BKE_pose_apply_action_selected_bones(struct Object *ob, struct bAction *action, struct AnimationEvalContext *anim_eval_context)
void BKE_pose_apply_action_all_bones(struct Object *ob, struct bAction *action, struct AnimationEvalContext *anim_eval_context)
void BKE_pose_apply_action_blend(struct Object *ob, struct bAction *action, struct AnimationEvalContext *anim_eval_context, const float blend_factor)
bool contains(const Key &key) const
Definition: BLI_set.hh:296
bool is_empty() const
Definition: BLI_set.hh:547
SyclQueue void void size_t num_bytes void
void BKE_action_find_fcurves_with_bones(const bAction *action, FoundFCurveCallback callback)
Definition: action_bones.cc:21
BoneNameSet BKE_armature_find_selected_bone_names(const bArmature *armature)
void RNA_id_pointer_create(ID *id, PointerRNA *r_ptr)
Definition: rna_access.c:112
short flag
struct bPose * pose
void * data
ListBase curves
PointerRNA * ptr
Definition: wm_files.c:3480