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