Commit: 95f02d7806004e3d908f0793164e6a23e6495b4b
Author: Jacques Lucke
Date:   Thu Feb 21 16:56:00 2019 +0100
Branches: functions
https://developer.blender.org/rB95f02d7806004e3d908f0793164e6a23e6495b4b

new Driver Variable type that evaluates a function

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

M       source/blender/blenkernel/BKE_fcurve.h
M       source/blender/blenkernel/CMakeLists.txt
M       source/blender/blenkernel/intern/fcurve.c
M       source/blender/depsgraph/CMakeLists.txt
M       source/blender/depsgraph/intern/builder/deg_builder_relations.cc
M       source/blender/editors/space_graph/graph_buttons.c
M       source/blender/makesdna/DNA_anim_types.h
M       source/blender/makesrna/intern/rna_fcurve.c

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

diff --git a/source/blender/blenkernel/BKE_fcurve.h 
b/source/blender/blenkernel/BKE_fcurve.h
index d9435534244..e75f3ad2977 100644
--- a/source/blender/blenkernel/BKE_fcurve.h
+++ b/source/blender/blenkernel/BKE_fcurve.h
@@ -225,6 +225,8 @@ struct FCurve *iter_step_fcurve(struct FCurve *fcu_iter, 
const char rna_path[]);
 /* high level function to get an fcurve from C without having the rna */
 struct FCurve *id_data_find_fcurve(ID *id, void *data, struct StructRNA *type, 
const char *prop_name, int index, bool *r_driven);
 
+void *get_driver_variable_function(struct DriverVar *dvar);
+
 /* Get list of LinkData's containing pointers to the F-Curves which control 
the types of data indicated
  * e.g.  numMatches = list_find_data_fcurves(matches, &act->curves, 
"pose.bones[", "MyFancyBone");
  */
diff --git a/source/blender/blenkernel/CMakeLists.txt 
b/source/blender/blenkernel/CMakeLists.txt
index b30acbcae24..87758108c03 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -26,6 +26,7 @@ set(INC
        ../blentranslation
        ../depsgraph
        ../draw
+       ../functions
        ../gpu
        ../ikplugin
        ../imbuf
diff --git a/source/blender/blenkernel/intern/fcurve.c 
b/source/blender/blenkernel/intern/fcurve.c
index 78282ddb2b5..b598697ad1b 100644
--- a/source/blender/blenkernel/intern/fcurve.c
+++ b/source/blender/blenkernel/intern/fcurve.c
@@ -57,6 +57,8 @@
 
 #include "RNA_access.h"
 
+#include "FN_functions.h"
+
 #include "atomic_ops.h"
 
 #include "CLG_log.h"
@@ -1611,6 +1613,38 @@ static float dvar_eval_transChan(ChannelDriver *driver, 
DriverVar *dvar)
        }
 }
 
+/* evaluate 'function' driver variable */
+static float dvar_eval_function(ChannelDriver *UNUSED(driver), DriverVar *dvar)
+{
+       FnFunction fn = (FnFunction)get_driver_variable_function(dvar);
+       if (fn == NULL) {
+               return 0.0f;
+       }
+
+       FnTuple fn_in = FN_tuple_for_input(fn);
+       FnTuple fn_out = FN_tuple_for_output(fn);
+       FnCallable callable = FN_function_get_callable(fn);
+
+       FN_function_call(callable, fn_in, fn_out);
+       float result = FN_tuple_get_float(fn_out, 0);
+
+       FN_tuple_free(fn_in);
+       FN_tuple_free(fn_out);
+
+       return result;
+}
+
+struct bNodeTree;
+void *get_driver_variable_function(DriverVar *dvar)
+{
+       FnType float_ty = FN_type_borrow_float();
+       FnType inputs[] = { NULL };
+       FnType outputs[] = { float_ty, NULL };
+
+       struct bNodeTree *tree = (struct bNodeTree *)dvar->targets[0].id;
+       return FN_function_get_with_signature(tree, inputs, outputs);
+}
+
 /* ......... */
 
 /* Table of Driver Varaiable Type Info Data */
@@ -1642,6 +1676,13 @@ static DriverVarTypeInfo dvar_types[MAX_DVAR_TYPES] = {
                {"Object/Bone"},     /* UI names for targets */
                {DTAR_FLAG_STRUCT_REF | DTAR_FLAG_ID_OB_ONLY}   /* flags */
        END_DVAR_TYPEDEF,
+
+       BEGIN_DVAR_TYPEDEF(DVAR_TYPE_FUNCTION)
+               dvar_eval_function,  /* eval callback */
+               1,                   /* number of targets used */
+               {"Function"},        /* UI names for targets */
+               {0},                 /* flags */
+       END_DVAR_TYPEDEF,
 };
 
 /* Get driver variable typeinfo */
@@ -1734,6 +1775,10 @@ void driver_change_variable_type(DriverVar *dvar, int 
type)
                /* object ID types only, or idtype not yet initialized */
                if ((flags & DTAR_FLAG_ID_OB_ONLY) || (dtar->idtype == 0))
                        dtar->idtype = ID_OB;
+
+               if (type == DVAR_TYPE_FUNCTION) {
+                       dtar->idtype = ID_NT;
+               }
        }
        DRIVER_TARGETS_LOOPER_END;
 }
diff --git a/source/blender/depsgraph/CMakeLists.txt 
b/source/blender/depsgraph/CMakeLists.txt
index 9a619ef560d..18674a85654 100644
--- a/source/blender/depsgraph/CMakeLists.txt
+++ b/source/blender/depsgraph/CMakeLists.txt
@@ -24,6 +24,7 @@ set(INC
        ../blenlib
        ../bmesh
        ../draw
+       ../functions
        ../makesdna
        ../makesrna
        ../modifiers
diff --git a/source/blender/depsgraph/intern/builder/deg_builder_relations.cc 
b/source/blender/depsgraph/intern/builder/deg_builder_relations.cc
index dfd940a176f..c77b192c42c 100644
--- a/source/blender/depsgraph/intern/builder/deg_builder_relations.cc
+++ b/source/blender/depsgraph/intern/builder/deg_builder_relations.cc
@@ -111,6 +111,8 @@ extern "C" {
 
 #include "intern/depsgraph_type.h"
 
+#include "FN_functions.hpp"
+
 namespace DEG {
 
 /* ***************** */
@@ -1551,6 +1553,17 @@ void DepsgraphRelationBuilder::build_driver_variables(ID 
*id, FCurve *fcu)
                                continue;
                        }
                        build_id(dtar->id);
+
+                       if (dvar->type == DVAR_TYPE_FUNCTION) {
+                               auto fn = (BLI::RefCounted<FN::Function> 
*)get_driver_variable_function(dvar);
+                               if (fn != NULL) {
+                                       FN::Dependencies dependencies;
+                                       
fn->ptr()->body<FN::TupleCallBody>()->dependencies(dependencies);
+                                       dependencies.add_relations(*this, 
driver_key);
+                                       fn->decref();
+                               }
+                       }
+
                        /* Initialize relations coming to proxy_from. */
                        Object *proxy_from = NULL;
                        if ((GS(dtar->id->name) == ID_OB) &&
diff --git a/source/blender/editors/space_graph/graph_buttons.c 
b/source/blender/editors/space_graph/graph_buttons.c
index a4d7b5e9fc9..754ead46e18 100644
--- a/source/blender/editors/space_graph/graph_buttons.c
+++ b/source/blender/editors/space_graph/graph_buttons.c
@@ -731,6 +731,16 @@ static void graph_panel_driverVar__transChan(uiLayout 
*layout, ID *id, DriverVar
        uiItemR(sub, &dtar_ptr, "transform_space", 0, IFACE_("Space"), 
ICON_NONE);
 }
 
+/* settings for 'function' driver variable type */
+static void graph_panel_driverVar_function(uiLayout *layout, ID *id, DriverVar 
*dvar)
+{
+       DriverTarget *dtar = &dvar->targets[0];
+       PointerRNA dtar_ptr;
+       RNA_pointer_create(id, &RNA_DriverTarget, dtar, &dtar_ptr);
+
+       uiItemR(layout, &dtar_ptr, "id", 0, IFACE_("Function"), ICON_NONE);
+}
+
 /* ----------------------------------------------------------------- */
 
 
@@ -970,6 +980,9 @@ static void graph_draw_driver_settings_panel(uiLayout 
*layout, ID *id, FCurve *f
                        case DVAR_TYPE_TRANSFORM_CHAN:     /* transform channel 
*/
                                graph_panel_driverVar__transChan(box, id, dvar);
                                break;
+                       case DVAR_TYPE_FUNCTION:     /* function */
+                               graph_panel_driverVar_function(box, id, dvar);
+                               break;
                }
 
                /* 3) value of variable */
diff --git a/source/blender/makesdna/DNA_anim_types.h 
b/source/blender/makesdna/DNA_anim_types.h
index a05b52d0f49..391308ebb51 100644
--- a/source/blender/makesdna/DNA_anim_types.h
+++ b/source/blender/makesdna/DNA_anim_types.h
@@ -411,6 +411,8 @@ typedef enum eDriverVar_Types {
        DVAR_TYPE_LOC_DIFF,
                /* 'final' transform for object/bones */
        DVAR_TYPE_TRANSFORM_CHAN,
+               /* evaluate function */
+       DVAR_TYPE_FUNCTION,
 
        /* maximum number of variable types
         * NOTE: this must always be th last item in this list,
diff --git a/source/blender/makesrna/intern/rna_fcurve.c 
b/source/blender/makesrna/intern/rna_fcurve.c
index 6c36f133d0f..3dfcaf25773 100644
--- a/source/blender/makesrna/intern/rna_fcurve.c
+++ b/source/blender/makesrna/intern/rna_fcurve.c
@@ -1556,6 +1556,7 @@ static void rna_def_drivervar(BlenderRNA *brna)
                                           "Final transformation value of 
object or bone"},
                {DVAR_TYPE_ROT_DIFF, "ROTATION_DIFF", 
ICON_DRIVER_ROTATIONAL_DIFFERENCE, "Rotational Difference", "Use the angle 
between two bones"},
                {DVAR_TYPE_LOC_DIFF, "LOC_DIFF", ICON_DRIVER_DISTANCE, 
"Distance", "Distance between two bones or objects"},
+               {DVAR_TYPE_FUNCTION, "FUNCTION", ICON_NONE, "Function", 
"Evaluate Function"},
                {0, NULL, 0, NULL, NULL},
        };

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to