Blender V5.3
drivers.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009 Blender Authors, Joshua Leung. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cctype>
10#include <cstdio>
11#include <cstring>
12
13#include "MEM_guardedalloc.h"
14
15#include "BLI_listbase.hh"
16#include "BLI_string.hh"
17#include "BLI_string_utf8.hh"
18#include "BLI_utildefines.hh"
19
20#include "DNA_anim_types.h"
21
22#include "BKE_anim_data.hh"
23#include "BKE_context.hh"
24#include "BKE_fcurve.hh"
25#include "BKE_fcurve_driver.h"
26#include "BKE_report.hh"
27
28#include "DEG_depsgraph.hh"
30
31#include "ED_keyframing.hh"
32
33#include "UI_interface.hh"
34#include "UI_resources.hh"
35
36#include "WM_api.hh"
37#include "WM_types.hh"
38
39#include "RNA_access.hh"
40#include "RNA_define.hh"
41#include "RNA_path.hh"
42#include "RNA_prototypes.hh"
43
44#include "ANIM_fcurve.hh"
45
46#include "anim_intern.hh"
47
48namespace blender {
49
50/* ************************************************** */
51/* Animation Data Validation */
52
54 const char rna_path[],
55 const int array_index,
56 eDriverFCurveCreationMode creation_mode)
57{
58 AnimData *adt;
59 FCurve *fcu;
60
61 /* sanity checks */
62 if (ELEM(nullptr, id, rna_path)) {
63 return nullptr;
64 }
65
66 /* init animdata if none available yet */
67 adt = BKE_animdata_from_id(id);
68 if (adt == nullptr && creation_mode != DRIVER_FCURVE_LOOKUP_ONLY) {
69 adt = BKE_animdata_ensure_id(id);
70 }
71 if (adt == nullptr) {
72 /* if still none (as not allowed to add, or ID doesn't have animdata for some reason) */
73 return nullptr;
74 }
75
76 /* try to find f-curve matching for this setting
77 * - add if not found and allowed to add one
78 * TODO: add auto-grouping support? how this works will need to be resolved
79 */
80 fcu = BKE_fcurve_find(&adt->drivers, rna_path, array_index);
81
82 if (fcu == nullptr && creation_mode != DRIVER_FCURVE_LOOKUP_ONLY) {
83 /* use default settings to make a F-Curve */
84 fcu = alloc_driver_fcurve(rna_path, array_index, creation_mode);
85
86 /* just add F-Curve to end of driver list */
87 BLI_addtail(&adt->drivers, fcu);
88 }
89
90 /* return the F-Curve */
91 return fcu;
92}
93
95 const int array_index,
96 eDriverFCurveCreationMode creation_mode)
97{
98 using namespace blender::animrig;
100
102 fcu->auto_smoothing = U.auto_smoothing_new;
103
104 /* store path - make copy, and store that */
105 if (rna_path) {
106 fcu->rna_path_set(rna_path);
107 }
108 fcu->array_index = array_index;
109
110 if (!ELEM(creation_mode, DRIVER_FCURVE_LOOKUP_ONLY, DRIVER_FCURVE_EMPTY)) {
111 /* add some new driver data */
112 fcu->driver = MEM_new<ChannelDriver>("ChannelDriver");
113
114 /* Add 2 keyframes so that user has something to work with
115 * - These are configured to 0,0 and 1,1 to give a 1-1 mapping
116 * which can be easily tweaked from there.
117 */
118 const KeyframeSettings settings = get_keyframe_settings(false);
119 insert_vert_fcurve(fcu, {0.0f, 0.0f}, settings, INSERTKEY_FAST);
120 insert_vert_fcurve(fcu, {1.0f, 1.0f}, settings, INSERTKEY_FAST);
123 }
124
125 return fcu;
126}
127
128/* ************************************************** */
129/* Driver Management API */
130
131/* Helper for ANIM_add_driver_with_target - Adds the actual driver */
132static int add_driver_with_target(ReportList * /*reports*/,
133 ID *dst_id,
134 const char dst_path[],
135 int dst_index,
136 ID *src_id,
137 const char src_path[],
138 int src_index,
139 PointerRNA *dst_ptr,
140 PropertyRNA *dst_prop,
141 PointerRNA *src_ptr,
142 PropertyRNA *src_prop,
143 eDriver_Types driver_type)
144{
145 FCurve *fcu;
146 const char *prop_name = RNA_property_identifier(src_prop);
147
148 /* Create F-Curve with Driver */
149 fcu = verify_driver_fcurve(dst_id, dst_path, dst_index, DRIVER_FCURVE_KEYFRAMES);
150
151 if (fcu && fcu->driver) {
152 ChannelDriver *driver = fcu->driver;
153 DriverVar *dvar;
154
155 /* Set the type of the driver */
156 driver->type = driver_type;
157
158 /* Set driver expression, so that the driver works out of the box
159 *
160 * The following checks define a bit of "auto-detection magic" we use
161 * to ensure that the drivers will behave as expected out of the box
162 * when faced with properties with different units.
163 */
164 /* XXX: if we have N-1 mapping, should we include all those in the expression? */
165 if ((RNA_property_unit(dst_prop) == PROP_UNIT_ROTATION) &&
167 {
168 /* Rotation Destination: normal -> radians, so convert src to radians
169 * (However, if both input and output is a rotation, don't apply such corrections)
170 */
171 STRNCPY_UTF8(driver->expression, "radians(var)");
172 }
173 else if ((RNA_property_unit(src_prop) == PROP_UNIT_ROTATION) &&
175 {
176 /* Rotation Source: radians -> normal, so convert src to degrees
177 * (However, if both input and output is a rotation, don't apply such corrections)
178 */
179 STRNCPY_UTF8(driver->expression, "degrees(var)");
180 }
181 else {
182 /* Just a normal property without any unit problems */
183 STRNCPY_UTF8(driver->expression, "var");
184 }
185
186 /* Create a driver variable for the target
187 * - For transform properties, we want to automatically use "transform channel" instead
188 * (The only issue is with quaternion rotations vs euler channels...)
189 * - To avoid problems with transform properties depending on the final transform that they
190 * control (thus creating pseudo-cycles - see #48734), we don't use transform channels
191 * when both the source and destinations are in same places.
192 */
193 dvar = driver_add_new_variable(driver);
194
195 if (ELEM(src_ptr->type, RNA_Object, RNA_PoseBone) &&
196 (STREQ(prop_name, "location") || STREQ(prop_name, "scale") ||
197 STRPREFIX(prop_name, "rotation_")) &&
198 (src_ptr->data != dst_ptr->data))
199 {
200 /* Transform Channel */
201 DriverTarget *dtar;
202
204 dtar = &dvar->targets[0];
205
206 /* Bone or Object target? */
207 dtar->id = src_id;
208 dtar->idtype = GS(src_id->name);
209
210 if (src_ptr->type == RNA_PoseBone) {
211 RNA_string_get(src_ptr, "name", dtar->pchan_name);
212 }
213
214 /* Transform channel depends on type */
215 if (STREQ(prop_name, "location")) {
216 if (src_index == 2) {
218 }
219 else if (src_index == 1) {
221 }
222 else {
224 }
225 }
226 else if (STREQ(prop_name, "scale")) {
227 if (src_index == 2) {
229 }
230 else if (src_index == 1) {
232 }
233 else {
235 }
236 }
237 else {
238 /* XXX: With quaternions and axis-angle, this mapping might not be correct...
239 * But since those have 4 elements instead, there's not much we can do
240 */
241 if (src_index == 2) {
243 }
244 else if (src_index == 1) {
246 }
247 else {
249 }
250 }
251 }
252 else {
253 /* Single RNA Property */
254 DriverTarget *dtar = &dvar->targets[0];
255
256 /* ID is as-is */
257 dtar->id = src_id;
258 dtar->idtype = GS(src_id->name);
259
260 /* Need to make a copy of the path (or build one with array index built in) */
261 if (RNA_property_array_check(src_prop)) {
262 dtar->rna_path = BLI_sprintfN("%s[%d]", src_path, src_index);
263 }
264 else {
265 dtar->rna_path = BLI_strdup(src_path);
266 }
267 }
268 }
269
270 /* set the done status */
271 return (fcu != nullptr);
272}
273
275 ID *dst_id,
276 const char dst_path[],
277 int dst_index,
278 ID *src_id,
279 const char src_path[],
280 int src_index,
281 short flag,
282 eDriver_Types driver_type,
283 short mapping_type)
284{
285 PointerRNA ptr;
286 PropertyRNA *prop;
287
288 PointerRNA ptr2;
289 PropertyRNA *prop2;
290 int done_tot = 0;
291
292 /* validate pointers first - exit if failure */
293 PointerRNA id_ptr = RNA_id_pointer_create(dst_id);
294 if (RNA_path_resolve_property(&id_ptr, dst_path, &ptr, &prop) == false) {
296 reports,
297 RPT_ERROR,
298 "Could not add driver, as RNA path is invalid for the given ID (ID = %s, path = %s)",
299 dst_id->name,
300 dst_path);
301 return 0;
302 }
303
304 PointerRNA id_ptr2 = RNA_id_pointer_create(src_id);
305 if ((RNA_path_resolve_property(&id_ptr2, src_path, &ptr2, &prop2) == false) ||
306 (mapping_type == CREATEDRIVER_MAPPING_NONE))
307 {
308 /* No target - So, fall back to default method for adding a "simple" driver normally */
309 return ANIM_add_driver(
310 reports, dst_id, dst_path, dst_index, flag | CREATEDRIVER_WITH_DEFAULT_DVAR, driver_type);
311 }
312
313 /* handle curve-property mappings based on mapping_type */
314 switch (mapping_type) {
315 /* N-N - Try to match as much as possible, then use the first one. */
317 /* Use the shorter of the two (to avoid out of bounds access) */
318 int dst_len = RNA_property_array_check(prop) ? RNA_property_array_length(&ptr, prop) : 1;
319 int src_len = RNA_property_array_check(prop) ? RNA_property_array_length(&ptr2, prop2) : 1;
320
321 int len = std::min(dst_len, src_len);
322
323 for (int i = 0; i < len; i++) {
324 done_tot += add_driver_with_target(reports,
325 dst_id,
326 dst_path,
327 i,
328 src_id,
329 src_path,
330 i,
331 &ptr,
332 prop,
333 &ptr2,
334 prop2,
335 driver_type);
336 }
337 break;
338 }
339 /* 1-N - Specified target index for all. */
341 default: {
342 int len = RNA_property_array_check(prop) ? RNA_property_array_length(&ptr, prop) : 1;
343
344 for (int i = 0; i < len; i++) {
345 done_tot += add_driver_with_target(reports,
346 dst_id,
347 dst_path,
348 i,
349 src_id,
350 src_path,
351 src_index,
352 &ptr,
353 prop,
354 &ptr2,
355 prop2,
356 driver_type);
357 }
358 break;
359 }
360
361 /* 1-1 - Use the specified index (unless -1). */
363 done_tot = add_driver_with_target(reports,
364 dst_id,
365 dst_path,
366 dst_index,
367 src_id,
368 src_path,
369 src_index,
370 &ptr,
371 prop,
372 &ptr2,
373 prop2,
374 driver_type);
375 break;
376 }
377 }
378
379 /* done */
380 return done_tot;
381}
382
383/* --------------------------------- */
384
386 ID *id,
387 const char rna_path[],
388 int array_index,
389 short flag,
390 eDriver_Types type)
391{
392 PointerRNA ptr;
393 PropertyRNA *prop;
394 FCurve *fcu;
395 int array_index_max;
396 int done_tot = 0;
397
398 /* validate pointer first - exit if failure */
400 if (RNA_path_resolve_property(&id_ptr, rna_path, &ptr, &prop) == false) {
402 reports,
403 RPT_ERROR,
404 "Could not add driver, as RNA path is invalid for the given ID (ID = %s, path = %s)",
405 id->name,
406 rna_path);
407 return 0;
408 }
409
410 /* key entire array convenience method */
411 if (array_index == -1) {
412 array_index_max = RNA_property_array_length(&ptr, prop);
413 array_index = 0;
414 }
415 else {
416 array_index_max = array_index;
417 }
418
419 /* maximum index should be greater than the start index */
420 if (array_index == array_index_max) {
421 array_index_max += 1;
422 }
423
424 /* will only loop once unless the array index was -1 */
425 for (; array_index < array_index_max; array_index++) {
426 /* create F-Curve with Driver */
428
429 if (fcu && fcu->driver) {
430 ChannelDriver *driver = fcu->driver;
431
432 /* set the type of the driver */
433 driver->type = type;
434
435 /* Creating drivers for buttons will create the driver(s) with type
436 * "scripted expression" so that their values won't be lost immediately,
437 * so here we copy those values over to the driver's expression
438 *
439 * If the "default dvar" option (for easier UI setup of drivers) is provided,
440 * include "var" in the expressions too, so that the user doesn't have to edit
441 * it to get something to happen. It should be fine to just add it to the default
442 * value, so that we get both in the expression, even if it's a bit more confusing
443 * that way...
444 */
445 if (type == DRIVER_TYPE_PYTHON) {
446 PropertyType proptype = RNA_property_type(prop);
447 int array = RNA_property_array_length(&ptr, prop);
448 const char *dvar_prefix = (flag & CREATEDRIVER_WITH_DEFAULT_DVAR) ? "var + " : "";
449 char *expression = driver->expression;
450 const size_t expression_maxncpy = sizeof(driver->expression);
451 int val;
452 float fval;
453
454 if (proptype == PROP_BOOLEAN) {
455 if (!array) {
456 val = RNA_property_boolean_get(&ptr, prop);
457 }
458 else {
459 val = RNA_property_boolean_get_index(&ptr, prop, array_index);
460 }
461
463 expression, expression_maxncpy, "%s%s", dvar_prefix, (val) ? "True" : "False");
464 }
465 else if (proptype == PROP_INT) {
466 if (!array) {
467 val = RNA_property_int_get(&ptr, prop);
468 }
469 else {
470 val = RNA_property_int_get_index(&ptr, prop, array_index);
471 }
472
473 BLI_snprintf_utf8(expression, expression_maxncpy, "%s%d", dvar_prefix, val);
474 }
475 else if (proptype == PROP_FLOAT) {
476 if (!array) {
477 fval = RNA_property_float_get(&ptr, prop);
478 }
479 else {
480 fval = RNA_property_float_get_index(&ptr, prop, array_index);
481 }
482
483 BLI_snprintf_utf8(expression, expression_maxncpy, "%s%.3f", dvar_prefix, fval);
484 BLI_str_rstrip_float_zero(expression, '\0');
485 }
486 else if (flag & CREATEDRIVER_WITH_DEFAULT_DVAR) {
487 BLI_strncpy_utf8(expression, "var", expression_maxncpy);
488 }
489 }
490
491 /* for easier setup of drivers from UI, a driver variable should be
492 * added if flag is set (UI calls only)
493 */
495 /* assume that users will mostly want this to be of type "Transform Channel" too,
496 * since this allows the easiest setting up of common rig components
497 */
498 DriverVar *dvar = driver_add_new_variable(driver);
500 }
501 }
502
503 /* set the done status */
504 done_tot += (fcu != nullptr);
505 }
506
507 /* done */
508 return done_tot;
509}
510
511bool ANIM_remove_driver(ID *id, const char rna_path[], int array_index)
512{
514 if (!adt) {
515 return false;
516 }
517
518 if (array_index >= 0) {
519 /* Simple case: Find the matching driver and remove it. */
521 if (!fcu) {
522 return false;
523 }
524
525 BLI_remlink(&adt->drivers, fcu);
526 BKE_fcurve_free(fcu);
527 return true;
528 }
529
530 /* Step through all drivers, removing all of those with the same RNA path. */
531 bool any_driver_removed = false;
532 FCurve *fcu_iter = adt->drivers.first();
533 FCurve *fcu;
534 while ((fcu = BKE_fcurve_iter_step(fcu_iter, rna_path)) != nullptr) {
535 /* Store the next fcurve for looping. */
536 fcu_iter = fcu->next;
537
538 BLI_remlink(&adt->drivers, fcu);
539 BKE_fcurve_free(fcu);
540
541 any_driver_removed = true;
542 }
543 return any_driver_removed;
544}
545
546/* ************************************************** */
547/* Driver Management API - Copy/Paste Drivers */
548
549/* Copy/Paste Buffer for Driver Data... */
551
553{
554 /* free the buffer F-Curve if it exists, as if it were just another F-Curve */
557 }
559}
560
562{
563 return (channeldriver_copypaste_buf != nullptr);
564}
565
566/* ------------------- */
567
569 ReportList *reports, ID *id, const char rna_path[], int array_index, short /*flag*/)
570{
571 PointerRNA ptr;
572 PropertyRNA *prop;
573 FCurve *fcu;
574
575 /* validate pointer first - exit if failure */
577 if (RNA_path_resolve_property(&id_ptr, rna_path, &ptr, &prop) == false) {
578 BKE_reportf(reports,
579 RPT_ERROR,
580 "Could not find driver to copy, as RNA path is invalid for the given ID (ID = %s, "
581 "path = %s)",
582 id->name,
583 rna_path);
584 return false;
585 }
586
587 /* try to get F-Curve with Driver */
589
590 /* clear copy/paste buffer first (for consistency with other copy/paste buffers) */
592
593 /* copy this to the copy/paste buf if it exists */
594 if (fcu && fcu->driver) {
596 channeldriver_copypaste_buf->rna_path_set("");
597 return true;
598 }
599
600 /* done */
601 return false;
602}
603
605 ReportList *reports, ID *id, const char rna_path[], int array_index, short /*flag*/)
606{
607 PointerRNA ptr;
608 PropertyRNA *prop;
609 FCurve *fcu;
610
611 /* validate pointer first - exit if failure */
613 if (RNA_path_resolve_property(&id_ptr, rna_path, &ptr, &prop) == false) {
615 reports,
616 RPT_ERROR,
617 "Could not paste driver, as RNA path is invalid for the given ID (ID = %s, path = %s)",
618 id->name,
619 rna_path);
620 return false;
621 }
622
623 /* if the buffer is empty, cannot paste... */
624 if (channeldriver_copypaste_buf == nullptr) {
625 BKE_report(reports, RPT_ERROR, "Paste driver: no driver to paste");
626 return false;
627 }
628
629 /* create Driver F-Curve, but without data which will be copied across... */
630 fcu = verify_driver_fcurve(id, rna_path, array_index, DRIVER_FCURVE_EMPTY);
631
632 if (fcu) {
633 /* copy across the curve data from the buffer curve
634 * NOTE: this step needs care to not miss new settings
635 */
636 /* keyframes/samples */
637 fcu->bezt = MEM_dupalloc(channeldriver_copypaste_buf->bezt);
638 fcu->fpt = MEM_dupalloc(channeldriver_copypaste_buf->fpt);
640
641 /* modifiers */
643
644 /* extrapolation mode */
646
647 /* the 'juicy' stuff - the driver */
649 }
650
651 /* done */
652 return (fcu != nullptr);
653}
654
655/* ************************************************** */
656/* Driver Management API - Copy/Paste Driver Variables */
657
658/* Copy/Paste Buffer for Driver Variables... */
659static ListBaseT<DriverVar> driver_vars_copybuf = {nullptr, nullptr};
660
662{
663 /* Free the driver variables kept in the buffer */
664 if (driver_vars_copybuf.first_) {
665 DriverVar *dvar, *dvarn;
666
667 /* Free variables (and any data they use) */
668 for (dvar = driver_vars_copybuf.first(); dvar; dvar = dvarn) {
669 dvarn = dvar->next;
671 }
672 }
673
674 driver_vars_copybuf.clear_no_delete();
675}
676
678{
679 return (driver_vars_copybuf.is_empty() == false);
680}
681
682/* -------------------------------------------------- */
683
685{
686 /* sanity checks */
687 if (ELEM(nullptr, fcu, fcu->driver)) {
688 BKE_report(reports, RPT_ERROR, "No driver to copy variables from");
689 return false;
690 }
691
692 if (fcu->driver->variables.is_empty()) {
693 BKE_report(reports, RPT_ERROR, "Driver has no variables to copy");
694 return false;
695 }
696
697 /* clear buffer */
699
700 /* copy over the variables */
702
703 return (driver_vars_copybuf.is_empty() == false);
704}
705
706bool ANIM_driver_vars_paste(ReportList *reports, FCurve *fcu, bool replace)
707{
708 ChannelDriver *driver = (fcu) ? fcu->driver : nullptr;
709 ListBaseT<DriverVar> tmp_list = {nullptr, nullptr};
710
711 /* sanity checks */
712 if (driver_vars_copybuf.is_empty()) {
713 BKE_report(reports, RPT_ERROR, "No driver variables in the internal clipboard to paste");
714 return false;
715 }
716
717 if (ELEM(nullptr, fcu, fcu->driver)) {
718 BKE_report(reports, RPT_ERROR, "Cannot paste driver variables without a driver");
719 return false;
720 }
721
722 /* 1) Make a new copy of the variables in the buffer - these will get pasted later... */
724
725 /* 2) Prepare destination array */
726 if (replace) {
727 DriverVar *dvar, *dvarn;
728
729 /* Free all existing vars first - We aren't retaining anything */
730 for (dvar = driver->variables.first(); dvar; dvar = dvarn) {
731 dvarn = dvar->next;
732 driver_free_variable_ex(driver, dvar);
733 }
734
735 driver->variables.clear_no_delete();
736 }
737
738 /* 3) Add new vars */
739 if (driver->variables.last()) {
740 DriverVar *last = driver->variables.last();
741 DriverVar *first = tmp_list.first();
742
743 last->next = first;
744 first->prev = last;
745
746 driver->variables.last_ = tmp_list.last();
747 }
748 else {
749 driver->variables.first_ = tmp_list.first_;
750 driver->variables.last_ = tmp_list.last();
751 }
752
753 /* since driver variables are cached, the expression needs re-compiling too */
754 BKE_driver_invalidate_expression(driver, false, true);
755
756 return true;
757}
758
759/* -------------------------------------------------- */
760
761void ANIM_copy_as_driver(ID *target_id, const char *target_path, const char *var_name)
762{
763 /* Clear copy/paste buffer first (for consistency with other copy/paste buffers). */
766
767 /* Create a dummy driver F-Curve. */
769 ChannelDriver *driver = fcu->driver;
770
771 /* Create a variable. */
772 DriverVar *var = driver_add_new_variable(driver);
773 DriverTarget *target = &var->targets[0];
774
775 target->idtype = GS(target_id->name);
776 target->id = target_id;
777 target->rna_path = BLI_strdup(target_path);
778
779 /* Set the variable name. */
780 if (var_name) {
781 STRNCPY_UTF8(var->name, var_name);
782
783 /* Sanitize the name. */
784 for (int i = 0; var->name[i]; i++) {
785 if (!(i > 0 ? isalnum(var->name[i]) : isalpha(var->name[i]))) {
786 var->name[i] = '_';
787 }
788 }
789 }
790
791 STRNCPY_UTF8(driver->expression, var->name);
792
793 /* Store the driver into the copy/paste buffers. */
795
797}
798
799/* ************************************************** */
800/* UI-Button Interface */
801
802/* Add Driver - Enum Defines ------------------------- */
803
805 /* XXX: These names need reviewing. */
807 "SINGLE_MANY",
808 0,
809 "All from Target",
810 "Drive all components of this property using the target picked"},
812 "DIRECT",
813 0,
814 "Single from Target",
815 "Drive this component of this property using the target picked"},
816
818 "MATCH",
819 ICON_COLOR,
820 "Match Indices",
821 "Create drivers for each pair of corresponding elements"},
822
824 "NONE_ALL",
825 ICON_HAND,
826 "Manually Create Later",
827 "Create drivers for all properties without assigning any targets yet"},
829 "NONE_SINGLE",
830 0,
831 "Manually Create Later (Single)",
832 "Create driver for this property only and without assigning any targets yet"},
833 {0, nullptr, 0, nullptr, nullptr},
834};
835
836/* Filtering callback for driver mapping types enum */
838 PointerRNA * /*owner_ptr*/,
839 PropertyRNA * /*owner_prop*/,
840 bool *r_free)
841{
843 EnumPropertyItem *item = nullptr;
844
845 PointerRNA ptr = {};
846 PropertyRNA *prop = nullptr;
847 int index;
848
849 int totitem = 0;
850
851 if (!C) { /* needed for docs */
853 }
854
856
857 if (ptr && ptr.has_owner_id() && prop && RNA_property_driver_editable(&ptr, prop)) {
858 const bool is_array = RNA_property_array_check(prop);
859
860 while (input->identifier) {
861 if (ELEM(input->value, CREATEDRIVER_MAPPING_1_1, CREATEDRIVER_MAPPING_NONE) || (is_array)) {
862 RNA_enum_item_add(&item, &totitem, input);
863 }
864 input++;
865 }
866 }
867 else {
868 /* We need at least this one! */
870 }
871
872 RNA_enum_item_end(&item, &totitem);
873
874 *r_free = true;
875 return item;
876}
877
878/* Add Driver (With Menu) Button Operator ------------------------ */
879
881{
882 PointerRNA ptr = {};
883 PropertyRNA *prop = nullptr;
884 int index;
885 bool driven, special;
886
887 /* this operator can only run if there's a property button active, and it can be animated */
889
890 if (!(ptr && ptr.has_owner_id() && prop)) {
891 return false;
892 }
893 if (!RNA_property_driver_editable(&ptr, prop)) {
894 return false;
895 }
896
897 /* Don't do anything if there is an fcurve for animation without a driver. */
899 C, &ptr, prop, index, nullptr, nullptr, &driven, &special);
900 return (fcu == nullptr || fcu->driver);
901}
902
903/* Wrapper for creating a driver without knowing what the targets will be yet
904 * (i.e. "manual/add later"). */
906{
907 PointerRNA ptr = {};
908 PropertyRNA *prop = nullptr;
909 int index;
910 int success = 0;
911
913
914 if (mapping_type == CREATEDRIVER_MAPPING_NONE_ALL) {
915 index = -1;
916 }
917
918 if (ptr && ptr.has_owner_id() && prop && RNA_property_driver_editable(&ptr, prop)) {
919 short flags = CREATEDRIVER_WITH_DEFAULT_DVAR;
920
921 if (const std::optional<std::string> path = RNA_path_from_ID_to_property(&ptr, prop)) {
922 success += ANIM_add_driver(
923 op->reports, ptr.owner_id, path->c_str(), index, flags, DRIVER_TYPE_PYTHON);
924 }
925 }
926
927 if (success) {
928 /* send updates */
932
933 return OPERATOR_FINISHED;
934 }
935 return OPERATOR_CANCELLED;
936}
937
939{
940 short mapping_type = RNA_enum_get(op->ptr, "mapping_type");
942 /* Just create driver with no targets */
943 return add_driver_button_none(C, op, mapping_type);
944 }
945
946 /* Create Driver using Eyedropper */
947 wmOperatorType *ot = WM_operatortype_find("UI_OT_eyedropper_driver", true);
948
949 /* XXX: We assume that it's fine to use the same set of properties,
950 * since they're actually the same. */
952
953 return OPERATOR_FINISHED;
954}
955
956/* Show menu or create drivers */
958 wmOperator *op,
959 const wmEvent * /*event*/)
960{
961 PropertyRNA *prop;
962
963 if ((prop = RNA_struct_find_property(op->ptr, "mapping_type")) &&
964 RNA_property_is_set(op->ptr, prop))
965 {
966 /* Mapping Type is Set - Directly go into creating drivers */
967 return add_driver_button_menu_exec(C, op);
968 }
969
970 /* Show menu */
971 /* TODO: This should get filtered by the enum filter. */
972 /* important to execute in the region we're currently in. */
974}
975
977{
978 /* identifiers */
979 ot->name = "Add Driver Menu";
980 ot->idname = "ANIM_OT_driver_button_add_menu";
981 ot->description = "Add driver(s) for the property(s) represented by the highlighted button";
982
983 /* callbacks */
986 ot->poll = add_driver_button_poll;
987
988 /* flags */
989 ot->flag = OPTYPE_UNDO | OPTYPE_INTERNAL;
990
991 /* properties */
992 ot->prop = RNA_def_enum(ot->srna,
993 "mapping_type",
995 0,
996 "Mapping Type",
997 "Method used to match target and driven properties");
999}
1000
1001/* Add Driver Button Operator ------------------------ */
1002
1004 wmOperator *op,
1005 const wmEvent * /*event*/)
1006{
1007 PointerRNA ptr = {};
1008 PropertyRNA *prop = nullptr;
1009 int index;
1010
1011 ui::context_active_but_prop_get(C, &ptr, &prop, &index);
1012
1013 if (ptr && ptr.has_owner_id() && prop && RNA_property_driver_editable(&ptr, prop)) {
1014 /* 1) Create a new "empty" driver for this property */
1015 short flags = CREATEDRIVER_WITH_DEFAULT_DVAR;
1016 bool changed = false;
1017
1018 if (const std::optional<std::string> path = RNA_path_from_ID_to_property(&ptr, prop)) {
1019 changed |=
1021 op->reports, ptr.owner_id, path->c_str(), index, flags, DRIVER_TYPE_PYTHON) != 0);
1022 }
1023
1024 if (changed) {
1025 /* send updates */
1030 }
1031
1032 /* 2) Show editing panel for setting up this driver */
1033 /* TODO: Use a different one from the editing popover, so we can have the single/all toggle? */
1034 ui::popover_panel_invoke(C, "GRAPH_PT_drivers_popover", true, false, op->reports);
1035 }
1036
1037 return OPERATOR_INTERFACE;
1038}
1039
1041{
1042 /* identifiers */
1043 ot->name = "Add Driver";
1044 ot->idname = "ANIM_OT_driver_button_add";
1045 ot->description = "Add driver for the property under the cursor";
1046
1047 /* callbacks */
1048 /* NOTE: No exec, as we need all these to use the current context info */
1051
1052 /* flags */
1054}
1055
1056/* Remove Driver Button Operator ------------------------ */
1057
1059{
1060 PointerRNA ptr = {};
1061 PropertyRNA *prop = nullptr;
1062 bool changed = false;
1063 int index;
1064 const bool all = RNA_boolean_get(op->ptr, "all");
1065
1066 ui::context_active_but_prop_get(C, &ptr, &prop, &index);
1067
1068 if (all) {
1069 index = -1;
1070 }
1071
1072 if (ptr && ptr.has_owner_id() && prop) {
1073 if (const std::optional<std::string> path = RNA_path_from_ID_to_property(&ptr, prop)) {
1074 changed = ANIM_remove_driver(ptr.owner_id, path->c_str(), index);
1075 }
1076 }
1077
1078 if (changed) {
1079 /* send updates */
1084 }
1085
1086 return (changed) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
1087}
1088
1090{
1091 /* identifiers */
1092 ot->name = "Remove Driver";
1093 ot->idname = "ANIM_OT_driver_button_remove";
1094 ot->description =
1095 "Remove the driver(s) for the connected property(s) represented by the highlighted button";
1096
1097 /* callbacks */
1099 /* TODO: `op->poll` need to have some driver to be able to do this. */
1100
1101 /* flags */
1103
1104 /* properties */
1105 RNA_def_boolean(ot->srna, "all", true, "All", "Delete drivers for all elements of the array");
1106}
1107
1108/* Edit Driver Button Operator ------------------------ */
1109
1111{
1112 PointerRNA ptr = {};
1113 PropertyRNA *prop = nullptr;
1114 int index;
1115
1116 ui::context_active_but_prop_get(C, &ptr, &prop, &index);
1117
1118 if (ptr && ptr.has_owner_id() && prop) {
1119 ui::popover_panel_invoke(C, "GRAPH_PT_drivers_popover", true, false, op->reports);
1120 }
1121
1122 return OPERATOR_INTERFACE;
1123}
1124
1126{
1127 /* identifiers */
1128 ot->name = "Edit Driver";
1129 ot->idname = "ANIM_OT_driver_button_edit";
1130 ot->description =
1131 "Edit the drivers for the connected property represented by the highlighted button";
1132
1133 /* callbacks */
1135 /* TODO: `op->poll` need to have some driver to be able to do this. */
1136
1137 /* flags */
1139}
1140
1141/* Copy Driver Button Operator ------------------------ */
1142
1144{
1145 PointerRNA ptr = {};
1146 PropertyRNA *prop = nullptr;
1147 bool changed = false;
1148 int index;
1149
1150 ui::context_active_but_prop_get(C, &ptr, &prop, &index);
1151
1152 if (ptr && ptr.has_owner_id() && prop && RNA_property_driver_editable(&ptr, prop)) {
1153 if (const std::optional<std::string> path = RNA_path_from_ID_to_property(&ptr, prop)) {
1154 /* only copy the driver for the button that this was involved for */
1155 changed = ANIM_copy_driver(op->reports, ptr.owner_id, path->c_str(), index, 0);
1156
1158 }
1159 }
1160
1161 /* Since we're just copying, we don't really need to do anything else. */
1162 return (changed) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
1163}
1164
1166{
1167 /* identifiers */
1168 ot->name = "Copy Driver";
1169 ot->idname = "ANIM_OT_copy_driver_button";
1170 ot->description = "Copy the driver for the highlighted button";
1171
1172 /* callbacks */
1174 /* TODO: `op->poll` need to have some driver to be able to do this. */
1175
1176 /* flags */
1178}
1179
1180/* Paste Driver Button Operator ------------------------ */
1181
1183{
1184 PointerRNA ptr = {};
1185 PropertyRNA *prop = nullptr;
1186 bool changed = false;
1187 int index;
1188
1189 ui::context_active_but_prop_get(C, &ptr, &prop, &index);
1190
1191 if (ptr && ptr.has_owner_id() && prop && RNA_property_driver_editable(&ptr, prop)) {
1192 if (const std::optional<std::string> path = RNA_path_from_ID_to_property(&ptr, prop)) {
1193 /* only copy the driver for the button that this was involved for */
1194 changed = ANIM_paste_driver(op->reports, ptr.owner_id, path->c_str(), index, 0);
1195
1197
1199
1201
1203 }
1204 }
1205
1206 /* Since we're just copying, we don't really need to do anything else. */
1207 return (changed) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
1208}
1209
1211{
1212 /* identifiers */
1213 ot->name = "Paste Driver";
1214 ot->idname = "ANIM_OT_paste_driver_button";
1215 ot->description = "Paste the driver in the internal clipboard to the highlighted button";
1216
1217 /* callbacks */
1219 /* TODO: `op->poll` need to have some driver to be able to do this. */
1220
1221 /* flags */
1223}
1224
1225/* ************************************************** */
1226
1227} // namespace blender
Functions to modify FCurves.
#define STRNCPY_UTF8(dst, src)
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NC_ANIMATION
Definition WM_types.hh:401
#define ND_KEYFRAME_PROP
Definition WM_types.hh:538
#define ND_FCURVES_ORDER
Definition WM_types.hh:542
#define UNUSED_FUNCTION(x)
#define STRPREFIX(a, b)
#define ELEM(...)
#define STREQ(a, b)
bool all(VecOp< bool, D >) RET
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
KeyframeSettings get_keyframe_settings(bool from_userprefs)
SingleKeyingResult insert_vert_fcurve(FCurve *fcu, const float2 position, const KeyframeSettings &settings, eInsertKeyFlags flag)
Main Key-framing API call.
wmOperatorStatus popover_panel_invoke(bContext *C, const char *idname, bool keep_open, bool use_numselect, ReportList *reports)
void context_update_anim_flag(const bContext *C)
Button * context_active_but_prop_get(const bContext *C, PointerRNA *r_ptr, PropertyRNA **r_prop, int *r_index)
BMesh Iterators.
void ANIM_driver_vars_copybuf_free()
Definition drivers.cc:661
UserDef U
Definition blender.cc:58
int RNA_property_array_length(PointerRNA *ptr, PropertyRNA *prop)
void RNA_def_enum_funcs(PropertyRNA *prop, EnumPropertyItemFunc itemfunc)
size_t size_t size_t BLI_snprintf_utf8(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
@ DVAR_TYPE_TRANSFORM_CHAN
void RNA_enum_item_add(EnumPropertyItem **items, int *totitem, const EnumPropertyItem *item)
int ANIM_add_driver(ReportList *reports, ID *id, const char rna_path[], int array_index, short flag, eDriver_Types type)
Main Driver Management API calls.
Definition drivers.cc:385
int RNA_enum_get(PointerRNA *ptr, const char *name)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
FCurve * BKE_fcurve_iter_step(FCurve *fcu_iter, const char rna_path[])
void BKE_driver_invalidate_expression(struct ChannelDriver *driver, bool expr_changed, bool varname_changed)
PointerRNA RNA_id_pointer_create(ID *id)
std::optional< std::string > RNA_path_from_ID_to_property(const PointerRNA *ptr, PropertyRNA *prop)
Definition rna_path.cc:1237
bool ANIM_driver_vars_paste(ReportList *reports, FCurve *fcu, bool replace)
Definition drivers.cc:706
bool ANIM_paste_driver(ReportList *reports, ID *id, const char rna_path[], int array_index, short)
Main Driver Management API calls.
Definition drivers.cc:604
void ANIM_OT_driver_button_remove(wmOperatorType *ot)
Definition drivers.cc:1089
const char * RNA_property_identifier(const PropertyRNA *prop)
bool ANIM_driver_vars_copy(ReportList *reports, FCurve *fcu)
Definition drivers.cc:684
FCurve * verify_driver_fcurve(ID *id, const char rna_path[], const int array_index, eDriverFCurveCreationMode creation_mode)
Definition drivers.cc:53
static wmOperatorStatus add_driver_button_menu_exec(bContext *C, wmOperator *op)
Definition drivers.cc:938
float RNA_property_float_get_index(PointerRNA *ptr, PropertyRNA *prop, int index)
void copy_fmodifiers(ListBaseT< FModifier > *dst, const ListBaseT< FModifier > *src)
struct ChannelDriver * fcurve_copy_driver(const struct ChannelDriver *driver)
@ PROP_UNIT_ROTATION
Definition RNA_types.hh:226
void driver_free_variable_ex(struct ChannelDriver *driver, struct DriverVar *dvar)
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
char * BLI_sprintfN(const char *__restrict format,...) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_PRINTF_FORMAT(1
PropertyUnit RNA_property_unit(PropertyRNA *prop)
bool RNA_property_driver_editable(const PointerRNA *ptr, PropertyRNA *prop)
bool ANIM_driver_can_paste()
Definition drivers.cc:561
static wmOperatorStatus remove_driver_button_exec(bContext *C, wmOperator *op)
Definition drivers.cc:1058
int ANIM_add_driver_with_target(ReportList *reports, ID *dst_id, const char dst_path[], int dst_index, ID *src_id, const char src_path[], int src_index, short flag, eDriver_Types driver_type, short mapping_type)
Main Driver Management API calls.
Definition drivers.cc:274
AnimData * BKE_animdata_ensure_id(ID *id)
Definition anim_data.cc:107
bool ANIM_copy_driver(ReportList *reports, ID *id, const char rna_path[], int array_index, short)
Main Driver Management API calls.
Definition drivers.cc:568
bool RNA_property_boolean_get(PointerRNA *ptr, PropertyRNA *prop)
bool RNA_property_array_check(PropertyRNA *prop)
PropertyType RNA_property_type(PropertyRNA *prop)
AnimData * BKE_animdata_from_id(const ID *id)
Definition anim_data.cc:93
static wmOperatorStatus add_driver_button_menu_invoke(bContext *C, wmOperator *op, const wmEvent *)
Definition drivers.cc:957
static wmOperatorStatus add_driver_button_invoke(bContext *C, wmOperator *op, const wmEvent *)
Definition drivers.cc:1003
@ PROP_BOOLEAN
Definition RNA_types.hh:210
void BKE_fcurve_handles_recalc(FCurve &fcu)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
wmOperatorStatus WM_menu_invoke_ex(bContext *C, wmOperator *op, wm::OpCallContext opcontext)
bool RNA_property_boolean_get_index(PointerRNA *ptr, PropertyRNA *prop, int index)
int BLI_str_rstrip_float_zero(char *str, char pad) ATTR_NONNULL(1)
FCurve * BKE_fcurve_copy(const FCurve *fcu)
static ListBaseT< DriverVar > driver_vars_copybuf
Definition drivers.cc:659
static wmOperatorStatus copy_driver_button_exec(bContext *C, wmOperator *op)
Definition drivers.cc:1143
bool ANIM_remove_driver(ID *id, const char rna_path[], int array_index)
Main Driver Management API calls.
Definition drivers.cc:511
static wmOperatorStatus paste_driver_button_exec(bContext *C, wmOperator *op)
Definition drivers.cc:1182
static FCurve * channeldriver_copypaste_buf
Definition drivers.cc:550
void RNA_enum_items_add_value(EnumPropertyItem **items, int *totitem, const EnumPropertyItem *item, int value)
void RNA_enum_item_end(EnumPropertyItem **items, int *totitem)
static int add_driver_with_target(ReportList *, ID *dst_id, const char dst_path[], int dst_index, ID *src_id, const char src_path[], int src_index, PointerRNA *dst_ptr, PropertyRNA *dst_prop, PointerRNA *src_ptr, PropertyRNA *src_prop, eDriver_Types driver_type)
Definition drivers.cc:132
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
static const EnumPropertyItem * driver_mapping_type_itemf(bContext *C, PointerRNA *, PropertyRNA *, bool *r_free)
Definition drivers.cc:837
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void ANIM_OT_copy_driver_button(wmOperatorType *ot)
Definition drivers.cc:1165
@ DTAR_TRANSCHAN_ROTY
@ DTAR_TRANSCHAN_LOCY
@ DTAR_TRANSCHAN_LOCX
@ DTAR_TRANSCHAN_SCALEX
@ DTAR_TRANSCHAN_ROTX
@ DTAR_TRANSCHAN_LOCZ
@ DTAR_TRANSCHAN_ROTZ
@ DTAR_TRANSCHAN_SCALEY
@ DTAR_TRANSCHAN_SCALEZ
float RNA_property_float_get(PointerRNA *ptr, PropertyRNA *prop)
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)
void BKE_fcurve_free(FCurve *fcu)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
Main * CTX_data_main(const bContext *C)
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
void ANIM_drivers_copybuf_free()
Definition drivers.cc:552
static wmOperatorStatus edit_driver_button_exec(bContext *C, wmOperator *op)
Definition drivers.cc:1110
static wmOperatorStatus add_driver_button_none(bContext *C, wmOperator *op, short mapping_type)
Definition drivers.cc:905
@ ID_RECALC_ANIMATION
Definition DNA_ID.h:1077
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
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
FCurve * alloc_driver_fcurve(const char rna_path[], const int array_index, eDriverFCurveCreationMode creation_mode)
Definition drivers.cc:94
void DEG_relations_tag_update(Main *bmain)
@ CREATEDRIVER_WITH_DEFAULT_DVAR
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ OPTYPE_INTERNAL
Definition WM_types.hh:210
static void UNUSED_FUNCTION ANIM_OT_driver_button_add_menu(wmOperatorType *ot)
Definition drivers.cc:976
std::string RNA_string_get(PointerRNA *ptr, const char *name)
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)
void ANIM_copy_as_driver(ID *target_id, const char *target_path, const char *var_name)
Definition drivers.cc:761
FCurve * BKE_fcurve_find(ListBaseT< FCurve > *list, const char rna_path[], int array_index)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
const EnumPropertyItem prop_driver_create_mapping_types[]
Definition drivers.cc:804
void driver_change_variable_type(struct DriverVar *dvar, eDriverVar_Types type)
int RNA_property_int_get(PointerRNA *ptr, PropertyRNA *prop)
void driver_variables_copy(ListBaseT< DriverVar > *dst_vars, const ListBaseT< DriverVar > *src_vars)
bool ANIM_driver_vars_can_paste()
Definition drivers.cc:677
void ANIM_OT_driver_button_edit(wmOperatorType *ot)
Definition drivers.cc:1125
bool RNA_path_resolve_property(const PointerRNA *ptr, const char *path, PointerRNA *r_ptr, PropertyRNA **r_prop)
Definition rna_path.cc:578
void ANIM_OT_driver_button_add(wmOperatorType *ot)
Definition drivers.cc:1040
@ FCURVE_EXTRAPOLATE_LINEAR
void ANIM_OT_paste_driver_button(wmOperatorType *ot)
Definition drivers.cc:1210
eDriverFCurveCreationMode
@ DRIVER_FCURVE_LOOKUP_ONLY
@ DRIVER_FCURVE_EMPTY
@ DRIVER_FCURVE_KEYFRAMES
static bool add_driver_button_poll(bContext *C)
Definition drivers.cc:880
int RNA_property_int_get_index(PointerRNA *ptr, PropertyRNA *prop, int index)
bool RNA_property_is_set(PointerRNA *ptr, PropertyRNA *prop)
@ CREATEDRIVER_MAPPING_NONE_ALL
@ CREATEDRIVER_MAPPING_NONE
@ CREATEDRIVER_MAPPING_N_N
@ CREATEDRIVER_MAPPING_1_1
@ CREATEDRIVER_MAPPING_1_N
FCurve * BKE_fcurve_create()
void driver_free_variable(ListBaseT< DriverVar > *variables, struct DriverVar *dvar)
struct DriverVar * driver_add_new_variable(struct ChannelDriver *driver)
ListBaseT< FCurve > drivers
ListBaseT< DriverVar > variables
eDriverTarget_TransformChannels transChan
struct DriverVar * next
struct DriverVar * prev
DriverTarget targets[8]
eFCurve_Flags flag
eFCurve_Extend extend
eFCurve_Smoothing auto_smoothing
struct FCurve * next
ListBaseT< FModifier > modifiers
ChannelDriver * driver
unsigned int totvert
char name[258]
Definition DNA_ID.h:433
StructRNA * type
Definition RNA_types.hh:63
bool has_owner_id() const
Definition RNA_types.hh:186
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
struct ReportList * reports