diff options
author | Campbell Barton <ideasman42@gmail.com> | 2016-07-30 09:34:01 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2016-07-30 09:46:13 +0300 |
commit | 4e845e06704bad3c11297ae8e86b400ef80b2a89 (patch) | |
tree | e26c66b90934e6230f5c4bc67f3fe7419b199ecf /source/blender/python | |
parent | 362b3bbe58ae378d5e154dd1a27d55d913594a1a (diff) |
Py-Driver: add 'self' option
Drivers can use this to refer to the data which the driver is applied to,
useful for objects, bones, to avoid having to create a variable pointing to its self.
Diffstat (limited to 'source/blender/python')
-rw-r--r-- | source/blender/python/BPY_extern.h | 3 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_driver.c | 21 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_driver.h | 3 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_rna_driver.c | 5 | ||||
-rw-r--r-- | source/blender/python/intern/bpy_rna_driver.h | 2 |
5 files changed, 31 insertions, 3 deletions
diff --git a/source/blender/python/BPY_extern.h b/source/blender/python/BPY_extern.h index 4006816e788..3148dab3c50 100644 --- a/source/blender/python/BPY_extern.h +++ b/source/blender/python/BPY_extern.h @@ -32,6 +32,7 @@ #ifndef __BPY_EXTERN_H__ #define __BPY_EXTERN_H__ +struct PathResolvedRNA; struct Text; /* defined in DNA_text_types.h */ struct ID; /* DNA_ID.h */ struct Object; /* DNA_object_types.h */ @@ -85,7 +86,7 @@ void BPY_modules_load_user(struct bContext *C); void BPY_app_handlers_reset(const short do_all); void BPY_driver_reset(void); -float BPY_driver_exec(struct ChannelDriver *driver, const float evaltime); +float BPY_driver_exec(struct PathResolvedRNA *anim_rna, struct ChannelDriver *driver, const float evaltime); void BPY_DECREF(void *pyob_ptr); /* Py_DECREF() */ void BPY_DECREF_RNA_INVALIDATE(void *pyob_ptr); diff --git a/source/blender/python/intern/bpy_driver.c b/source/blender/python/intern/bpy_driver.c index 65b6bd501ce..2f0c054e381 100644 --- a/source/blender/python/intern/bpy_driver.c +++ b/source/blender/python/intern/bpy_driver.c @@ -120,6 +120,22 @@ static void bpy_pydriver_update_dict(const float evaltime) } } +static PyObject *bpy_pydriver_InternStr__self = NULL; + +static void bpy_pydriver_update_dict_self(struct PathResolvedRNA *anim_rna) +{ + if (bpy_pydriver_InternStr__self == NULL) { + bpy_pydriver_InternStr__self = PyUnicode_FromString("self"); + } + + PyObject *item = pyrna_driver_self_from_anim_rna(anim_rna); + PyDict_SetItem(bpy_pydriver_Dict, + bpy_pydriver_InternStr__self, + item); + Py_DECREF(item); + +} + /* Update function, it gets rid of pydrivers global dictionary, forcing * BPY_driver_exec to recreate it. This function is used to force * reloading the Blender text module "pydrivers.py", if available, so @@ -174,7 +190,7 @@ static void pydriver_error(ChannelDriver *driver) * now release the GIL on python operator execution instead, using * PyEval_SaveThread() / PyEval_RestoreThread() so we don't lock up blender. */ -float BPY_driver_exec(ChannelDriver *driver, const float evaltime) +float BPY_driver_exec(struct PathResolvedRNA *anim_rna, ChannelDriver *driver, const float evaltime) { PyObject *driver_vars = NULL; PyObject *retval = NULL; @@ -226,6 +242,9 @@ float BPY_driver_exec(ChannelDriver *driver, const float evaltime) /* update global namespace */ bpy_pydriver_update_dict(evaltime); + if (driver->flag & DRIVER_FLAG_USE_SELF) { + bpy_pydriver_update_dict_self(anim_rna); + } if (driver->expr_comp == NULL) driver->flag |= DRIVER_FLAG_RECOMPILE; diff --git a/source/blender/python/intern/bpy_driver.h b/source/blender/python/intern/bpy_driver.h index 1fccec7e1b2..017a6fe89c5 100644 --- a/source/blender/python/intern/bpy_driver.h +++ b/source/blender/python/intern/bpy_driver.h @@ -28,12 +28,13 @@ #define __BPY_DRIVER_H__ struct ChannelDriver; +struct PathResolvedRNA; int bpy_pydriver_create_dict(void); extern PyObject *bpy_pydriver_Dict; /* externals */ -float BPY_driver_exec(struct ChannelDriver *driver, const float evaltime); +float BPY_driver_exec(struct PathResolvedRNA *anim_rna, struct ChannelDriver *driver, const float evaltime); void BPY_driver_reset(void); #endif /* __BPY_DRIVER_H__ */ diff --git a/source/blender/python/intern/bpy_rna_driver.c b/source/blender/python/intern/bpy_rna_driver.c index 482508a8d85..98fc372f2fc 100644 --- a/source/blender/python/intern/bpy_rna_driver.c +++ b/source/blender/python/intern/bpy_rna_driver.c @@ -77,3 +77,8 @@ PyObject *pyrna_driver_get_variable_value( return driver_arg; } + +PyObject *pyrna_driver_self_from_anim_rna(struct PathResolvedRNA *anim_rna) +{ + return pyrna_struct_CreatePyObject(&anim_rna->ptr); +} diff --git a/source/blender/python/intern/bpy_rna_driver.h b/source/blender/python/intern/bpy_rna_driver.h index 8deac2e4384..2021575537d 100644 --- a/source/blender/python/intern/bpy_rna_driver.h +++ b/source/blender/python/intern/bpy_rna_driver.h @@ -27,7 +27,9 @@ struct ChannelDriver; struct DriverTarget; +struct PathResolvedRNA; PyObject *pyrna_driver_get_variable_value(struct ChannelDriver *driver, struct DriverTarget *dtar); +PyObject *pyrna_driver_self_from_anim_rna(struct PathResolvedRNA *anim_rna); #endif /* __BPY_RNA_DRIVER_H__ */ |