This commit takes the 'Slotted Actions' out of the experimental phase. As a result: - All newly created Actions will be slotted Actions. - Legacy Actions loaded from disk will be versioned to slotted Actions. - The new Python API for slots, layers, strips, and channel bags is available. - The legacy Python API for accessing F-Curves and Action Groups is still available, and will operate on the F-Curves/Groups for the first slot only. - Creating an Action by keying (via the UI, operators, or the `rna_struct.keyframe_insert` function) will try and share Actions between related data-blocks. See !126655 for more info about this. - Assigning an Action to a data-block will auto-assign a suitable Action Slot. The logic for this is described below. However, There are cases where this does _not_ automatically assign a slot, and thus the Action will effectively _not_ animate the data-block. Effort has been spent to make Action selection work both reliably for Blender users as well as keep the behaviour the same for Python scripts. Where these two goals did not converge, reliability and understandability for users was prioritised. Auto-selection of the Action Slot upon assigning the Action works as follows. The first rule to find a slot wins. 1. The data-block remembers the slot name that was last assigned. If the newly assigned Action has a slot with that name, it is chosen. 2. If the Action has a slot with the same name as the data-block, it is chosen. 3. If the Action has only one slot, and it has never been assigned to anything, it is chosen. 4. If the Action is assigned to an NLA strip or an Action constraint, and the Action has a single slot, and that slot has a suitable ID type, it is chosen. This last step is what I was referring to with "Where these two goals did not converge, reliability and understandability for users was prioritised." For regular Action assignments (like via the Action selectors in the Properties editor) this rule doesn't apply, even though with legacy Actions the final state ("it is animated by this Action") differs from the final state with slotted Actions ("it has no slot so is not animated"). This is done to support the following workflow: - Create an Action by animating Cube. - In order to animate Suzanne with that same Action, assign the Action to Suzanne. - Start keying Suzanne. This auto-creates and auto-assigns a new slot for Suzanne. If rule 4. above would apply in this case, the 2nd step would automatically select the Cube slot for Suzanne as well, which would immediately overwrite Suzanne's properties with the Cube animation. Technically, this commit: - removes the `WITH_ANIM_BAKLAVA` build flag, - removes the `use_animation_baklava` experimental flag in preferences, - updates the code to properly deal with the fact that empty Actions are now always considered slotted/layered Actions (instead of that relying on the user preference). Note that 'slotted Actions' and 'layered Actions' are the exact same thing, just focusing on different aspects (slot & layers) of the new data model. The "Baklava phase 1" assumptions are still asserted. This means that: - an Action can have zero or one layer, - that layer can have zero or one strip, - that strip must be of type 'keyframe' and be infinite with zero offset. The code to handle legacy Actions is NOT removed in this commit. It will be removed later. For now it's likely better to keep it around as reference to the old behaviour in order to aid in some inevitable bugfixing. Ref: #120406
336 lines
8.9 KiB
C++
336 lines
8.9 KiB
C++
/* SPDX-FileCopyrightText: 2024 Blender Authors
|
|
*
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
#include "ANIM_action.hh"
|
|
#include "ANIM_action_legacy.hh"
|
|
|
|
#include "BLI_listbase_wrapper.hh"
|
|
|
|
#include "BKE_fcurve.hh"
|
|
|
|
namespace blender::animrig::legacy {
|
|
|
|
static Strip *first_keyframe_strip(Action &action)
|
|
{
|
|
for (Layer *layer : action.layers()) {
|
|
for (Strip *strip : layer->strips()) {
|
|
if (strip->type() == Strip::Type::Keyframe) {
|
|
return strip;
|
|
}
|
|
}
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
ChannelBag *channelbag_get(Action &action)
|
|
{
|
|
if (action.slots().is_empty()) {
|
|
return nullptr;
|
|
}
|
|
|
|
Strip *keystrip = first_keyframe_strip(action);
|
|
if (!keystrip) {
|
|
return nullptr;
|
|
}
|
|
|
|
return keystrip->data<StripKeyframeData>(action).channelbag_for_slot(*action.slot(0));
|
|
}
|
|
|
|
ChannelBag &channelbag_ensure(Action &action)
|
|
{
|
|
assert_baklava_phase_1_invariants(action);
|
|
|
|
/* Ensure a Slot. */
|
|
Slot *slot;
|
|
if (action.slots().is_empty()) {
|
|
slot = &action.slot_add();
|
|
}
|
|
else {
|
|
slot = action.slot(0);
|
|
}
|
|
|
|
/* Ensure a Layer + keyframe Strip. */
|
|
action.layer_keystrip_ensure();
|
|
Strip &keystrip = *action.layer(0)->strip(0);
|
|
|
|
/* Ensure a ChannelBag. */
|
|
return keystrip.data<StripKeyframeData>(action).channelbag_for_slot_ensure(*slot);
|
|
}
|
|
|
|
/* Lots of template args to support transparent non-const and const versions. */
|
|
template<typename ActionType,
|
|
typename FCurveType,
|
|
typename LayerType,
|
|
typename StripType,
|
|
typename StripKeyframeDataType,
|
|
typename ChannelBagType>
|
|
static Vector<FCurveType *> fcurves_all_templated(ActionType &action)
|
|
{
|
|
/* Legacy Action. */
|
|
if (action.is_action_legacy()) {
|
|
Vector<FCurveType *> legacy_fcurves;
|
|
LISTBASE_FOREACH (FCurveType *, fcurve, &action.curves) {
|
|
legacy_fcurves.append(fcurve);
|
|
}
|
|
return legacy_fcurves;
|
|
}
|
|
|
|
/* Layered Action. */
|
|
BLI_assert(action.is_action_layered());
|
|
|
|
Vector<FCurveType *> all_fcurves;
|
|
for (LayerType *layer : action.layers()) {
|
|
for (StripType *strip : layer->strips()) {
|
|
switch (strip->type()) {
|
|
case Strip::Type::Keyframe: {
|
|
StripKeyframeDataType &strip_data = strip->template data<StripKeyframeData>(action);
|
|
for (ChannelBagType *bag : strip_data.channelbags()) {
|
|
for (FCurveType *fcurve : bag->fcurves()) {
|
|
all_fcurves.append(fcurve);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return all_fcurves;
|
|
}
|
|
|
|
Vector<FCurve *> fcurves_all(bAction *action)
|
|
{
|
|
if (!action) {
|
|
return {};
|
|
}
|
|
return fcurves_all_templated<Action, FCurve, Layer, Strip, StripKeyframeData, ChannelBag>(
|
|
action->wrap());
|
|
}
|
|
|
|
Vector<const FCurve *> fcurves_all(const bAction *action)
|
|
{
|
|
if (!action) {
|
|
return {};
|
|
}
|
|
return fcurves_all_templated<const Action,
|
|
const FCurve,
|
|
const Layer,
|
|
const Strip,
|
|
const StripKeyframeData,
|
|
const ChannelBag>(action->wrap());
|
|
}
|
|
|
|
Vector<FCurve *> fcurves_first_slot(bAction *action)
|
|
{
|
|
if (!action) {
|
|
return {};
|
|
}
|
|
Action &action_wrap = action->wrap();
|
|
|
|
if (action_wrap.is_action_legacy()) {
|
|
return fcurves_all(action);
|
|
}
|
|
|
|
if (action_wrap.slots().is_empty()) {
|
|
return {};
|
|
}
|
|
return fcurves_for_action_slot(action, action_wrap.slot(0)->handle);
|
|
}
|
|
|
|
/* Lots of template args to support transparent non-const and const versions. */
|
|
template<typename ActionType,
|
|
typename FCurveType,
|
|
typename LayerType,
|
|
typename StripType,
|
|
typename StripKeyframeDataType,
|
|
typename ChannelBagType>
|
|
static Vector<FCurveType *> fcurves_for_action_slot_templated(ActionType &action,
|
|
const slot_handle_t slot_handle)
|
|
{
|
|
/* Legacy Action. */
|
|
if (action.is_action_legacy()) {
|
|
return listbase_to_vector<FCurveType>(action.curves);
|
|
}
|
|
|
|
/* Layered Action. */
|
|
Vector<FCurveType *> as_vector(animrig::fcurves_for_action_slot(action, slot_handle));
|
|
return as_vector;
|
|
}
|
|
|
|
Vector<FCurve *> fcurves_for_action_slot(bAction *action, const slot_handle_t slot_handle)
|
|
{
|
|
if (!action) {
|
|
return {};
|
|
}
|
|
return fcurves_for_action_slot_templated<Action,
|
|
FCurve,
|
|
Layer,
|
|
Strip,
|
|
StripKeyframeData,
|
|
ChannelBag>(action->wrap(), slot_handle);
|
|
}
|
|
Vector<const FCurve *> fcurves_for_action_slot(const bAction *action,
|
|
const slot_handle_t slot_handle)
|
|
{
|
|
if (!action) {
|
|
return {};
|
|
}
|
|
return fcurves_for_action_slot_templated<const Action,
|
|
const FCurve,
|
|
const Layer,
|
|
const Strip,
|
|
const StripKeyframeData,
|
|
const ChannelBag>(action->wrap(), slot_handle);
|
|
}
|
|
|
|
Vector<FCurve *> fcurves_for_assigned_action(AnimData *adt)
|
|
{
|
|
if (!adt || !adt->action) {
|
|
return {};
|
|
}
|
|
return legacy::fcurves_for_action_slot(adt->action, adt->slot_handle);
|
|
}
|
|
Vector<const FCurve *> fcurves_for_assigned_action(const AnimData *adt)
|
|
{
|
|
if (!adt || !adt->action) {
|
|
return {};
|
|
}
|
|
return legacy::fcurves_for_action_slot(const_cast<const bAction *>(adt->action),
|
|
adt->slot_handle);
|
|
}
|
|
|
|
bool assigned_action_has_keyframes(AnimData *adt)
|
|
{
|
|
if (adt == nullptr || adt->action == nullptr) {
|
|
return false;
|
|
}
|
|
|
|
Action &action = adt->action->wrap();
|
|
|
|
if (action.is_action_legacy()) {
|
|
return action.curves.first != nullptr;
|
|
}
|
|
|
|
return action.has_keyframes(adt->slot_handle);
|
|
}
|
|
|
|
Vector<bActionGroup *> channel_groups_all(bAction *action)
|
|
{
|
|
if (!action) {
|
|
return {};
|
|
}
|
|
|
|
Action &action_wrap = action->wrap();
|
|
|
|
/* Legacy Action. */
|
|
if (action_wrap.is_action_legacy()) {
|
|
Vector<bActionGroup *> legacy_groups;
|
|
LISTBASE_FOREACH (bActionGroup *, group, &action_wrap.groups) {
|
|
legacy_groups.append(group);
|
|
}
|
|
return legacy_groups;
|
|
}
|
|
|
|
/* Layered Action. */
|
|
Vector<bActionGroup *> all_groups;
|
|
for (Layer *layer : action_wrap.layers()) {
|
|
for (Strip *strip : layer->strips()) {
|
|
switch (strip->type()) {
|
|
case Strip::Type::Keyframe: {
|
|
StripKeyframeData &strip_data = strip->template data<StripKeyframeData>(action_wrap);
|
|
for (ChannelBag *bag : strip_data.channelbags()) {
|
|
all_groups.extend(bag->channel_groups());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return all_groups;
|
|
}
|
|
|
|
Vector<bActionGroup *> channel_groups_for_assigned_slot(AnimData *adt)
|
|
{
|
|
if (!adt || !adt->action) {
|
|
return {};
|
|
}
|
|
|
|
Action &action = adt->action->wrap();
|
|
|
|
/* Legacy Action. */
|
|
if (action.is_action_legacy()) {
|
|
return channel_groups_all(adt->action);
|
|
}
|
|
|
|
/* Layered Action. */
|
|
ChannelBag *bag = channelbag_for_action_slot(action, adt->slot_handle);
|
|
if (!bag) {
|
|
return {};
|
|
}
|
|
|
|
Vector<bActionGroup *> slot_groups(bag->channel_groups());
|
|
return slot_groups;
|
|
}
|
|
|
|
bool action_treat_as_legacy(const bAction &action)
|
|
{
|
|
/* At runtime, legacy Actions should have been versioned to layered/slotted Actions. However,
|
|
* unit tests can still create legacy Actions, and so this function still has to distinguish
|
|
* between them.
|
|
*
|
|
* Note that empty Actions also count as 'layered'. */
|
|
return !action.wrap().is_action_layered();
|
|
}
|
|
|
|
bool action_fcurves_remove(bAction &action,
|
|
const slot_handle_t slot_handle,
|
|
const StringRefNull rna_path_prefix)
|
|
{
|
|
BLI_assert(!rna_path_prefix.is_empty());
|
|
if (rna_path_prefix.is_empty()) {
|
|
return false;
|
|
}
|
|
|
|
Action &action_wrapped = action.wrap();
|
|
|
|
/* Legacy Action. Code is 'borrowed' from fcurves_path_remove_fix() in
|
|
* blenkernel/intern/anim_data.cc */
|
|
if (action_wrapped.is_action_legacy()) {
|
|
bool any_removed = false;
|
|
LISTBASE_FOREACH_MUTABLE (FCurve *, fcurve, &action.curves) {
|
|
if (!fcurve->rna_path) {
|
|
continue;
|
|
}
|
|
|
|
if (STRPREFIX(fcurve->rna_path, rna_path_prefix.c_str())) {
|
|
BLI_remlink(&action.curves, fcurve);
|
|
BKE_fcurve_free(fcurve);
|
|
any_removed = true;
|
|
}
|
|
}
|
|
return any_removed;
|
|
}
|
|
|
|
/* Layered Action. */
|
|
ChannelBag *bag = channelbag_for_action_slot(action.wrap(), slot_handle);
|
|
if (!bag) {
|
|
return false;
|
|
}
|
|
|
|
bool any_removed = false;
|
|
for (int64_t fcurve_index = 0; fcurve_index < bag->fcurve_array_num; fcurve_index++) {
|
|
FCurve *fcurve = bag->fcurve(fcurve_index);
|
|
if (!fcurve->rna_path) {
|
|
continue;
|
|
}
|
|
|
|
if (STRPREFIX(fcurve->rna_path, rna_path_prefix.c_str())) {
|
|
bag->fcurve_remove_by_index(fcurve_index);
|
|
fcurve_index--;
|
|
any_removed = true;
|
|
}
|
|
}
|
|
return any_removed;
|
|
}
|
|
|
|
} // namespace blender::animrig::legacy
|