[Bf-blender-cvs] [b22afef72c5] master: Cleanup: Replace 'add mode' short for driver FCurve creation with an enum

Sybren A. Stüvel noreply at git.blender.org
Fri Sep 27 17:02:57 CEST 2019


Commit: b22afef72c5cab001b7458defc3d042742803b9d
Author: Sybren A. Stüvel
Date:   Fri Sep 27 16:33:48 2019 +0200
Branches: master
https://developer.blender.org/rBb22afef72c5cab001b7458defc3d042742803b9d

Cleanup: Replace 'add mode' short for driver FCurve creation with an enum

The 'add mode' was a `short` between -1 and 2 inclusive, and magic numbers
sprinkled throughout the code. It's now an enum with descriptive names.

No functional changes.

===================================================================

M	source/blender/editors/animation/drivers.c
M	source/blender/editors/include/ED_keyframing.h
M	source/blender/editors/interface/interface_anim.c
M	source/blender/makesrna/intern/rna_animation.c

===================================================================

diff --git a/source/blender/editors/animation/drivers.c b/source/blender/editors/animation/drivers.c
index 7b9e6a10f44..e75dd92e90b 100644
--- a/source/blender/editors/animation/drivers.c
+++ b/source/blender/editors/animation/drivers.c
@@ -62,13 +62,11 @@
 
 /* Get (or add relevant data to be able to do so) F-Curve from the driver stack,
  * for the given Animation Data block. This assumes that all the destinations are valid.
- *
- * - add: 0 - don't add anything if not found,
- *        1 - add new Driver FCurve (with keyframes for visual tweaking),
- *        2 - add new Driver FCurve (with generator, for script backwards compatibility)
- *        -1 - add new Driver FCurve without driver stuff (for pasting)
  */
-FCurve *verify_driver_fcurve(ID *id, const char rna_path[], const int array_index, short add)
+FCurve *verify_driver_fcurve(ID *id,
+                             const char rna_path[],
+                             const int array_index,
+                             eDriverFCurveCreationMode creation_mode)
 {
   AnimData *adt;
   FCurve *fcu;
@@ -80,7 +78,7 @@ FCurve *verify_driver_fcurve(ID *id, const char rna_path[], const int array_inde
 
   /* init animdata if none available yet */
   adt = BKE_animdata_from_id(id);
-  if ((adt == NULL) && (add)) {
+  if (adt == NULL && creation_mode != DRIVER_FCURVE_LOOKUP_ONLY) {
     adt = BKE_animdata_add_id(id);
   }
   if (adt == NULL) {
@@ -94,9 +92,9 @@ FCurve *verify_driver_fcurve(ID *id, const char rna_path[], const int array_inde
    */
   fcu = list_find_fcurve(&adt->drivers, rna_path, array_index);
 
-  if ((fcu == NULL) && (add)) {
+  if (fcu == NULL && creation_mode != DRIVER_FCURVE_LOOKUP_ONLY) {
     /* use default settings to make a F-Curve */
-    fcu = alloc_driver_fcurve(rna_path, array_index, add);
+    fcu = alloc_driver_fcurve(rna_path, array_index, creation_mode);
 
     /* just add F-Curve to end of driver list */
     BLI_addtail(&adt->drivers, fcu);
@@ -106,7 +104,9 @@ FCurve *verify_driver_fcurve(ID *id, const char rna_path[], const int array_inde
   return fcu;
 }
 
-struct FCurve *alloc_driver_fcurve(const char rna_path[], const int array_index, short add)
+struct FCurve *alloc_driver_fcurve(const char rna_path[],
+                                   const int array_index,
+                                   eDriverFCurveCreationMode creation_mode)
 {
   FCurve *fcu = MEM_callocN(sizeof(FCurve), "FCurve");
 
@@ -119,9 +119,7 @@ struct FCurve *alloc_driver_fcurve(const char rna_path[], const int array_index,
   }
   fcu->array_index = array_index;
 
-  /* If add is negative, don't init this data yet,
-   * since it will be filled in by the pasted driver. */
-  if (add > 0) {
+  if (!ELEM(creation_mode, DRIVER_FCURVE_LOOKUP_ONLY, DRIVER_FCURVE_EMPTY)) {
     BezTriple *bezt;
     size_t i;
 
@@ -129,8 +127,7 @@ struct FCurve *alloc_driver_fcurve(const char rna_path[], const int array_index,
     fcu->driver = MEM_callocN(sizeof(ChannelDriver), "ChannelDriver");
 
     /* F-Modifier or Keyframes? */
-    // FIXME: replace these magic numbers with defines
-    if (add == 2) {
+    if (creation_mode == DRIVER_FCURVE_GENERATOR) {
       /* Python API Backwards compatibility hack:
        * Create FModifier so that old scripts won't break
        * for now before 2.7 series -- (September 4, 2013)
@@ -177,7 +174,8 @@ static int add_driver_with_target(ReportList *UNUSED(reports),
                                   int driver_type)
 {
   FCurve *fcu;
-  short add_mode = (flag & CREATEDRIVER_WITH_FMODIFIER) ? 2 : 1;
+  short add_mode = (flag & CREATEDRIVER_WITH_FMODIFIER) ? DRIVER_FCURVE_GENERATOR :
+                                                          DRIVER_FCURVE_KEYFRAMES;
   const char *prop_name = RNA_property_identifier(src_prop);
 
   /* Create F-Curve with Driver */
@@ -593,7 +591,7 @@ bool ANIM_remove_driver(ReportList *UNUSED(reports),
        * Note: here is one of the places where we don't want new F-Curve + Driver added!
        *      so 'add' var must be 0
        */
-      fcu = verify_driver_fcurve(id, rna_path, array_index, 0);
+      fcu = verify_driver_fcurve(id, rna_path, array_index, DRIVER_FCURVE_LOOKUP_ONLY);
       if (fcu) {
         BLI_remlink(&adt->drivers, fcu);
         free_fcurve(fcu);
@@ -653,7 +651,7 @@ bool ANIM_copy_driver(
   }
 
   /* try to get F-Curve with Driver */
-  fcu = verify_driver_fcurve(id, rna_path, array_index, 0);
+  fcu = verify_driver_fcurve(id, rna_path, array_index, DRIVER_FCURVE_LOOKUP_ONLY);
 
   /* clear copy/paste buffer first (for consistency with other copy/paste buffers) */
   ANIM_drivers_copybuf_free();
@@ -711,7 +709,7 @@ bool ANIM_paste_driver(
   }
 
   /* create Driver F-Curve, but without data which will be copied across... */
-  fcu = verify_driver_fcurve(id, rna_path, array_index, -1);
+  fcu = verify_driver_fcurve(id, rna_path, array_index, DRIVER_FCURVE_EMPTY);
 
   if (fcu) {
     /* copy across the curve data from the buffer curve
@@ -855,7 +853,7 @@ void ANIM_copy_as_driver(struct ID *target_id, const char *target_path, const ch
   ANIM_driver_vars_copybuf_free();
 
   /* Create a dummy driver F-Curve. */
-  FCurve *fcu = alloc_driver_fcurve(NULL, 0, 1);
+  FCurve *fcu = alloc_driver_fcurve(NULL, 0, DRIVER_FCURVE_KEYFRAMES);
   ChannelDriver *driver = fcu->driver;
 
   /* Create a variable. */
diff --git a/source/blender/editors/include/ED_keyframing.h b/source/blender/editors/include/ED_keyframing.h
index 42e5add2ef0..16b3c9c240a 100644
--- a/source/blender/editors/include/ED_keyframing.h
+++ b/source/blender/editors/include/ED_keyframing.h
@@ -310,15 +310,24 @@ extern EnumPropertyItem prop_driver_create_mapping_types[];
 
 /* -------- */
 
+typedef enum eDriverFCurveCreationMode {
+  DRIVER_FCURVE_LOOKUP_ONLY = 0, /* Don't add anything if not found. */
+  DRIVER_FCURVE_KEYFRAMES = 1,   /* Add with keyframes, for visual tweaking. */
+  DRIVER_FCURVE_GENERATOR = 2,   /* Add with generator, for script backwards compatibility. */
+  DRIVER_FCURVE_EMPTY = 3        /* Add without data, for pasting. */
+} eDriverFCurveCreationMode;
+
 /* Low-level call to add a new driver F-Curve. This shouldn't be used directly for most tools,
  * although there are special cases where this approach is preferable.
  */
 struct FCurve *verify_driver_fcurve(struct ID *id,
                                     const char rna_path[],
                                     const int array_index,
-                                    short add);
+                                    eDriverFCurveCreationMode creation_mode);
 
-struct FCurve *alloc_driver_fcurve(const char rna_path[], const int array_index, short add);
+struct FCurve *alloc_driver_fcurve(const char rna_path[],
+                                   const int array_index,
+                                   eDriverFCurveCreationMode creation_mode);
 
 /* -------- */
 
diff --git a/source/blender/editors/interface/interface_anim.c b/source/blender/editors/interface/interface_anim.c
index 168c6051327..c8baa1a7c7b 100644
--- a/source/blender/editors/interface/interface_anim.c
+++ b/source/blender/editors/interface/interface_anim.c
@@ -243,7 +243,7 @@ bool ui_but_anim_expression_create(uiBut *but, const char *str)
   }
 
   /* create driver */
-  fcu = verify_driver_fcurve(id, path, but->rnaindex, 1);
+  fcu = verify_driver_fcurve(id, path, but->rnaindex, DRIVER_FCURVE_KEYFRAMES);
   if (fcu) {
     ChannelDriver *driver = fcu->driver;
 
diff --git a/source/blender/makesrna/intern/rna_animation.c b/source/blender/makesrna/intern/rna_animation.c
index 6157ec41f19..e1a24326474 100644
--- a/source/blender/makesrna/intern/rna_animation.c
+++ b/source/blender/makesrna/intern/rna_animation.c
@@ -668,8 +668,7 @@ static FCurve *rna_Driver_new(
     return NULL;
   }
 
-  short add_mode = 1;
-  FCurve *fcu = verify_driver_fcurve(id, rna_path, array_index, add_mode);
+  FCurve *fcu = verify_driver_fcurve(id, rna_path, array_index, DRIVER_FCURVE_KEYFRAMES);
   BLI_assert(fcu != NULL);
 
   DEG_relations_tag_update(bmain);



More information about the Bf-blender-cvs mailing list