Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoshua Leung <aligorith@gmail.com>2011-07-04 07:12:28 +0400
committerJoshua Leung <aligorith@gmail.com>2011-07-04 07:12:28 +0400
commitde1c4fafc7ecd644dd9ba06dfc7a4f77b4d06683 (patch)
treed9d41fdbb5065bb10a05d6314edaf41114d11eec
parente35806470408d01ddaf844c8ebd93fbc2cfdd68d (diff)
First stages of easier "expressions" creation...
It is now possible to create "scripted expression" drivers by simply clicking on some property, and typing some short Python expression prefixed with a '#'. This will result in a scripted expression driver, with the typed-in text being created. For example, you can click on X-Location of the default cube, and type: #sin(frame) and a new driver will be created for the x-location of the cube. This will use the current frame value, and modulate this with a sine wave. Do note though, that the current frame is a special case here. In the current implementation, a special "frame" driver variable, which references the current scene frame is created automatically, so that this simple and (assumed) common case will work straight out of the box. Future improvements: - Explore possibilities of semi-automated extraction of variables from such expressions, resulting in automated variable extraction. (Doing away with variables completely is definitely 100% off the agenda though) - Look into some ways of defining some shorthands for referencing local data (possibly related to variable extraction?)
-rw-r--r--source/blender/editors/animation/drivers.c2
-rw-r--r--source/blender/editors/include/ED_keyframing.h7
-rw-r--r--source/blender/editors/interface/interface.c4
-rw-r--r--source/blender/editors/interface/interface_anim.c77
-rw-r--r--source/blender/editors/interface/interface_intern.h1
5 files changed, 88 insertions, 3 deletions
diff --git a/source/blender/editors/animation/drivers.c b/source/blender/editors/animation/drivers.c
index 75b54a7529a..c9e422baa3e 100644
--- a/source/blender/editors/animation/drivers.c
+++ b/source/blender/editors/animation/drivers.c
@@ -80,7 +80,7 @@ void free_anim_drivers_copybuf (void);
* 1 - add new Driver FCurve,
* -1 - add new Driver FCurve without driver stuff (for pasting)
*/
-static 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, short add)
{
AnimData *adt;
FCurve *fcu;
diff --git a/source/blender/editors/include/ED_keyframing.h b/source/blender/editors/include/ED_keyframing.h
index 294b9b8475a..0ca3a932266 100644
--- a/source/blender/editors/include/ED_keyframing.h
+++ b/source/blender/editors/include/ED_keyframing.h
@@ -225,6 +225,13 @@ typedef enum eCreateDriverFlags {
/* -------- */
+/* 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);
+
+/* -------- */
+
/* Returns whether there is a driver in the copy/paste buffer to paste */
short ANIM_driver_can_paste(void);
diff --git a/source/blender/editors/interface/interface.c b/source/blender/editors/interface/interface.c
index 30c0f552b72..17a08917a16 100644
--- a/source/blender/editors/interface/interface.c
+++ b/source/blender/editors/interface/interface.c
@@ -1701,6 +1701,10 @@ int ui_set_but_string(bContext *C, uiBut *but, const char *str)
/* driver expression */
return 1;
}
+ else if(str[0]=='#') {
+ /* shortcut to create new driver expression (versus immediate Py-execution) */
+ return ui_but_anim_expression_create(but, str+1);
+ }
else {
/* number editing */
double value;
diff --git a/source/blender/editors/interface/interface_anim.c b/source/blender/editors/interface/interface_anim.c
index 75e7ee701a2..2e172c496a3 100644
--- a/source/blender/editors/interface/interface_anim.c
+++ b/source/blender/editors/interface/interface_anim.c
@@ -6,6 +6,7 @@
#include <stdlib.h>
#include <string.h>
+#include "MEM_guardedalloc.h"
#include "DNA_anim_types.h"
#include "DNA_scene_types.h"
@@ -13,15 +14,19 @@
#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"
@@ -84,7 +89,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;
@@ -96,6 +101,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;
diff --git a/source/blender/editors/interface/interface_intern.h b/source/blender/editors/interface/interface_intern.h
index 8475090b468..8194ad610f7 100644
--- a/source/blender/editors/interface/interface_intern.h
+++ b/source/blender/editors/interface/interface_intern.h
@@ -520,6 +520,7 @@ void ui_but_anim_add_keyingset(struct bContext *C);
void ui_but_anim_remove_keyingset(struct bContext *C);
int ui_but_anim_expression_get(uiBut *but, char *str, int maxlen);
int ui_but_anim_expression_set(uiBut *but, const char *str);
+int ui_but_anim_expression_create(uiBut *but, const char *str);
void ui_but_anim_autokey(struct bContext *C, uiBut *but, struct Scene *scene, float cfra);
#endif