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
This commit is contained in:
2009-11-04 15:16:41 +00:00
parent 4033aba579
commit 3ac98f1abd
2 changed files with 88 additions and 41 deletions

View File

@@ -147,6 +147,7 @@ short ANIM_add_driver (ID *id, const char rna_path[], int array_index, short fla
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,37 +156,46 @@ short ANIM_add_driver (ID *id, const char rna_path[], int array_index, short fla
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);
}
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);
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);
}
}
}
}
@@ -357,27 +367,20 @@ static int add_driver_button_exec (bContext *C, wmOperator *op)
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;
for (a=0; a<length; a++)
success+= ANIM_add_driver(ptr.id.data, path, index+a, 0, DRIVER_TYPE_PYTHON);
if (path) {
success+= ANIM_add_driver(ptr.id.data, path, index, 0, DRIVER_TYPE_PYTHON);
MEM_freeN(path);
}

View File

@@ -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 @@ static PyObject *pyrna_struct_keyframe_insert(BPy_StructRNA * self, PyObject *ar
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 @@ static struct PyMethodDef pyrna_struct_methods[] = {
/* 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},