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