[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [24312] trunk/blender/source/blender: python function for adding a driver.
Campbell Barton
ideasman42 at gmail.com
Wed Nov 4 16:16:41 CET 2009
Revision: 24312
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=24312
Author: campbellbarton
Date: 2009-11-04 16:16:41 +0100 (Wed, 04 Nov 2009)
Log Message:
-----------
python function for adding a driver. eg
ob.driver_add("location")
ob.driver_add("location", 0) # x location only
Also changed ANIM_add_driver so an index of -1 adds drivers to every item in the array
Modified Paths:
--------------
trunk/blender/source/blender/editors/animation/drivers.c
trunk/blender/source/blender/python/intern/bpy_rna.c
Modified: trunk/blender/source/blender/editors/animation/drivers.c
===================================================================
--- trunk/blender/source/blender/editors/animation/drivers.c 2009-11-04 15:03:26 UTC (rev 24311)
+++ trunk/blender/source/blender/editors/animation/drivers.c 2009-11-04 15:16:41 UTC (rev 24312)
@@ -147,6 +147,7 @@
PointerRNA id_ptr, ptr;
PropertyRNA *prop;
FCurve *fcu;
+ int array_index_max = array_index+1;
/* validate pointer first - exit if failure */
RNA_id_pointer_create(id, &id_ptr);
@@ -155,38 +156,47 @@
return 0;
}
- /* create F-Curve with Driver */
- fcu= verify_driver_fcurve(id, rna_path, array_index, 1);
+ if(array_index==-1) { /* Key All */
+ array_index= 0;
+ array_index_max= RNA_property_array_length(&ptr, prop) + 1;
+ }
- if (fcu && fcu->driver) {
- fcu->driver->type= type;
+ /* will only loop once unless the array index was -1 */
+ for( ; array_index < array_index_max; array_index++) {
- /* fill in current value for python */
- if (type == DRIVER_TYPE_PYTHON) {
- PropertyType proptype= RNA_property_type(prop);
- int array= RNA_property_array_length(&ptr, prop);
- char *expression= fcu->driver->expression;
- int val, maxlen= sizeof(fcu->driver->expression);
- float fval;
+ /* create F-Curve with Driver */
+ fcu= verify_driver_fcurve(id, rna_path, array_index, 1);
+
+ if (fcu && fcu->driver) {
+ fcu->driver->type= type;
- if (proptype == PROP_BOOLEAN) {
- if (!array) val= RNA_property_boolean_get(&ptr, prop);
- else val= RNA_property_boolean_get_index(&ptr, prop, array_index);
+ /* fill in current value for python */
+ if (type == DRIVER_TYPE_PYTHON) {
+ PropertyType proptype= RNA_property_type(prop);
+ int array= RNA_property_array_length(&ptr, prop);
+ char *expression= fcu->driver->expression;
+ int val, maxlen= sizeof(fcu->driver->expression);
+ float fval;
- BLI_strncpy(expression, (val)? "True": "False", maxlen);
+ if (proptype == PROP_BOOLEAN) {
+ if (!array) val= RNA_property_boolean_get(&ptr, prop);
+ else val= RNA_property_boolean_get_index(&ptr, prop, array_index);
+
+ BLI_strncpy(expression, (val)? "True": "False", maxlen);
+ }
+ else if (proptype == PROP_INT) {
+ if (!array) val= RNA_property_int_get(&ptr, prop);
+ else val= RNA_property_int_get_index(&ptr, prop, array_index);
+
+ BLI_snprintf(expression, maxlen, "%d", val);
+ }
+ else if (proptype == PROP_FLOAT) {
+ if (!array) fval= RNA_property_float_get(&ptr, prop);
+ else fval= RNA_property_float_get_index(&ptr, prop, array_index);
+
+ BLI_snprintf(expression, maxlen, "%.3f", fval);
+ }
}
- else if (proptype == PROP_INT) {
- if (!array) val= RNA_property_int_get(&ptr, prop);
- else val= RNA_property_int_get_index(&ptr, prop, array_index);
-
- BLI_snprintf(expression, maxlen, "%d", val);
- }
- else if (proptype == PROP_FLOAT) {
- if (!array) fval= RNA_property_float_get(&ptr, prop);
- else fval= RNA_property_float_get_index(&ptr, prop, array_index);
-
- BLI_snprintf(expression, maxlen, "%.3f", fval);
- }
}
}
@@ -357,28 +367,21 @@
PropertyRNA *prop= NULL;
char *path;
short success= 0;
- int a, index, length, all= RNA_boolean_get(op->ptr, "all");
+ int index, length, all= RNA_boolean_get(op->ptr, "all");
/* try to create driver using property retrieved from UI */
memset(&ptr, 0, sizeof(PointerRNA));
uiAnimContextProperty(C, &ptr, &prop, &index);
-
+
+ if (all)
+ index= -1;
+
if (ptr.data && prop && RNA_property_animateable(ptr.data, prop)) {
path= RNA_path_from_ID_to_property(&ptr, prop);
- if (path) {
- if (all) {
- length= RNA_property_array_length(&ptr, prop);
-
- if (length) index= 0;
- else length= 1;
- }
- else
- length= 1;
+ if (path) {
+ success+= ANIM_add_driver(ptr.id.data, path, index, 0, DRIVER_TYPE_PYTHON);
- for (a=0; a<length; a++)
- success+= ANIM_add_driver(ptr.id.data, path, index+a, 0, DRIVER_TYPE_PYTHON);
-
MEM_freeN(path);
}
}
Modified: trunk/blender/source/blender/python/intern/bpy_rna.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_rna.c 2009-11-04 15:03:26 UTC (rev 24311)
+++ trunk/blender/source/blender/python/intern/bpy_rna.c 2009-11-04 15:16:41 UTC (rev 24312)
@@ -41,6 +41,7 @@
/* only for keyframing */
#include "DNA_scene_types.h"
+#include "DNA_anim_types.h"
#include "ED_keyframing.h"
#define USE_MATHUTILS
@@ -1193,6 +1194,48 @@
return result;
}
+
+static PyObject *pyrna_struct_driver_add(BPy_StructRNA * self, PyObject *args)
+{
+ char *path, *path_full;
+ int index= -1; /* default to all */
+ PropertyRNA *prop;
+ PyObject *result;
+
+ if (!PyArg_ParseTuple(args, "s|i:driver_add", &path, &index))
+ return NULL;
+
+ if (self->ptr.data==NULL) {
+ PyErr_Format( PyExc_TypeError, "driver_add, this struct has no data, cant be animated", path);
+ return NULL;
+ }
+
+ prop = RNA_struct_find_property(&self->ptr, path);
+
+ if (prop==NULL) {
+ PyErr_Format( PyExc_TypeError, "driver_add, property \"%s\" not found", path);
+ return NULL;
+ }
+
+ if (!RNA_property_animateable(&self->ptr, prop)) {
+ PyErr_Format( PyExc_TypeError, "driver_add, property \"%s\" not animatable", path);
+ return NULL;
+ }
+
+ path_full= RNA_path_from_ID_to_property(&self->ptr, prop);
+
+ if (path_full==NULL) {
+ PyErr_Format( PyExc_TypeError, "driver_add, could not make path to \"%s\"", path);
+ return NULL;
+ }
+
+ result= PyBool_FromLong( ANIM_add_driver((ID *)self->ptr.id.data, path_full, index, 0, DRIVER_TYPE_PYTHON));
+ MEM_freeN(path_full);
+
+ return result;
+}
+
+
static PyObject *pyrna_struct_is_property_set(BPy_StructRNA * self, PyObject *args)
{
char *name;
@@ -1878,6 +1921,7 @@
/* maybe this become and ID function */
{"keyframe_insert", (PyCFunction)pyrna_struct_keyframe_insert, METH_VARARGS, NULL},
+ {"driver_add", (PyCFunction)pyrna_struct_driver_add, METH_VARARGS, NULL},
{"is_property_set", (PyCFunction)pyrna_struct_is_property_set, METH_VARARGS, NULL},
{"is_property_hidden", (PyCFunction)pyrna_struct_is_property_hidden, METH_VARARGS, NULL},
More information about the Bf-blender-cvs
mailing list