Merging trunk up to r38932.
[blender.git] / source / blender / editors / interface / interface_anim.c
index d969181..1113f90 100644 (file)
@@ -30,6 +30,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "MEM_guardedalloc.h"
 
 #include "DNA_anim_types.h"
 #include "DNA_scene_types.h"
 
 #include "BLI_listbase.h"
 #include "BLI_string.h"
+#include "BLI_utildefines.h"
 
 #include "BKE_context.h"
+#include "BKE_animsys.h"
 #include "BKE_fcurve.h"
-
+#include "BKE_global.h"
 
 #include "ED_keyframing.h"
 
 #include "UI_interface.h"
 
+#include "RNA_access.h"
+
 #include "WM_api.h"
 #include "WM_types.h"
 
@@ -108,7 +113,7 @@ int ui_but_anim_expression_set(uiBut *but, const char *str)
 
        if(fcu && driven) {
                driver= fcu->driver;
-
+               
                if(driver && driver->type == DRIVER_TYPE_PYTHON) {
                        BLI_strncpy(driver->expression, str, sizeof(driver->expression));
                        driver->flag |= DRIVER_FLAG_RECOMPILE;
@@ -120,6 +125,74 @@ int ui_but_anim_expression_set(uiBut *but, const char *str)
        return 0;
 }
 
+/* create new expression for button (i.e. a "scripted driver"), if it can be created... */
+int ui_but_anim_expression_create(uiBut *but, const char *str)
+{
+       bContext *C = but->block->evil_C;
+       ID *id;
+       FCurve *fcu;
+       char *path;
+       short ok=0;
+       
+       /* button must have RNA-pointer to a numeric-capable property */
+       if (ELEM(NULL, but->rnapoin.data, but->rnaprop)) {
+               if (G.f & G_DEBUG) 
+                       printf("ERROR: create expression failed - button has no RNA info attached\n");
+               return 0;
+       }
+       
+       /* make sure we have animdata for this */
+       // FIXME: until materials can be handled by depsgraph, don't allow drivers to be created for them
+       id = (ID *)but->rnapoin.id.data;
+       if ((id == NULL) || (GS(id->name)==ID_MA) || (GS(id->name)==ID_TE)) {
+               if (G.f & G_DEBUG)
+                       printf("ERROR: create expression failed - invalid id-datablock for adding drivers (%p)\n", id);
+               return 0;
+       }
+       
+       /* get path */
+       path = RNA_path_from_ID_to_property(&but->rnapoin, but->rnaprop);
+       
+       /* create driver */
+       fcu = verify_driver_fcurve(id, path, but->rnaindex, 1);
+       if (fcu) {
+               ChannelDriver *driver= fcu->driver;
+               
+               if (driver) {
+                       /* set type of driver */
+                       driver->type = DRIVER_TYPE_PYTHON;
+                       
+                       /* set the expression */
+                       // TODO: need some way of identifying variables used
+                       BLI_strncpy(driver->expression, str, sizeof(driver->expression));
+                       
+                       /* FIXME: for now, assume that 
+                        *      - for expressions, users are likely to be using "frame" -> current frame" as a variable
+                        *      - driver_add_new_variable() adds a single-prop variable by default
+                        */
+                       {
+                               DriverVar *dvar;
+                               DriverTarget *dtar;
+                               
+                               dvar = driver_add_new_variable(driver);
+                               BLI_strncpy(dvar->name, "frame", sizeof(dvar->name));
+                               
+                               dtar = &dvar->targets[0];
+                               dtar->id = (ID *)CTX_data_scene(C); // XXX: should we check that C is valid first?
+                               dtar->rna_path = BLI_sprintfN("frame_current");
+                       }
+                       
+                       /* updates */
+                       driver->flag |= DRIVER_FLAG_RECOMPILE;
+                       WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME, NULL);
+               }
+       }
+       
+       MEM_freeN(path);
+       
+       return ok;
+}
+
 void ui_but_anim_autokey(bContext *C, uiBut *but, Scene *scene, float cfra)
 {
        ID *id;