Blender V5.3
interface_anim.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdio>
10#include <cstdlib>
11#include <cstring>
12
13#include "DNA_anim_types.h"
14#include "DNA_screen_types.h"
15
16#include "BLI_listbase.hh"
17#include "BLI_string.hh"
18#include "BLI_string_utf8.hh"
19#include "BLI_utildefines.hh"
20
21#include "BKE_animsys.hh"
22#include "BKE_context.hh"
23#include "BKE_fcurve.hh"
24#include "BKE_fcurve_driver.h"
25#include "BKE_global.hh"
26#include "BKE_nla.hh"
27
29
30#include "ED_keyframing.hh"
31
32#include "ANIM_fcurve.hh"
33#include "ANIM_keyframing.hh"
34
35#include "RNA_access.hh"
36#include "RNA_path.hh"
37
38#include "WM_api.hh"
39#include "WM_types.hh"
40
41#include "interface_intern.hh"
42
43namespace blender::ui {
44
46 Button *but, AnimData **adt, bAction **action, bool *r_driven, bool *r_special)
47{
48 /* for entire array buttons we check the first component, it's not perfect
49 * but works well enough in typical cases */
50 const int rnaindex = (but->rnaindex == -1) ? 0 : but->rnaindex;
51
52 return BKE_fcurve_find_by_rna_context_ui(static_cast<bContext *>(but->block->evil_C),
53 &but->rnapoin,
54 but->rnaprop,
55 rnaindex,
56 adt,
57 action,
58 r_driven,
59 r_special);
60}
61
62void button_anim_flag(Button *but, const AnimationEvalContext *anim_eval_context)
63{
64 /* Clear the flags that this function might set. */
67
68 /* NOTE: "special" is reserved for special F-Curves stored on the animation data
69 * itself (which are used to animate properties of the animation data).
70 * We count those as "animated" too for now
71 */
72 AnimData *adt;
73 bAction *act;
74 bool driven;
75 bool special;
76 FCurve *fcu = but_get_fcurve(but, &adt, &act, &driven, &special);
77
78 if (!fcu) {
79 return;
80 }
81 if (driven) {
82 but->flag |= BUT_DRIVEN;
83 return;
84 }
85
86 /* Empty curves are ignored by the animation evaluation system. */
87 if (BKE_fcurve_is_empty(fcu)) {
88 return;
89 }
90
91 but->flag |= BUT_ANIMATED;
92
93 /* #41525 - When the active action is a NLA strip being edited,
94 * we need to correct the frame number to "look inside" the
95 * remapped action
96 */
97 float cfra = anim_eval_context->eval_time;
98 if (adt) {
100 }
101
102 if (animrig::fcurve_frame_has_keyframe(fcu, cfra)) {
103 but->flag |= BUT_ANIMATED_KEY;
104 }
105
106 /* This feature is not implemented at all for the NLA. However, if the NLA just consists of
107 * stashed (i.e. deactivated) Actions, it doesn't do anything, and we can treat it as
108 * non-existent here. Note that this is mostly to play nice with stashed Actions, and doesn't
109 * fully look at all the track & strip flags. */
110 if (adt) {
111 for (NlaTrack &nla_track : adt->nla_tracks) {
112 if (!(nla_track.flag & NLATRACK_MUTED)) {
113 /* Found a non-muted track, so this NLA is not purely for stashing Actions. */
114 return;
115 }
116 }
117 }
118
120 anim_eval_context, cfra);
121 if (fcurve_is_changed(but->rnapoin, but->rnaprop, fcu, &remapped_context)) {
123 }
124}
125
127{
128 Button *but_iter = nullptr;
129
132 if (but->block->buttons_ptrs.is_empty()) {
133 return nullptr;
134 }
135 int i = but->block->but_index(but);
136 i = i > 0 ? i - 1 : but->block->buttons_ptrs.size() - 1;
137 const int start = i;
138 do {
139 but_iter = but->block->buttons_ptrs[i].get();
140 if (but_iter != but &&
142 but_iter, &but->decorated_rnapoin, but->decorated_rnaprop, but->decorated_rnaindex))
143 {
144 return but_iter;
145 }
146 i = i > 0 ? i - 1 : but->block->buttons_ptrs.size() - 1;
147 } while (i != start);
148
149 return nullptr;
150}
151
153{
154 if (!but->decorated_rnapoin || !but->decorated_rnaprop) {
155 /* Nothing to do. */
156 return;
157 }
158
159 const Button *but_anim = but_anim_decorate_find_attached_button(but);
160
161 if (!but_anim) {
162 printf("Could not find button with matching property to decorate (%s.%s)\n",
165 return;
166 }
167
168 const int64_t flag = but_anim->flag;
169
170 if (flag & BUT_DRIVEN) {
171 but->icon = ICON_DECORATE_DRIVER;
172 but->toggle_keyframe_on_click = false;
173 }
174 else if (flag & BUT_ANIMATED_KEY) {
175 but->icon = ICON_DECORATE_KEYFRAME;
176 but->toggle_keyframe_on_click = true;
177 }
178 else if (flag & BUT_ANIMATED) {
179 but->icon = ICON_DECORATE_ANIMATE;
180 but->toggle_keyframe_on_click = true;
181 }
182 else if (flag & BUT_OVERRIDDEN) {
183 but->icon = ICON_DECORATE_OVERRIDE;
184 but->toggle_keyframe_on_click = false;
185 }
186 else {
187 but->icon = ICON_DECORATE;
188 but->toggle_keyframe_on_click = true;
189 }
190
191 const int64_t flag_copy = (BUT_DISABLED | BUT_INACTIVE);
192 but->flag = (but->flag & ~flag_copy) | (flag & flag_copy);
193}
194
196{
197 const Button *but_anim = but_anim_decorate_find_attached_button(but);
198 if (but_anim == nullptr) [[unlikely]] {
199 return false;
200 }
201 return (but_anim->flag & BUT_ANIMATED_KEY) != 0;
202}
203
204bool button_anim_expression_get(Button *but, char *str, size_t str_maxncpy)
205{
206 FCurve *fcu;
207 ChannelDriver *driver;
208 bool driven, special;
209
210 fcu = but_get_fcurve(but, nullptr, nullptr, &driven, &special);
211
212 if (fcu && driven) {
213 driver = fcu->driver;
214
215 if (driver && driver->type == DRIVER_TYPE_PYTHON) {
216 if (str) {
217 BLI_strncpy_utf8(str, driver->expression, str_maxncpy);
218 }
219 return true;
220 }
221 }
222
223 return false;
224}
225
226bool button_anim_expression_set(Button *but, const char *str)
227{
228 FCurve *fcu;
229 ChannelDriver *driver;
230 bool driven, special;
231
232 fcu = but_get_fcurve(but, nullptr, nullptr, &driven, &special);
233
234 if (fcu && driven) {
235 driver = fcu->driver;
236
237 if (driver && (driver->type == DRIVER_TYPE_PYTHON)) {
238 bContext *C = static_cast<bContext *>(but->block->evil_C);
239
240 STRNCPY_UTF8(driver->expression, str);
241
242 /* tag driver as needing to be recompiled */
243 BKE_driver_invalidate_expression(driver, true, false);
244
245 /* clear invalid flags which may prevent this from working */
246 driver->flag &= ~DRIVER_FLAG_INVALID;
247 fcu->flag &= ~FCURVE_DISABLED;
248
249 /* this notifier should update the Graph Editor and trigger depsgraph refresh? */
251
253
254 return true;
255 }
256 }
257
258 return false;
259}
260
262{
263 bContext *C = static_cast<bContext *>(but->block->evil_C);
264 ID *id;
265 FCurve *fcu;
266 bool ok = false;
267
268 /* button must have RNA-pointer to a numeric-capable property */
269 if (!but->rnapoin || !but->rnaprop) {
270 if (G.debug & G_DEBUG) {
271 printf("ERROR: create expression failed - button has no RNA info attached\n");
272 }
273 return false;
274 }
275
276 if (RNA_property_array_check(but->rnaprop) != 0) {
277 if (but->rnaindex == -1) {
278 if (G.debug & G_DEBUG) {
279 printf("ERROR: create expression failed - cannot create expression for entire array\n");
280 }
281 return false;
282 }
283 }
284
285 /* Make sure we have animation-data for this. */
286 /* FIXME: until materials can be handled by depsgraph,
287 * don't allow drivers to be created for them. */
288 id = but->rnapoin.owner_id;
289 if ((id == nullptr) || (GS(id->name) == ID_MA) || (GS(id->name) == ID_TE)) {
290 if (G.debug & G_DEBUG) {
291 printf("ERROR: create expression failed - invalid data-block for adding drivers (%p)\n", id);
292 }
293 return false;
294 }
295
296 /* get path */
297 const std::optional<std::string> path = RNA_path_from_ID_to_property(&but->rnapoin,
298 but->rnaprop);
299 if (!path) {
300 return false;
301 }
302
303 /* create driver */
304 fcu = verify_driver_fcurve(id, path->c_str(), but->rnaindex, DRIVER_FCURVE_KEYFRAMES);
305 if (fcu) {
306 ChannelDriver *driver = fcu->driver;
307
308 if (driver) {
309 /* set type of driver */
310 driver->type = DRIVER_TYPE_PYTHON;
311
312 /* set the expression */
313 /* TODO: need some way of identifying variables used */
314 STRNCPY_UTF8(driver->expression, str);
315
316 /* updates */
317 BKE_driver_invalidate_expression(driver, true, false);
320 ok = true;
321 }
322 }
323
324 return ok;
325}
326
327void button_anim_autokey(bContext *C, Button *but, Scene *scene, float cfra)
328{
329 animrig::autokeyframe_property(C, scene, &but->rnapoin, but->rnaprop, but->rnaindex, cfra, true);
330}
331
333{
334 /* this operator calls context_active_but_prop_get */
336 C, "ANIM_OT_copy_driver_button", wm::OpCallContext::InvokeDefault, nullptr, nullptr);
337}
338
340{
341 /* this operator calls context_active_but_prop_get */
343 C, "ANIM_OT_paste_driver_button", wm::OpCallContext::InvokeDefault, nullptr, nullptr);
344}
345
346void button_anim_decorate_cb(bContext *C, void *arg_but, void * /*arg_dummy*/)
347{
349 auto *but_decorate = static_cast<ButtonDecorator *>(arg_but);
350 if (!but_decorate->toggle_keyframe_on_click) {
351 return;
352 }
353
354 Button *but_anim = but_anim_decorate_find_attached_button(but_decorate);
355 if (!but_anim) {
356 return;
357 }
358
359 /* While click drag the active button may not be `but_decorate`, instead is the but where the
360 * drag started, temporarily override `but_anim` as active. */
361 but_anim->flag |= BUT_ACTIVE_OVERRIDE;
362 wm->op_undo_depth++;
363
364 if (but_anim->flag & BUT_ANIMATED_KEY) {
365 wmOperatorType *ot = WM_operatortype_find("ANIM_OT_keyframe_delete_button", false);
367 RNA_boolean_set(&props_ptr, "all", but_anim->rnaindex == -1);
369 WM_operator_properties_free(&props_ptr);
370 }
371 else {
372 wmOperatorType *ot = WM_operatortype_find("ANIM_OT_keyframe_insert_button", false);
374 RNA_boolean_set(&props_ptr, "all", but_anim->rnaindex == -1);
376 WM_operator_properties_free(&props_ptr);
377 }
378
379 but_anim->flag &= ~BUT_ACTIVE_OVERRIDE;
380 wm->op_undo_depth--;
381}
382
383} // namespace blender::ui
Functions to modify FCurves.
Functions to insert, delete or modify keyframes.
#define STRNCPY_UTF8(dst, src)
#define C
Definition RandGen.cpp:29
#define button_is_decorator(but)
#define NC_ANIMATION
Definition WM_types.hh:401
#define NA_ADDED
Definition WM_types.hh:636
#define ND_KEYFRAME
Definition WM_types.hh:537
#define str(s)
bool fcurve_frame_has_keyframe(const FCurve *fcu, float frame)
bool autokeyframe_property(bContext *C, Scene *scene, PointerRNA *ptr, PropertyRNA *prop, int rnaindex, float cfra, bool only_if_property_keyed)
void button_anim_decorate_update_from_flag(ButtonDecorator *but)
void button_anim_copy_driver(bContext *C)
void button_anim_paste_driver(bContext *C)
void button_anim_flag(Button *but, const AnimationEvalContext *anim_eval_context)
void button_anim_autokey(bContext *C, Button *but, Scene *scene, float cfra)
void button_anim_decorate_cb(bContext *C, void *arg_but, void *)
bool button_anim_expression_create(Button *but, const char *str)
static FCurve * but_get_fcurve(Button *but, AnimData **adt, bAction **action, bool *r_driven, bool *r_special)
bool button_anim_expression_set(Button *but, const char *str)
bool button_anim_expression_get(Button *but, char *str, size_t str_maxncpy)
bool button_rna_equals_ex(const Button *but, const PointerRNA *ptr, const PropertyRNA *prop, int index)
Definition interface.cc:795
static Button * but_anim_decorate_find_attached_button(ButtonDecorator *but)
bool button_anim_decorate_pushed_state(ButtonDecorator *but)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
@ DRIVER_FLAG_INVALID
float BKE_nla_tweakedit_remap(AnimData *adt, float cframe, eNlaTime_ConvertModes mode)
AnimationEvalContext BKE_animsys_eval_context_construct_at(const AnimationEvalContext *anim_eval_context, float eval_time) ATTR_WARN_UNUSED_RESULT
Definition anim_sys.cc:610
void BKE_driver_invalidate_expression(struct ChannelDriver *driver, bool expr_changed, bool varname_changed)
std::optional< std::string > RNA_path_from_ID_to_property(const PointerRNA *ptr, PropertyRNA *prop)
Definition rna_path.cc:1237
void WM_operator_properties_free(PointerRNA *ptr)
const char * RNA_property_identifier(const PropertyRNA *prop)
FCurve * verify_driver_fcurve(ID *id, const char rna_path[], const int array_index, eDriverFCurveCreationMode creation_mode)
Definition drivers.cc:53
bool fcurve_is_changed(PointerRNA ptr, PropertyRNA *prop, FCurve *fcu, const AnimationEvalContext *anim_eval_context)
Lesser Keyframe Checking API call.
@ NLATIME_CONVERT_UNMAP
Definition BKE_nla.hh:560
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
PointerRNA WM_operator_properties_create_ptr(wmOperatorType *ot)
bool RNA_property_array_check(PropertyRNA *prop)
Global G
Definition blender.cc:57
wmWindowManager * CTX_wm_manager(const bContext *C)
wmOperatorStatus WM_operator_name_call(bContext *C, const char *opstring, wm::OpCallContext context, PointerRNA *properties, const wmEvent *event)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void RNA_boolean_set(PointerRNA *ptr, const char *name, bool value)
const char * RNA_struct_identifier(const StructRNA *type)
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)
Main * CTX_data_main(const bContext *C)
wmOperatorStatus WM_operator_name_call_ptr(bContext *C, wmOperatorType *ot, wm::OpCallContext context, PointerRNA *properties, const wmEvent *event)
ID_Type GS(const char *name)
Definition DNA_ID.h:410
void DEG_relations_tag_update(Main *bmain)
bool BKE_fcurve_is_empty(const FCurve *fcu)
FCurve * BKE_fcurve_find_by_rna_context_ui(bContext *C, const PointerRNA *ptr, PropertyRNA *prop, int rnaindex, AnimData **r_animdata, bAction **r_action, bool *r_driven, bool *r_special)
@ DRIVER_FCURVE_KEYFRAMES
ListBaseT< NlaTrack > nla_tracks
eFCurve_Flags flag
ChannelDriver * driver
eNlaTrack_Flag flag
StructRNA * type
Definition RNA_types.hh:63
int but_index(const Button *but) const
Definition interface.cc:276
Vector< std::unique_ptr< Button > > buttons_ptrs