Files
test2/source/blender/editors/space_graph/graph_utils.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

307 lines
8.5 KiB
C++
Raw Normal View History

/* SPDX-FileCopyrightText: 2009 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup spgraph
2011-02-27 20:29:51 +00:00
*/
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <cstring>
#include "DNA_anim_types.h"
#include "DNA_screen_types.h"
#include "DNA_space_types.h"
#include "MEM_guardedalloc.h"
#include "BLI_blenlib.h"
#include "BKE_context.hh"
#include "BKE_fcurve.h"
#include "BKE_screen.hh"
#include "ED_anim_api.hh"
#include "ED_screen.hh"
#include "UI_interface.hh"
#include "RNA_access.hh"
#include "RNA_prototypes.h"
2012-05-08 20:18:33 +00:00
#include "graph_intern.h" /* own include */
/* -------------------------------------------------------------------- */
/** \name Set Up Drivers Editor
* \{ */
void ED_drivers_editor_init(bContext *C, ScrArea *area)
{
SpaceGraph *sipo = (SpaceGraph *)area->spacedata.first;
/* Set mode */
sipo->mode = SIPO_MODE_DRIVERS;
/* Show Properties Region (or else the settings can't be edited) */
ARegion *region_props = BKE_area_find_region_type(area, RGN_TYPE_UI);
if (region_props) {
UI_panel_category_active_set(region_props, "Drivers");
region_props->flag &= ~RGN_FLAG_HIDDEN;
/* XXX: Adjust width of this too? */
ED_region_visibility_change_update(C, area, region_props);
}
else {
printf("%s: Couldn't find properties region for Drivers Editor - %p\n", __func__, area);
}
/* Adjust framing in graph region */
/* TODO: Have a way of not resetting this every time?
* (e.g. So that switching back and forth between editors doesn't keep jumping?)
*/
ARegion *region_main = BKE_area_find_region_type(area, RGN_TYPE_WINDOW);
if (region_main) {
/* XXX: Ideally we recenter based on the range instead... */
region_main->v2d.tot.xmin = -2.0f;
region_main->v2d.tot.ymin = -2.0f;
region_main->v2d.tot.xmax = 2.0f;
region_main->v2d.tot.ymax = 2.0f;
region_main->v2d.cur = region_main->v2d.tot;
}
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Active F-Curve
* \{ */
2012-05-08 20:18:33 +00:00
bAnimListElem *get_active_fcurve_channel(bAnimContext *ac)
{
ListBase anim_data = {nullptr, nullptr};
2022-07-08 09:09:59 +10:00
int filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_ACTIVE |
ANIMFILTER_FCURVESONLY);
size_t items = ANIM_animdata_filter(
ac, &anim_data, eAnimFilter_Flags(filter), ac->data, eAnimCont_Types(ac->datatype));
/* We take the first F-Curve only, since some other ones may have had 'active' flag set
* if they were from linked data.
*/
if (items) {
2012-05-08 20:18:33 +00:00
bAnimListElem *ale = (bAnimListElem *)anim_data.first;
/* remove first item from list, then free the rest of the list and return the stored one */
BLI_remlink(&anim_data, ale);
ANIM_animdata_freelist(&anim_data);
return ale;
}
/* no active F-Curve */
return nullptr;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Operator Polling Callbacks
* \{ */
2018-07-02 11:47:00 +02:00
bool graphop_visible_keyframes_poll(bContext *C)
{
bAnimContext ac;
ListBase anim_data = {nullptr, nullptr};
ScrArea *area = CTX_wm_area(C);
size_t items;
int filter;
bool found = false;
/* firstly, check if in Graph Editor */
/* TODO: also check for region? */
if ((area == nullptr) || (area->spacetype != SPACE_GRAPH)) {
return found;
2019-04-22 09:19:45 +10:00
}
/* try to init Anim-Context stuff ourselves and check */
2019-04-22 09:19:45 +10:00
if (ANIM_animdata_get_context(C, &ac) == 0) {
return found;
2019-04-22 09:19:45 +10:00
}
/* loop over the visible (selection doesn't matter) F-Curves, and see if they're suitable
* stopping on the first successful match
*/
Animation: Add GP layers in regular Dopesheet Grease Pencil animation channels are now also shown in the Dopesheet mode of the Dopesheet editor and in the Timeline. Grease pencil related events are now listened not only by container `SACTCONT_GPENCIL` (Grease Pencil Dopesheet), but also `SACTCONT_DOPESHEET` (main Dopesheet), and `SACTCONT_TIMELINE` (timeline). A new Animation Filter flag was added: `ANIMFILTER_FCURVESONLY`. For now this only filters out Grease Pencil Layer channels. **Implemented:** - Preview range set: now only considers selected Grease Pencil keyframes when `onlySel` parameter is true. Not only this allows the operator to work with grease pencil keyframes in main dopesheet, but it also fixes the operator in the Grease Pencil dopesheet. - Translation: allocation (and freeing) of specific memory for translation of Grease Pencil keyframes. - Copy/Paste: call to both Fcurve and GPencil operators, to allow for mixed selection. Errors are only reported when both the FCurve and GPencil functions fail to paste anything. - Keyframe Type change and Insert Keyframe: removed some code here to unify Grease Pencil dopesheet and main dopesheet code. - Jump, Snap, Mirror, Select all/box/lasso/circle, Select left/right, Clickselect: account for Grease Pencil channels within the channels loop, no need for `ANIMFILTER_FCURVESONLY` there. **Not Implemented:** - Graph-related operators. The filter `ANIMFILTER_FCURVESONLY` is naively added to all graph-related operators, meaning more-or-less all operators that used `ANIMFILTER_CURVE_VISIBLE`. - Select linked: is for F-curves channel only - Select more/less: not yet implemented for grease pencil layers. - Clean Keys, Sample, Extrapolation, Interpolation, Easing, and Handle type change: work on Fcurve-channels only, so the `ANIMFILTER_FCURVESONLY` filter is activated Graying out these operators (when no fcurve keyframe is selected) can be done with custom poll functions BUT may affect performance. This is NOT done in this patch. **Dopesheet Summary Selection:** The main summary of the dopesheet now also takes into account Grease Pencil keyframes, using some nasty copy/pasting of code, as explained [on devtalk](https://devtalk.blender.org/t/gpencil-layers-integration-in-main-dopesheet-selection-issue/24527). It works, but may be improved, providing some deeper changes. Reviewed By: mendio, pepeland, sybren Maniphest Tasks: T97477 Differential Revision: https://developer.blender.org/D15003
2022-06-30 15:16:05 +02:00
filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_CURVE_VISIBLE | ANIMFILTER_FCURVESONLY);
items = ANIM_animdata_filter(
&ac, &anim_data, eAnimFilter_Flags(filter), ac.data, eAnimCont_Types(ac.datatype));
2019-04-22 09:19:45 +10:00
if (items == 0) {
return found;
2019-04-22 09:19:45 +10:00
}
LISTBASE_FOREACH (bAnimListElem *, ale, &anim_data) {
2012-05-08 20:18:33 +00:00
FCurve *fcu = (FCurve *)ale->data;
/* visible curves for selection must fulfill the following criteria:
2018-11-14 12:53:15 +11:00
* - it has bezier keyframes
* - F-Curve modifiers do not interfere with the result too much
* (i.e. the modifier-control drawing check returns false)
*/
if (fcu->bezt == nullptr) {
continue;
2019-04-22 09:19:45 +10:00
}
2020-06-05 09:30:15 +02:00
if (BKE_fcurve_are_keyframes_usable(fcu)) {
found = true;
break;
}
}
/* cleanup and return findings */
ANIM_animdata_freelist(&anim_data);
return found;
}
2018-07-02 11:47:00 +02:00
bool graphop_editable_keyframes_poll(bContext *C)
{
bAnimContext ac;
ListBase anim_data = {nullptr, nullptr};
ScrArea *area = CTX_wm_area(C);
size_t items;
int filter;
bool found = false;
/* firstly, check if in Graph Editor or Dopesheet */
/* TODO: also check for region? */
if (area == nullptr || !ELEM(area->spacetype, SPACE_GRAPH, SPACE_ACTION)) {
return found;
2019-04-22 09:19:45 +10:00
}
/* try to init Anim-Context stuff ourselves and check */
2019-04-22 09:19:45 +10:00
if (ANIM_animdata_get_context(C, &ac) == 0) {
return found;
2019-04-22 09:19:45 +10:00
}
/* loop over the editable F-Curves, and see if they're suitable
* stopping on the first successful match
*/
Animation: Add GP layers in regular Dopesheet Grease Pencil animation channels are now also shown in the Dopesheet mode of the Dopesheet editor and in the Timeline. Grease pencil related events are now listened not only by container `SACTCONT_GPENCIL` (Grease Pencil Dopesheet), but also `SACTCONT_DOPESHEET` (main Dopesheet), and `SACTCONT_TIMELINE` (timeline). A new Animation Filter flag was added: `ANIMFILTER_FCURVESONLY`. For now this only filters out Grease Pencil Layer channels. **Implemented:** - Preview range set: now only considers selected Grease Pencil keyframes when `onlySel` parameter is true. Not only this allows the operator to work with grease pencil keyframes in main dopesheet, but it also fixes the operator in the Grease Pencil dopesheet. - Translation: allocation (and freeing) of specific memory for translation of Grease Pencil keyframes. - Copy/Paste: call to both Fcurve and GPencil operators, to allow for mixed selection. Errors are only reported when both the FCurve and GPencil functions fail to paste anything. - Keyframe Type change and Insert Keyframe: removed some code here to unify Grease Pencil dopesheet and main dopesheet code. - Jump, Snap, Mirror, Select all/box/lasso/circle, Select left/right, Clickselect: account for Grease Pencil channels within the channels loop, no need for `ANIMFILTER_FCURVESONLY` there. **Not Implemented:** - Graph-related operators. The filter `ANIMFILTER_FCURVESONLY` is naively added to all graph-related operators, meaning more-or-less all operators that used `ANIMFILTER_CURVE_VISIBLE`. - Select linked: is for F-curves channel only - Select more/less: not yet implemented for grease pencil layers. - Clean Keys, Sample, Extrapolation, Interpolation, Easing, and Handle type change: work on Fcurve-channels only, so the `ANIMFILTER_FCURVESONLY` filter is activated Graying out these operators (when no fcurve keyframe is selected) can be done with custom poll functions BUT may affect performance. This is NOT done in this patch. **Dopesheet Summary Selection:** The main summary of the dopesheet now also takes into account Grease Pencil keyframes, using some nasty copy/pasting of code, as explained [on devtalk](https://devtalk.blender.org/t/gpencil-layers-integration-in-main-dopesheet-selection-issue/24527). It works, but may be improved, providing some deeper changes. Reviewed By: mendio, pepeland, sybren Maniphest Tasks: T97477 Differential Revision: https://developer.blender.org/D15003
2022-06-30 15:16:05 +02:00
filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_CURVE_VISIBLE |
ANIMFILTER_FCURVESONLY);
items = ANIM_animdata_filter(
&ac, &anim_data, eAnimFilter_Flags(filter), ac.data, eAnimCont_Types(ac.datatype));
2019-04-22 09:19:45 +10:00
if (items == 0) {
CTX_wm_operator_poll_msg_set(C, "There is no animation data to operate on");
return found;
2019-04-22 09:19:45 +10:00
}
LISTBASE_FOREACH (bAnimListElem *, ale, &anim_data) {
2012-05-08 20:18:33 +00:00
FCurve *fcu = (FCurve *)ale->data;
/* editable curves must fulfill the following criteria:
2018-11-14 12:53:15 +11:00
* - it has bezier keyframes
* - it must not be protected from editing (this is already checked for with the edit flag
* - F-Curve modifiers do not interfere with the result too much
* (i.e. the modifier-control drawing check returns false)
*/
if (fcu->bezt == nullptr && fcu->fpt != nullptr) {
/* This is a baked curve, it is never editable. */
continue;
2019-04-22 09:19:45 +10:00
}
2020-06-05 09:30:15 +02:00
if (BKE_fcurve_is_keyframable(fcu)) {
found = true;
break;
}
}
/* cleanup and return findings */
ANIM_animdata_freelist(&anim_data);
return found;
}
2018-07-02 11:47:00 +02:00
bool graphop_active_fcurve_poll(bContext *C)
{
bAnimContext ac;
bAnimListElem *ale;
ScrArea *area = CTX_wm_area(C);
bool has_fcurve = false;
/* firstly, check if in Graph Editor */
/* TODO: also check for region? */
if ((area == nullptr) || (area->spacetype != SPACE_GRAPH)) {
return has_fcurve;
2019-04-22 09:19:45 +10:00
}
/* try to init Anim-Context stuff ourselves and check */
2019-04-22 09:19:45 +10:00
if (ANIM_animdata_get_context(C, &ac) == 0) {
return has_fcurve;
2019-04-22 09:19:45 +10:00
}
/* try to get the Active F-Curve */
2012-05-08 20:18:33 +00:00
ale = get_active_fcurve_channel(&ac);
if (ale == nullptr) {
return has_fcurve;
2019-04-22 09:19:45 +10:00
}
/* Do we have a suitable F-Curves?
* - For most cases, NLA Control Curves are sufficiently similar to NLA
* curves to serve this role too. Under the hood, they are F-Curves too.
* The only problems which will arise here are if these need to be
* in an Action too (but drivers would then also be affected!)
*/
has_fcurve = ((ale->data) && ELEM(ale->type, ANIMTYPE_FCURVE, ANIMTYPE_NLACURVE));
Animation Channel Filtering Refactor - Part 3 (Visibility Flag Split) * This (big) commit is aimed at cleaning up the filtering flags used by the animation channel filtering code. The list of filtering flags has been growing a bit "organically" since it's humble origins for use in the Action Editor some 3 years (IIRC) ago now during a weekend hackathon. Obviously, some things have ended up tacked on, while others have been the product of other flag options. Nevertheless, it was time for a bit of a spring clean! * Most notably, one area where the system outgrown its original design for the Action Editor was in terms of the "visibility" filtering flag it was using. While in the Action Editor the concept of what channels to include was strictly dictated by whether the channel hierarchy showed it, in the Graph Editor this is not always the case. In other words, there was a difference between the data the channels represented being visible and the channels for that data being visible in the hierarchy. Long story short: this lead to bug report [#27076] (and many like it), where if you selected an F-Curve, then collapsed the Group it was in, then even after selecting another F-Curve in another Group, the original F-Curve's properties would still be shown in the Properties Region. The good news is that this commit fixes this issue right away! * More good news will follow, as I start checking on the flag usage of other tools, but I'm committing this first so that we have a stable reference (of code similar to the old buggy stuff) on which we can fall back to later to find bugs (should they pop up). Anyways, back to the trenches!
2011-06-22 11:41:26 +00:00
if (has_fcurve) {
2012-05-08 20:18:33 +00:00
FCurve *fcu = (FCurve *)ale->data;
has_fcurve = (fcu->flag & FCURVE_VISIBLE) != 0;
}
/* free temp data... */
MEM_freeN(ale);
/* return success */
return has_fcurve;
}
bool graphop_active_editable_fcurve_ctx_poll(bContext *C)
{
PointerRNA ptr = CTX_data_pointer_get_type(C, "active_editable_fcurve", &RNA_FCurve);
return ptr.data != nullptr;
}
2018-07-02 11:47:00 +02:00
bool graphop_selected_fcurve_poll(bContext *C)
{
bAnimContext ac;
ListBase anim_data = {nullptr, nullptr};
ScrArea *area = CTX_wm_area(C);
size_t items;
int filter;
/* firstly, check if in Graph Editor */
/* TODO: also check for region? */
if ((area == nullptr) || (area->spacetype != SPACE_GRAPH)) {
return false;
2019-04-22 09:19:45 +10:00
}
/* try to init Anim-Context stuff ourselves and check */
2019-04-22 09:19:45 +10:00
if (ANIM_animdata_get_context(C, &ac) == 0) {
return false;
2019-04-22 09:19:45 +10:00
}
/* Get the editable + selected F-Curves, and as long as we got some, we can return.
* NOTE: curve-visible flag isn't included,
* otherwise selecting a curve via list to edit is too cumbersome. */
Animation: Add GP layers in regular Dopesheet Grease Pencil animation channels are now also shown in the Dopesheet mode of the Dopesheet editor and in the Timeline. Grease pencil related events are now listened not only by container `SACTCONT_GPENCIL` (Grease Pencil Dopesheet), but also `SACTCONT_DOPESHEET` (main Dopesheet), and `SACTCONT_TIMELINE` (timeline). A new Animation Filter flag was added: `ANIMFILTER_FCURVESONLY`. For now this only filters out Grease Pencil Layer channels. **Implemented:** - Preview range set: now only considers selected Grease Pencil keyframes when `onlySel` parameter is true. Not only this allows the operator to work with grease pencil keyframes in main dopesheet, but it also fixes the operator in the Grease Pencil dopesheet. - Translation: allocation (and freeing) of specific memory for translation of Grease Pencil keyframes. - Copy/Paste: call to both Fcurve and GPencil operators, to allow for mixed selection. Errors are only reported when both the FCurve and GPencil functions fail to paste anything. - Keyframe Type change and Insert Keyframe: removed some code here to unify Grease Pencil dopesheet and main dopesheet code. - Jump, Snap, Mirror, Select all/box/lasso/circle, Select left/right, Clickselect: account for Grease Pencil channels within the channels loop, no need for `ANIMFILTER_FCURVESONLY` there. **Not Implemented:** - Graph-related operators. The filter `ANIMFILTER_FCURVESONLY` is naively added to all graph-related operators, meaning more-or-less all operators that used `ANIMFILTER_CURVE_VISIBLE`. - Select linked: is for F-curves channel only - Select more/less: not yet implemented for grease pencil layers. - Clean Keys, Sample, Extrapolation, Interpolation, Easing, and Handle type change: work on Fcurve-channels only, so the `ANIMFILTER_FCURVESONLY` filter is activated Graying out these operators (when no fcurve keyframe is selected) can be done with custom poll functions BUT may affect performance. This is NOT done in this patch. **Dopesheet Summary Selection:** The main summary of the dopesheet now also takes into account Grease Pencil keyframes, using some nasty copy/pasting of code, as explained [on devtalk](https://devtalk.blender.org/t/gpencil-layers-integration-in-main-dopesheet-selection-issue/24527). It works, but may be improved, providing some deeper changes. Reviewed By: mendio, pepeland, sybren Maniphest Tasks: T97477 Differential Revision: https://developer.blender.org/D15003
2022-06-30 15:16:05 +02:00
filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_SEL | ANIMFILTER_FOREDIT |
ANIMFILTER_FCURVESONLY);
items = ANIM_animdata_filter(
&ac, &anim_data, eAnimFilter_Flags(filter), ac.data, eAnimCont_Types(ac.datatype));
2019-04-22 09:19:45 +10:00
if (items == 0) {
return false;
2019-04-22 09:19:45 +10:00
}
/* cleanup and return findings */
ANIM_animdata_freelist(&anim_data);
return true;
}
/** \} */