Commit: 89e5c756665c1e7e622ddbd5deece0dda4ce7724
Author: Sybren A. Stüvel
Date:   Tue Jul 21 16:01:26 2015 +0200
Branches: master
https://developer.blender.org/rB89e5c756665c1e7e622ddbd5deece0dda4ce7724

Added Action.fcurves.find(data_path, array_index=0)

Finding a specific F-Curve is often needed in Python, and usually
consists of a construct like:

```
 [fcurve
  for fcurve in ob.animation_data.action.fcurves
  if fcurve.data_path == "location"][1]
```

This can now be written as
`ob.animation_data.action.fcurves.find('location', 1)`

This new function `Action.fcurves.find()` is still O(N) in the number
of FCurves in the Action, but at least it allows us to remove
boiler-plate code. It is also faster than the Python equivalent, as
only the found F-Curve is converted to Python.

Reviewers: campbellbarton, aligorith

Reviewed By: aligorith

Differential Revision: https://developer.blender.org/D1427

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

M       source/blender/makesrna/intern/rna_action.c

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

diff --git a/source/blender/makesrna/intern/rna_action.c 
b/source/blender/makesrna/intern/rna_action.c
index d121e1f..424e200 100644
--- a/source/blender/makesrna/intern/rna_action.c
+++ b/source/blender/makesrna/intern/rna_action.c
@@ -119,6 +119,17 @@ static FCurve *rna_Action_fcurve_new(bAction *act, 
ReportList *reports, const ch
        return verify_fcurve(act, group, NULL, data_path, index, 1);
 }
 
+static FCurve *rna_Action_fcurve_find(bAction *act, ReportList *reports, const 
char *data_path, int index)
+{
+       if (data_path[0] == '\0') {
+               BKE_report(reports, RPT_ERROR, "F-Curve data path empty, 
invalid argument");
+               return NULL;
+       }
+
+       /* Returns NULL if not found. */
+       return list_find_fcurve(&act->curves, data_path, index);
+}
+
 static void rna_Action_fcurve_remove(bAction *act, ReportList *reports, 
PointerRNA *fcu_ptr)
 {
        FCurve *fcu = fcu_ptr->data;
@@ -576,6 +587,7 @@ static void rna_def_action_fcurves(BlenderRNA *brna, 
PropertyRNA *cprop)
        RNA_def_struct_sdna(srna, "bAction");
        RNA_def_struct_ui_text(srna, "Action F-Curves", "Collection of action 
F-Curves");
 
+       /* Action.fcurves.new(...) */
        func = RNA_def_function(srna, "new", "rna_Action_fcurve_new");
        RNA_def_function_ui_description(func, "Add an F-Curve to the action");
        RNA_def_function_flag(func, FUNC_USE_REPORTS);
@@ -587,7 +599,19 @@ static void rna_def_action_fcurves(BlenderRNA *brna, 
PropertyRNA *cprop)
        parm = RNA_def_pointer(func, "fcurve", "FCurve", "", "Newly created 
F-Curve");
        RNA_def_function_return(func, parm);
 
+       /* Action.fcurves.find(...) */
+       func = RNA_def_function(srna, "find", "rna_Action_fcurve_find");
+       RNA_def_function_ui_description(func, "Find an F-Curve. Note that this 
function performs a linear scan "
+                                       "of all F-Curves in the action.");
+       RNA_def_function_flag(func, FUNC_USE_REPORTS);
+       parm = RNA_def_string(func, "data_path", NULL, 0, "Data Path", "F-Curve 
data path");
+       RNA_def_property_flag(parm, PROP_REQUIRED);
+       RNA_def_int(func, "index", 0, 0, INT_MAX, "Index", "Array index", 0, 
INT_MAX);
+
+       parm = RNA_def_pointer(func, "fcurve", "FCurve", "", "The found 
F-Curve, or None if it doesn't exist");
+       RNA_def_function_return(func, parm);
 
+       /* Action.fcurves.remove(...) */
        func = RNA_def_function(srna, "remove", "rna_Action_fcurve_remove");
        RNA_def_function_ui_description(func, "Remove action group");
        RNA_def_function_flag(func, FUNC_USE_REPORTS);

_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to