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