Blender V5.0
graph_utils.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cfloat>
10#include <cstdio>
11#include <cstring>
12
13#include "DNA_anim_types.h"
14#include "DNA_screen_types.h"
15#include "DNA_space_types.h"
16
17#include "MEM_guardedalloc.h"
18
19#include "BLI_listbase.h"
20
21#include "BKE_context.hh"
22#include "BKE_fcurve.hh"
23#include "BKE_screen.hh"
24
25#include "ED_anim_api.hh"
26#include "ED_screen.hh"
27
28#include "UI_interface.hh"
29
30#include "RNA_prototypes.hh"
31
32#include "graph_intern.hh" /* own include */
33
34/* -------------------------------------------------------------------- */
37
39{
40 SpaceGraph *sipo = (SpaceGraph *)area->spacedata.first;
41
42 /* Set mode */
43 sipo->mode = SIPO_MODE_DRIVERS;
44
45 /* Show Properties Region (or else the settings can't be edited) */
46 ARegion *region_props = BKE_area_find_region_type(area, RGN_TYPE_UI);
47 if (region_props) {
48 UI_panel_category_active_set(region_props, "Drivers");
49
50 region_props->flag &= ~RGN_FLAG_HIDDEN;
51 /* XXX: Adjust width of this too? */
52
53 ED_region_visibility_change_update(C, area, region_props);
54 }
55 else {
56 printf("%s: Couldn't find properties region for Drivers Editor - %p\n", __func__, area);
57 }
58
59 /* Adjust framing in graph region */
60 /* TODO: Have a way of not resetting this every time?
61 * (e.g. So that switching back and forth between editors doesn't keep jumping?)
62 */
64 if (region_main) {
65 /* XXX: Ideally we recenter based on the range instead... */
66 region_main->v2d.tot.xmin = -2.0f;
67 region_main->v2d.tot.ymin = -2.0f;
68 region_main->v2d.tot.xmax = 2.0f;
69 region_main->v2d.tot.ymax = 2.0f;
70
71 region_main->v2d.cur = region_main->v2d.tot;
72 }
73}
74
76
77/* -------------------------------------------------------------------- */
80
82{
83 ListBase anim_data = {nullptr, nullptr};
86 size_t items = ANIM_animdata_filter(
87 ac, &anim_data, eAnimFilter_Flags(filter), ac->data, eAnimCont_Types(ac->datatype));
88
89 /* We take the first F-Curve only, since some other ones may have had 'active' flag set
90 * if they were from linked data.
91 */
92 if (items) {
93 bAnimListElem *ale = (bAnimListElem *)anim_data.first;
94
95 /* remove first item from list, then free the rest of the list and return the stored one */
96 BLI_remlink(&anim_data, ale);
97 ANIM_animdata_freelist(&anim_data);
98
99 return ale;
100 }
101
102 /* no active F-Curve */
103 return nullptr;
104}
105
107
108/* -------------------------------------------------------------------- */
111
113{
114 bAnimContext ac;
115 ListBase anim_data = {nullptr, nullptr};
116 ScrArea *area = CTX_wm_area(C);
117 size_t items;
118 int filter;
119 bool found = false;
120
121 /* firstly, check if in Graph Editor */
122 /* TODO: also check for region? */
123 if ((area == nullptr) || (area->spacetype != SPACE_GRAPH)) {
124 return found;
125 }
126
127 /* try to init Anim-Context stuff ourselves and check */
128 if (ANIM_animdata_get_context(C, &ac) == 0) {
129 return found;
130 }
131
132 /* loop over the visible (selection doesn't matter) F-Curves, and see if they're suitable
133 * stopping on the first successful match
134 */
136 items = ANIM_animdata_filter(
137 &ac, &anim_data, eAnimFilter_Flags(filter), ac.data, eAnimCont_Types(ac.datatype));
138 if (items == 0) {
139 return found;
140 }
141
142 LISTBASE_FOREACH (bAnimListElem *, ale, &anim_data) {
143 const FCurve *fcu = (const FCurve *)ale->data;
144
145 /* visible curves for selection must fulfill the following criteria:
146 * - it has bezier keyframes
147 * - F-Curve modifiers do not interfere with the result too much
148 * (i.e. the modifier-control drawing check returns false)
149 */
150 if (fcu->bezt == nullptr) {
151 continue;
152 }
154 found = true;
155 break;
156 }
157 }
158
159 /* cleanup and return findings */
160 ANIM_animdata_freelist(&anim_data);
161 return found;
162}
163
165{
166 bAnimContext ac;
167 ListBase anim_data = {nullptr, nullptr};
168 ScrArea *area = CTX_wm_area(C);
169 size_t items;
170 int filter;
171 bool found = false;
172
173 /* Firstly, check if in Graph Editor or Dope-sheet. */
174 /* TODO: also check for region? */
175 if (area == nullptr || !ELEM(area->spacetype, SPACE_GRAPH, SPACE_ACTION)) {
176 return found;
177 }
178
179 /* try to init Anim-Context stuff ourselves and check */
180 if (ANIM_animdata_get_context(C, &ac) == 0) {
181 return found;
182 }
183
184 /* loop over the editable F-Curves, and see if they're suitable
185 * stopping on the first successful match
186 */
189 items = ANIM_animdata_filter(
190 &ac, &anim_data, eAnimFilter_Flags(filter), ac.data, eAnimCont_Types(ac.datatype));
191 if (items == 0) {
192 CTX_wm_operator_poll_msg_set(C, "There is no animation data to operate on");
193 return found;
194 }
195
196 LISTBASE_FOREACH (bAnimListElem *, ale, &anim_data) {
197 const FCurve *fcu = (const FCurve *)ale->data;
198
199 /* editable curves must fulfill the following criteria:
200 * - it has bezier keyframes
201 * - it must not be protected from editing (this is already checked for with the edit flag
202 * - F-Curve modifiers do not interfere with the result too much
203 * (i.e. the modifier-control drawing check returns false)
204 */
205 if (fcu->bezt == nullptr && fcu->fpt != nullptr) {
206 /* This is a baked curve, it is never editable. */
207 continue;
208 }
209 if (BKE_fcurve_is_keyframable(fcu)) {
210 found = true;
211 break;
212 }
213 }
214
215 /* cleanup and return findings */
216 ANIM_animdata_freelist(&anim_data);
217 return found;
218}
219
221{
222 bAnimContext ac;
223 bAnimListElem *ale;
224 ScrArea *area = CTX_wm_area(C);
225 bool has_fcurve = false;
226
227 /* firstly, check if in Graph Editor */
228 /* TODO: also check for region? */
229 if ((area == nullptr) || (area->spacetype != SPACE_GRAPH)) {
230 return has_fcurve;
231 }
232
233 /* try to init Anim-Context stuff ourselves and check */
234 if (ANIM_animdata_get_context(C, &ac) == 0) {
235 return has_fcurve;
236 }
237
238 /* try to get the Active F-Curve */
239 ale = get_active_fcurve_channel(&ac);
240 if (ale == nullptr) {
241 return has_fcurve;
242 }
243
244 /* Do we have a suitable F-Curves?
245 * - For most cases, NLA Control Curves are sufficiently similar to NLA
246 * curves to serve this role too. Under the hood, they are F-Curves too.
247 * The only problems which will arise here are if these need to be
248 * in an Action too (but drivers would then also be affected!)
249 */
250 has_fcurve = ((ale->data) && ELEM(ale->type, ANIMTYPE_FCURVE, ANIMTYPE_NLACURVE));
251 if (has_fcurve) {
252 const FCurve *fcu = (const FCurve *)ale->data;
253 has_fcurve = (fcu->flag & FCURVE_VISIBLE) != 0;
254 }
255
256 /* free temp data... */
257 MEM_freeN(ale);
258
259 /* return success */
260 return has_fcurve;
261}
262
264{
265 PointerRNA ptr = CTX_data_pointer_get_type(C, "active_editable_fcurve", &RNA_FCurve);
266
267 return ptr.data != nullptr;
268}
269
271{
272 bAnimContext ac;
273 ListBase anim_data = {nullptr, nullptr};
274 ScrArea *area = CTX_wm_area(C);
275 size_t items;
276 int filter;
277
278 /* firstly, check if in Graph Editor */
279 /* TODO: also check for region? */
280 if ((area == nullptr) || (area->spacetype != SPACE_GRAPH)) {
281 return false;
282 }
283
284 /* try to init Anim-Context stuff ourselves and check */
285 if (ANIM_animdata_get_context(C, &ac) == 0) {
286 return false;
287 }
288
289 /* Get the editable + selected F-Curves, and as long as we got some, we can return.
290 * NOTE: curve-visible flag isn't included,
291 * otherwise selecting a curve via list to edit is too cumbersome. */
294 items = ANIM_animdata_filter(
295 &ac, &anim_data, eAnimFilter_Flags(filter), ac.data, eAnimCont_Types(ac.datatype));
296 if (items == 0) {
297 return false;
298 }
299
300 /* cleanup and return findings */
301 ANIM_animdata_freelist(&anim_data);
302 return true;
303}
304
PointerRNA CTX_data_pointer_get_type(const bContext *C, const char *member, StructRNA *type)
void CTX_wm_operator_poll_msg_set(bContext *C, const char *msg)
ScrArea * CTX_wm_area(const bContext *C)
bool BKE_fcurve_is_keyframable(const FCurve *fcu)
bool BKE_fcurve_are_keyframes_usable(const FCurve *fcu)
ARegion * BKE_area_find_region_type(const ScrArea *area, int region_type)
Definition screen.cc:846
#define LISTBASE_FOREACH(type, var, list)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:131
#define ELEM(...)
@ FCURVE_VISIBLE
@ RGN_TYPE_UI
@ RGN_TYPE_WINDOW
@ RGN_FLAG_HIDDEN
@ SPACE_ACTION
@ SPACE_GRAPH
@ SIPO_MODE_DRIVERS
@ ANIMTYPE_NLACURVE
@ ANIMTYPE_FCURVE
eAnimCont_Types
eAnimFilter_Flags
@ ANIMFILTER_ACTIVE
@ ANIMFILTER_FOREDIT
@ ANIMFILTER_DATA_VISIBLE
@ ANIMFILTER_CURVE_VISIBLE
@ ANIMFILTER_FCURVESONLY
@ ANIMFILTER_SEL
void ED_region_visibility_change_update(bContext *C, ScrArea *area, ARegion *region)
Definition area.cc:2351
Read Guarded memory(de)allocation.
#define C
Definition RandGen.cpp:29
void UI_panel_category_active_set(ARegion *region, const char *idname)
void ANIM_animdata_freelist(ListBase *anim_data)
Definition anim_deps.cc:463
bool ANIM_animdata_get_context(const bContext *C, bAnimContext *ac)
size_t ANIM_animdata_filter(bAnimContext *ac, ListBase *anim_data, const eAnimFilter_Flags filter_mode, void *data, const eAnimCont_Types datatype)
#define filter
#define printf(...)
bool graphop_visible_keyframes_poll(bContext *C)
bool graphop_active_editable_fcurve_ctx_poll(bContext *C)
bAnimListElem * get_active_fcurve_channel(bAnimContext *ac)
bool graphop_editable_keyframes_poll(bContext *C)
bool graphop_active_fcurve_poll(bContext *C)
bool graphop_selected_fcurve_poll(bContext *C)
void ED_drivers_editor_init(bContext *C, ScrArea *area)
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
FPoint * fpt
BezTriple * bezt
void * first
ListBase spacedata
eAnimCont_Types datatype
eAnim_ChannelType type
float xmax
float xmin
float ymax
float ymin
PointerRNA * ptr
Definition wm_files.cc:4238