Changeset View
Changeset View
Standalone View
Standalone View
source/blender/python/intern/bpy_driver.c
| Show All 36 Lines | |||||
| #include "BLI_listbase.h" | #include "BLI_listbase.h" | ||||
| #include "BLI_math_base.h" | #include "BLI_math_base.h" | ||||
| #include "BLI_string.h" | #include "BLI_string.h" | ||||
| #include "BKE_fcurve.h" | #include "BKE_fcurve.h" | ||||
| #include "BKE_global.h" | #include "BKE_global.h" | ||||
| #include "RNA_access.h" | |||||
| #include "bpy_driver.h" | #include "bpy_driver.h" | ||||
| extern void BPY_update_rna_module(void); | extern void BPY_update_rna_module(void); | ||||
| /* XXX, use some special include? - rather not include all of bpy_rna.h here */ | |||||
| extern PyObject *pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop); | |||||
| extern PyObject *pyrna_array_index(PointerRNA *ptr, PropertyRNA *prop, int index); | |||||
| extern PyObject *pyrna_struct_CreatePyObject(PointerRNA *ptr); | |||||
| /* for pydrivers (drivers using one-line Python expressions to express relationships between targets) */ | /* for pydrivers (drivers using one-line Python expressions to express relationships between targets) */ | ||||
| PyObject *bpy_pydriver_Dict = NULL; | PyObject *bpy_pydriver_Dict = NULL; | ||||
| /* For faster execution we keep a special dictionary for pydrivers, with | /* For faster execution we keep a special dictionary for pydrivers, with | ||||
| * the needed modules and aliases. | * the needed modules and aliases. | ||||
| */ | */ | ||||
| int bpy_pydriver_create_dict(void) | int bpy_pydriver_create_dict(void) | ||||
| ▲ Show 20 Lines • Show All 200 Lines • ▼ Show 20 Lines | float BPY_driver_exec(ChannelDriver *driver, const float evaltime) | ||||
| else { | else { | ||||
| expr_vars = PyTuple_GET_ITEM(((PyObject *)driver->expr_comp), 1); | expr_vars = PyTuple_GET_ITEM(((PyObject *)driver->expr_comp), 1); | ||||
| } | } | ||||
| /* add target values to a dict that will be used as '__locals__' dict */ | /* add target values to a dict that will be used as '__locals__' dict */ | ||||
| driver_vars = PyDict_New(); // XXX do we need to decref this? | driver_vars = PyDict_New(); // XXX do we need to decref this? | ||||
| for (dvar = driver->variables.first, i = 0; dvar; dvar = dvar->next) { | for (dvar = driver->variables.first, i = 0; dvar; dvar = dvar->next) { | ||||
| PyObject *driver_arg = NULL; | PyObject *driver_arg = NULL; | ||||
| float tval = 0.0f; | |||||
| /* support for any RNA data */ | |||||
| #if 1 | |||||
| if ((dvar->type == DVAR_TYPE_SINGLE_PROP) && | |||||
| (dvar->flag & DVAR_FLAG_AS_OBJECT)) | |||||
| { | |||||
| PointerRNA ptr; | |||||
| PropertyRNA *prop = NULL; | |||||
| int index; | |||||
| if (driver_get_variable_object(driver, &dvar->targets[0], &ptr, &prop, &index)) { | |||||
| if (prop) { | |||||
| if (index != -1) { | |||||
| if (index >= RNA_property_array_length(&ptr, prop) || index < 0) { | |||||
| /* can't resolve, fallback to None */ | |||||
| driver_arg = Py_None; | |||||
| Py_INCREF(driver_arg); | |||||
| } | |||||
| else { | |||||
| /* object, property & index */ | |||||
| driver_arg = pyrna_array_index(&ptr, prop, index); | |||||
| } | |||||
| } | |||||
| else { | |||||
| /* object & property */ | |||||
| driver_arg = pyrna_prop_to_py(&ptr, prop); | |||||
| } | |||||
| } | |||||
| else { | |||||
| /* object only */ | |||||
| driver_arg = pyrna_struct_CreatePyObject(&ptr); | |||||
| } | |||||
| } | |||||
| else { | |||||
| /* can't resolve, fallback to None */ | |||||
| driver_arg = Py_None; | |||||
| Py_INCREF(driver_arg); | |||||
| } | |||||
| } | |||||
| else | |||||
| #endif | |||||
| { | |||||
| /* try to get variable value */ | /* try to get variable value */ | ||||
| tval = driver_get_variable_value(driver, dvar); | float tval = driver_get_variable_value(driver, dvar); | ||||
| driver_arg = PyFloat_FromDouble((double)tval); | driver_arg = PyFloat_FromDouble((double)tval); | ||||
| } | |||||
| /* try to add to dictionary */ | /* try to add to dictionary */ | ||||
| /* if (PyDict_SetItemString(driver_vars, dvar->name, driver_arg)) { */ | /* if (PyDict_SetItemString(driver_vars, dvar->name, driver_arg)) { */ | ||||
| if (PyDict_SetItem(driver_vars, PyTuple_GET_ITEM(expr_vars, i++), driver_arg) < 0) { | if (PyDict_SetItem(driver_vars, PyTuple_GET_ITEM(expr_vars, i++), driver_arg) < 0) { | ||||
| /* this target failed - bad name */ | /* this target failed - bad name */ | ||||
| if (targets_ok) { | if (targets_ok) { | ||||
| /* first one - print some extra info for easier identification */ | /* first one - print some extra info for easier identification */ | ||||
| fprintf(stderr, "\nBPY_driver_eval() - Error while evaluating PyDriver:\n"); | fprintf(stderr, "\nBPY_driver_eval() - Error while evaluating PyDriver:\n"); | ||||
| targets_ok = 0; | targets_ok = 0; | ||||
| ▲ Show 20 Lines • Show All 48 Lines • Show Last 20 Lines | |||||