Blender V5.3
action_data.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2015 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cfloat>
10#include <cstdlib>
11#include <cstring>
12
13#include "BLI_listbase.hh"
14#include "BLI_utildefines.hh"
15
16#include "BLT_translation.hh"
17
18#include "DNA_anim_types.h"
19#include "DNA_key_types.h"
20#include "DNA_object_types.h"
21#include "DNA_scene_types.h"
22
23#include "RNA_access.hh"
24#include "RNA_define.hh"
25#include "RNA_prototypes.hh"
26
27#include "BKE_action.hh"
28#include "BKE_context.hh"
29#include "BKE_key.hh"
30#include "BKE_lib_id.hh"
31#include "BKE_nla.hh"
32#include "BKE_report.hh"
33#include "BKE_scene.hh"
34
35#include "ANIM_action.hh"
36
37#include "ED_anim_api.hh"
38#include "ED_screen.hh"
39
40#include "DEG_depsgraph.hh"
41
42#include "WM_api.hh"
43#include "WM_types.hh"
44
45#include "UI_interface_c.hh"
46
47#include "action_intern.hh"
48
49namespace blender {
50
51/* -------------------------------------------------------------------- */
54
56{
57 { /* Support use from the layout.template_action() UI template. */
58 PointerRNA ptr = {};
59 PropertyRNA *prop = nullptr;
61 /* template_action() sets a RNA_AnimData pointer, whereas other code may set
62 * other pointer types. This code here only deals with the former. */
63 if (prop && ptr.type == RNA_AnimData) {
64 if (!RNA_property_editable(&ptr, prop)) {
65 return nullptr;
66 }
67 if (r_adt_id_owner) {
68 *r_adt_id_owner = ptr.owner_id;
69 }
70 AnimData *adt = static_cast<AnimData *>(ptr.data);
71 return adt;
72 }
73 }
74
75 SpaceLink *space_data = CTX_wm_space_data(C);
76 if (!space_data || space_data->spacetype != SPACE_ACTION) {
77 return nullptr;
78 }
79
80 SpaceAction *saction = reinterpret_cast<SpaceAction *>(space_data);
82 AnimData *adt = nullptr;
83
84 /* Get AnimData block to use */
85 if (saction->mode == SACTCONT_ACTION) {
86 /* Currently, "Action Editor" means object-level only... */
87 if (ob) {
88 adt = ob->adt;
89 if (r_adt_id_owner) {
90 *r_adt_id_owner = &ob->id;
91 }
92 }
93 }
94 else if (saction->mode == SACTCONT_SHAPEKEY) {
95 Key *key = BKE_key_from_object(ob);
96 if (key) {
97 adt = key->adt;
98 if (r_adt_id_owner) {
99 *r_adt_id_owner = &key->id;
100 }
101 }
102 }
103
104 return adt;
105}
106
108
109/* -------------------------------------------------------------------- */
112
114{
115 bAction *action;
116
117 /* create action - the way to do this depends on whether we've got an
118 * existing one there already, in which case we make a copy of it
119 * (which is useful for "versioning" actions within the same file)
120 */
121 if (oldact && GS(oldact->id.name) == ID_AC) {
122 /* make a copy of the existing action */
123 action = id_cast<bAction *>(BKE_id_copy(CTX_data_main(C), &oldact->id));
124 }
125 else {
126 /* just make a new (empty) action */
127 action = BKE_action_add(CTX_data_main(C), DATA_("Action"));
128 }
129
130 /* when creating new ID blocks, there is already 1 user (as for all new datablocks),
131 * but the RNA pointer code will assign all the proper users instead, so we compensate
132 * for that here
133 */
134 BLI_assert(action->id.us == 1);
135 id_us_min(&action->id);
136
137 return action;
138}
139
141
142/* -------------------------------------------------------------------- */
151
153{
154 { /* Support use from the layout.template_action() UI template. */
155 PointerRNA ptr = {};
156 PropertyRNA *prop = nullptr;
158 if (prop) {
159 return RNA_property_editable(&ptr, prop);
160 }
161 }
162
163 Scene *scene = CTX_data_scene(C);
164
165 /* Check tweak-mode is off (as you don't want to be tampering with the action in that case) */
166 /* NOTE: unlike for pushdown,
167 * this operator needs to be run when creating an action from nothing... */
169 SpaceAction *saction = reinterpret_cast<SpaceAction *>(CTX_wm_space_data(C));
171
172 /* For now, actions are only for the active object, and on object and shape-key levels... */
173 if (saction->mode == SACTCONT_ACTION) {
174 /* XXX: This assumes that actions are assigned to the active object in this mode */
175 if (ob) {
176 if ((ob->adt == nullptr) || (ob->adt->flag & ADT_NLA_EDIT_ON) == 0) {
177 return true;
178 }
179 }
180 }
181 else if (saction->mode == SACTCONT_SHAPEKEY) {
182 Key *key = BKE_key_from_object(ob);
183 if (key) {
184 if ((key->adt == nullptr) || (key->adt->flag & ADT_NLA_EDIT_ON) == 0) {
185 return true;
186 }
187 }
188 }
189 }
190 else if (ED_operator_nla_active(C)) {
191 if (!(scene->flag & SCE_NLA_EDIT_ON)) {
192 return true;
193 }
194 }
195
196 /* something failed... */
197 return false;
198}
199
201{
202 PointerRNA ptr;
203 PropertyRNA *prop;
204
205 bAction *oldact = nullptr;
206 AnimData *adt = nullptr;
207 ID *adt_id_owner = nullptr;
208 /* hook into UI */
210
211 if (prop) {
212 /* The operator was called from a button. */
213 PointerRNA oldptr;
214
215 oldptr = RNA_property_pointer_get(&ptr, prop);
216 oldact = id_cast<bAction *>(oldptr.owner_id);
217
218 /* stash the old action to prevent it from being lost */
219 if (ptr.type == RNA_AnimData) {
220 adt = static_cast<AnimData *>(ptr.data);
221 adt_id_owner = ptr.owner_id;
222 }
223 else if (ptr.type == RNA_SpaceDopeSheetEditor) {
224 adt = ED_actedit_animdata_from_context(C, &adt_id_owner);
225 }
226 }
227 else {
228 adt = ED_actedit_animdata_from_context(C, &adt_id_owner);
229 oldact = adt ? adt->action : nullptr;
230 }
231 {
232 bAction *action = nullptr;
233
234 /* Perform stashing operation - But only if there is an action */
235 if (adt && oldact) {
236 BLI_assert(adt_id_owner != nullptr);
237 /* stash the action */
238 if (!BKE_nla_action_stash({*adt_id_owner, *adt}, ID_IS_OVERRIDE_LIBRARY(adt_id_owner))) {
239#if 0
240 printf("WARNING: Failed to stash %s. It may already exist in the NLA stack though\n",
241 oldact->id.name);
242#endif
243 }
244 }
245
246 /* create action */
247 action = action_create_new(C, oldact);
248
249 if (prop) {
250 /* set this new action */
251 PointerRNA idptr = RNA_id_pointer_create(&action->id);
252 RNA_property_pointer_set(&ptr, prop, idptr, nullptr);
253 RNA_property_update(C, &ptr, prop);
254 }
255 }
256
257 /* set notifier that keyframes have changed */
259
260 return OPERATOR_FINISHED;
261}
262
264{
265 /* identifiers */
266 ot->name = "New Action";
267 ot->idname = "ACTION_OT_new";
268 ot->description = "Create new action";
269
270 /* API callbacks. */
271 ot->exec = action_new_exec;
272 ot->poll = action_new_poll;
273
274 /* flags */
276}
277
279
280/* -------------------------------------------------------------------- */
288
290{
292 return false;
293 }
294
296 if (!adt || !adt->action) {
297 return false;
298 }
299
300 /* NOTE: We check this for the AnimData block in question and not the global flag,
301 * as the global flag may be left dirty by some of the browsing ops here.
302 */
303 return (adt->flag & ADT_NLA_EDIT_ON) == 0;
304}
305
307{
308 ID *adt_id_owner = nullptr;
309 AnimData *adt = ED_actedit_animdata_from_context(C, &adt_id_owner);
310
311 /* Do the deed... */
312 if (adt && adt->action) {
313 animrig::Action &action = adt->action->wrap();
314
315 /* action can be safely added */
316 BKE_nla_action_pushdown({*adt_id_owner, *adt}, ID_IS_OVERRIDE_LIBRARY(adt_id_owner));
317
318 Main *bmain = CTX_data_main(C);
319 DEG_id_tag_update_ex(bmain, adt_id_owner, ID_RECALC_ANIMATION);
320
321 /* The action needs updating too, as FCurve modifiers are to be reevaluated. They won't extend
322 * beyond the NLA strip after pushing down to the NLA. */
324 }
325
326 /* Send notifiers that stuff has changed */
328 return OPERATOR_FINISHED;
329}
330
332{
333 /* identifiers */
334 ot->name = "Push Down Action";
335 ot->idname = "ACTION_OT_push_down";
336 ot->description = "Push action down on to the NLA stack as a new strip";
337
338 /* callbacks */
341
342 /* flags */
344}
345
347
348/* -------------------------------------------------------------------- */
351
353{
354 ID *adt_id_owner = nullptr;
355 AnimData *adt = ED_actedit_animdata_from_context(C, &adt_id_owner);
356
357 /* Perform stashing operation */
358 if (adt) {
359 /* stash the action */
360 if (!BKE_nla_action_stash({*adt_id_owner, *adt}, ID_IS_OVERRIDE_LIBRARY(adt_id_owner))) {
361 /* action has already been added - simply warn about this, and clear */
362 BKE_report(op->reports, RPT_ERROR, "Action+Slot has already been stashed");
363 }
364
365 if (!animrig::unassign_action({*adt_id_owner, *adt})) {
366 BKE_report(op->reports, RPT_ERROR, "Could not unassign the active Action");
367 }
368 }
369
370 /* Send notifiers that stuff has changed */
372 return OPERATOR_FINISHED;
373}
374
376{
377 /* identifiers */
378 ot->name = "Stash Action";
379 ot->idname = "ACTION_OT_stash";
380 ot->description = "Store this action in the NLA stack as a non-contributing strip for later use";
381
382 /* callbacks */
385
386 /* flags */
388
389 /* properties */
390 ot->prop = RNA_def_boolean(ot->srna,
391 "create_new",
392 true,
393 "Create New Action",
394 "Create a new action once the existing one has been safely stored");
395}
396
398
399/* -------------------------------------------------------------------- */
406
408{
411
412 /* Check tweak-mode is off (as you don't want to be tampering with the action in that case) */
413 /* NOTE: unlike for pushdown,
414 * this operator needs to be run when creating an action from nothing... */
415 if (adt) {
416 if (!(adt->flag & ADT_NLA_EDIT_ON)) {
417 return true;
418 }
419 }
420 else {
421 /* There may not be any action/animdata yet, so, just fall back to the global setting
422 * (which may not be totally valid yet if the action editor was used and things are
423 * now in an inconsistent state)
424 */
425 SpaceAction *saction = reinterpret_cast<SpaceAction *>(CTX_wm_space_data(C));
426 Scene *scene = CTX_data_scene(C);
427
428 if (!(scene->flag & SCE_NLA_EDIT_ON)) {
429 /* For now, actions are only for the active object, and on object and shape-key levels...
430 */
431 return ELEM(saction->mode, SACTCONT_ACTION, SACTCONT_SHAPEKEY);
432 }
433 }
434 }
435
436 /* something failed... */
437 return false;
438}
439
441{
442 ID *adt_id_owner = nullptr;
443 AnimData *adt = ED_actedit_animdata_from_context(C, &adt_id_owner);
444
445 /* Check for no action... */
446 if (adt->action == nullptr) {
447 /* just create a new action */
448 bAction *action = action_create_new(C, nullptr);
449 if (!animrig::assign_action(action, {*adt_id_owner, *adt})) {
451 op->reports, RPT_ERROR, "Could not assign a new Action to %s", adt_id_owner->name + 2);
452 }
453 }
454 else if (adt) {
455 /* Perform stashing operation */
456 if (BKE_nla_action_stash({*adt_id_owner, *adt}, ID_IS_OVERRIDE_LIBRARY(adt_id_owner))) {
457 bAction *new_action = nullptr;
458
459 /* Create new action not based on the old one
460 * (since the "new" operator already does that). */
461 new_action = action_create_new(C, nullptr);
462 if (!animrig::assign_action(new_action, {*adt_id_owner, *adt})) {
464 op->reports, RPT_ERROR, "Could not assign a new Action to %s", adt_id_owner->name + 2);
465 }
466 }
467 else {
468 /* action has already been added - simply warn about this, and clear */
469 BKE_report(op->reports, RPT_ERROR, "Action+Slot has already been stashed");
470 if (!animrig::unassign_action({*adt_id_owner, *adt})) {
472 op->reports, RPT_ERROR, "Could not un-assign Action from %s", adt_id_owner->name + 2);
473 }
474 }
475 }
476
477 /* Send notifiers that stuff has changed */
479 return OPERATOR_FINISHED;
480}
481
483{
484 /* identifiers */
485 ot->name = "Stash Action";
486 ot->idname = "ACTION_OT_stash_and_create";
487 ot->description =
488 "Store this action in the NLA stack as a non-contributing strip for later use, and create a "
489 "new action";
490
491 /* callbacks */
494
495 /* flags */
497}
498
500
501/* -------------------------------------------------------------------- */
511
513 bContext *C, ID *id, AnimData *adt, bAction *act, ReportList *reports, bool force_delete)
514{
515 BLI_assert(id);
516
517 /* If the old action only has a single user (that it's about to lose),
518 * warn user about it
519 *
520 * TODO: Maybe we should just save it for them? But then, there's the problem of
521 * trying to get rid of stuff that's actually unwanted!
522 */
523 if (act->id.us == 1) {
524 BKE_reportf(reports,
526 "Action '%s' will not be saved, create Fake User or Stash in NLA Stack to retain",
527 act->id.name + 2);
528 }
529
530 /* Clear Fake User and remove action stashing strip (if present) */
531 if (force_delete) {
532 /* Remove stashed strip binding this action to this datablock */
533 /* XXX: we cannot unlink it from *OTHER* datablocks that may also be stashing it,
534 * but GE users only seem to use/care about single-object binding for now so this
535 * should be fine
536 */
537 if (adt) {
538 NlaTrack *nlt, *nlt_next;
539 NlaStrip *strip, *nstrip;
540
541 for (nlt = adt->nla_tracks.first(); nlt; nlt = nlt_next) {
542 nlt_next = nlt->next;
543
544 if (strstr(nlt->name, DATA_("[Action Stash]"))) {
545 for (strip = nlt->strips.first(); strip; strip = nstrip) {
546 nstrip = strip->next;
547
548 if (strip->act == act) {
549 /* Remove this strip, and the track too if it doesn't have anything else */
550 BKE_nlastrip_remove_and_free(&nlt->strips, strip, true);
551
552 if (nlt->strips.first_ == nullptr) {
553 BLI_assert(nstrip == nullptr);
555 }
556 }
557 }
558 }
559 }
560 }
561
562 /* Clear Fake User */
563 id_fake_user_clear(&act->id);
564 }
565
566 /* If in Tweak Mode, don't unlink. Instead, this becomes a shortcut to exit Tweak Mode. */
567 if ((adt) && (adt->flag & ADT_NLA_EDIT_ON)) {
568 BKE_nla_tweakmode_exit({*id, *adt});
569
570 Scene *scene = CTX_data_scene(C);
571 if (scene != nullptr) {
572 scene->flag &= ~SCE_NLA_EDIT_ON;
573 }
574 }
575 else {
576 /* Clear AnimData -> action via RNA, so that it triggers message bus updates. */
577 PointerRNA ptr = RNA_pointer_create_discrete(id, RNA_AnimData, adt);
578 PropertyRNA *prop = RNA_struct_find_property(&ptr, "action");
579
580 RNA_property_pointer_set(&ptr, prop, {}, nullptr);
581 RNA_property_update(C, &ptr, prop);
582 }
583}
584
585/* -------------------------- */
586
588{
589 ID *animated_id = nullptr;
590 AnimData *adt = ED_actedit_animdata_from_context(C, &animated_id);
591 if (!animated_id) {
592 return false;
593 }
594 if (!BKE_id_is_editable(CTX_data_main(C), animated_id)) {
595 return false;
596 }
597 return adt && adt->action;
598}
599
601{
602 ID *animated_id = nullptr;
603 AnimData *adt = ED_actedit_animdata_from_context(C, &animated_id);
604 bool force_delete = RNA_boolean_get(op->ptr, "force_delete");
605
606 if (adt && adt->action) {
607 ED_animedit_unlink_action(C, animated_id, adt, adt->action, op->reports, force_delete);
608 }
609
610 /* Unlink is also abused to exit NLA tweak mode. */
612
613 return OPERATOR_FINISHED;
614}
615
617{
618 /* NOTE: this is hardcoded to match the behavior for the unlink button
619 * (in `interface_templates.cc`). */
620 RNA_boolean_set(op->ptr, "force_delete", event->modifier & KM_SHIFT);
621 return action_unlink_exec(C, op);
622}
623
625{
626 PropertyRNA *prop;
627
628 /* identifiers */
629 ot->name = "Unlink Action";
630 ot->idname = "ACTION_OT_unlink";
631 ot->description = "Unlink this action from the active action slot (and/or exit Tweak Mode)";
632
633 /* callbacks */
637
638 /* properties */
639 prop = RNA_def_boolean(ot->srna,
640 "force_delete",
641 false,
642 "Force Delete",
643 "Clear Fake User and remove "
644 "copy stashed in this data-block's NLA stack");
646
647 /* flags */
649}
650
652
653} // namespace blender
Functions and classes to work with Actions.
Blender kernel action and pose functionality.
#define DATA_(msgid)
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
Object is a sort of wrapper for general info.
#define C
Definition RandGen.cpp:29
#define ND_NLA_ACTCHANGE
Definition WM_types.hh:541
#define NC_ANIMATION
Definition WM_types.hh:401
#define NA_ADDED
Definition WM_types.hh:636
#define ND_KEYFRAME
Definition WM_types.hh:537
#define ELEM(...)
bool unassign_action(ID &animated_id)
bool assign_action(bAction *action, ID &animated_id)
void context_active_but_prop_get_templateID(const bContext *C, PointerRNA *r_ptr, PropertyRNA **r_prop)
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
PointerRNA RNA_property_pointer_get(PointerRNA *ptr, PropertyRNA *prop)
@ RPT_WARNING
Definition BKE_report.hh:40
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
static wmOperatorStatus action_unlink_invoke(bContext *C, wmOperator *op, const wmEvent *event)
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
void RNA_property_pointer_set(PointerRNA *ptr, PropertyRNA *prop, PointerRNA ptr_value, ReportList *reports)
bool ED_operator_nla_active(bContext *C)
PointerRNA RNA_id_pointer_create(ID *id)
static bool action_stash_create_poll(bContext *C)
static bool action_unlink_poll(bContext *C)
static wmOperatorStatus action_stash_create_exec(bContext *C, wmOperator *op)
bAction * BKE_action_add(Main *bmain, const char name[])
static wmOperatorStatus action_pushdown_exec(bContext *C, wmOperator *)
static wmOperatorStatus action_stash_exec(bContext *C, wmOperator *op)
void BKE_nla_tweakmode_exit(OwnedAnimData owned_adt)
static wmOperatorStatus action_unlink_exec(bContext *C, wmOperator *op)
bool ED_operator_action_active(bContext *C)
void ED_animedit_unlink_action(bContext *C, ID *id, AnimData *adt, bAction *act, ReportList *reports, bool force_delete)
void ACTION_OT_stash(wmOperatorType *ot)
void ACTION_OT_unlink(wmOperatorType *ot)
void DEG_id_tag_update_ex(Main *bmain, ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
static bool action_new_poll(bContext *C)
void BKE_nla_action_pushdown(OwnedAnimData owned_adt, bool is_liboverride)
static wmOperatorStatus action_new_exec(bContext *C, wmOperator *)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
void ACTION_OT_stash_and_create(wmOperatorType *ot)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void RNA_boolean_set(PointerRNA *ptr, const char *name, bool value)
void ACTION_OT_push_down(wmOperatorType *ot)
SpaceLink * CTX_wm_space_data(const bContext *C)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
Main * CTX_data_main(const bContext *C)
@ ID_RECALC_ANIMATION
Definition DNA_ID.h:1077
ID_Type GS(const char *name)
Definition DNA_ID.h:410
@ PROP_SKIP_SAVE
Definition RNA_types.hh:414
void BKE_nlatrack_remove_and_free(ListBaseT< NlaTrack > *tracks, NlaTrack *nlt, bool do_id_user)
bool RNA_property_editable(const PointerRNA *ptr, PropertyRNA *prop)
Scene * CTX_data_scene(const bContext *C)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
AnimData * ED_actedit_animdata_from_context(const bContext *C, ID **r_adt_id_owner)
Key * BKE_key_from_object(Object *ob)
Definition key.cc:1311
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
static bool action_pushdown_poll(bContext *C)
bool BKE_id_is_editable(const Main *bmain, const ID *id)
Definition lib_id.cc:2782
ID * BKE_id_copy(Main *bmain, const ID *id)
Definition lib_id.cc:856
Object * CTX_data_active_object(const bContext *C)
void id_us_min(ID *id)
Definition lib_id.cc:441
void ACTION_OT_new(wmOperatorType *ot)
bool BKE_nla_action_stash(OwnedAnimData owned_adt, bool is_liboverride)
static bAction * action_create_new(bContext *C, bAction *oldact)
void id_fake_user_clear(ID *id)
Definition lib_id.cc:479
void BKE_nlastrip_remove_and_free(ListBaseT< NlaStrip > *strips, NlaStrip *strip, const bool do_id_user)
eAnimData_Flag flag
ListBaseT< NlaTrack > nla_tracks
char name[258]
Definition DNA_ID.h:433
struct AnimData * adt
struct NlaStrip * next
ListBaseT< NlaStrip > strips
struct NlaTrack * next
struct AnimData * adt
StructRNA * type
Definition RNA_types.hh:63
eAnimEdit_Context mode
wmEventModifierFlag modifier
Definition WM_types.hh:834
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
struct ReportList * reports