diff options
author | Willian Padovani Germano <wpgermano@gmail.com> | 2006-04-30 20:22:31 +0400 |
---|---|---|
committer | Willian Padovani Germano <wpgermano@gmail.com> | 2006-04-30 20:22:31 +0400 |
commit | 89dab4397d9eb2b9970ba28edeaf95c578dcaab4 (patch) | |
tree | 267c2faf6a9f2475630df2894a5e24832bc9ace4 /source/blender/blenkernel | |
parent | 3b84767824d4f9666dfc5eea1fcf134d7643fb27 (diff) |
Pydrivers: Ipo Drivers controlled by Python expressions
wiki with info: http://mediawiki.blender.org/index.php/BlenderDev/PyDrivers
(there are two sample .blends in the patch tracker entry, last link in
the wiki page)
Notes:
In usiblender.c I just made Python exit before the main library gets
freed. I found a situation with pydrivers where py's gc tried to del
objects on exit and their ID's were not valid anymore (so sigsegv).
Ton needs to check the depsgraph part.
For now pydrivers can reference their own object, something normal
ipodrivers can't. This seems to work fine and is quite useful, but if
tests prove the restriction is necessary, we just need to uncomment a
piece of code in EXPP_interface.c, marked with "XXX".
Thanks Ton for the ipodrivers code and adding the hooks for the py part
and Martin for the "Button Python Evaluation" patch from which I started
this one.
Anyone interested, please check the wiki, the .blends (they have
README's) and tell me about any issue.
Diffstat (limited to 'source/blender/blenkernel')
-rw-r--r-- | source/blender/blenkernel/BKE_bad_level_calls.h | 7 | ||||
-rw-r--r-- | source/blender/blenkernel/bad_level_call_stubs/stubs.c | 10 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/depsgraph.c | 44 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/ipo.c | 13 |
4 files changed, 64 insertions, 10 deletions
diff --git a/source/blender/blenkernel/BKE_bad_level_calls.h b/source/blender/blenkernel/BKE_bad_level_calls.h index f261f786dd4..10248fad220 100644 --- a/source/blender/blenkernel/BKE_bad_level_calls.h +++ b/source/blender/blenkernel/BKE_bad_level_calls.h @@ -57,13 +57,20 @@ struct Sequence; struct ListBase; void build_seqar(struct ListBase *seqbase, struct Sequence ***seqar, int *totseq); +/* BPython API */ struct ID; struct Script; struct Text; +struct IpoDriver; /* DNA_curve_types.h */ +struct Object; void BPY_do_pyscript (struct ID *id, short int event); void BPY_clear_script (struct Script *script); void BPY_free_compiled_text (struct Text *text); void BPY_free_screen_spacehandlers (struct bScreen *sc); +/* ipo.c: */ +float BPY_pydriver_eval(struct IpoDriver *driver); +/* depsgraph.c: */ +struct Object **BPY_pydriver_get_objects(struct IpoDriver *driver); /* writefile.c */ struct Oops; diff --git a/source/blender/blenkernel/bad_level_call_stubs/stubs.c b/source/blender/blenkernel/bad_level_call_stubs/stubs.c index 7bc4e664429..3e858aca631 100644 --- a/source/blender/blenkernel/bad_level_call_stubs/stubs.c +++ b/source/blender/blenkernel/bad_level_call_stubs/stubs.c @@ -109,6 +109,16 @@ void BPY_do_pyscript(ID *id, short int event){} void BPY_clear_script(Script *script){} void BPY_free_compiled_text(struct Text *text){} void BPY_free_screen_spacehandlers (struct bScreen *sc){} +float BPY_pydriver_eval(struct IpoDriver *driver) +{ + return 0; +} + +/* depsgraph.c: */ +struct Object **BPY_pydriver_get_objects(struct IpoDriver *driver) +{ + return 0; +} /* writefile.c */ /* struct Oops; */ diff --git a/source/blender/blenkernel/intern/depsgraph.c b/source/blender/blenkernel/intern/depsgraph.c index b6a85bbe910..42898bc8976 100644 --- a/source/blender/blenkernel/intern/depsgraph.c +++ b/source/blender/blenkernel/intern/depsgraph.c @@ -75,7 +75,8 @@ #include "MEM_guardedalloc.h" #include "blendef.h" - +#include "BPY_extern.h" + #include "depsgraph_private.h" /* Queue and stack operations for dag traversal @@ -302,12 +303,41 @@ static void dag_add_driver_relation(Ipo *ipo, DagForest *dag, DagNode *node, int DagNode *node1; for(icu= ipo->curve.first; icu; icu= icu->next) { - if(icu->driver && icu->driver->ob) { - node1 = dag_get_node(dag, icu->driver->ob); - if(icu->driver->blocktype==ID_AR) - dag_add_relation(dag, node1, node, isdata?DAG_RL_DATA_DATA:DAG_RL_DATA_OB); - else - dag_add_relation(dag, node1, node, isdata?DAG_RL_OB_DATA:DAG_RL_OB_OB); + if(icu->driver) { + + if (icu->driver->type == IPO_DRIVER_TYPE_PYTHON) { + + if ((icu->driver->flag & IPO_DRIVER_FLAG_INVALID) || (icu->driver->name[0] == '\0')) + continue; /* empty or invalid expression */ + else { + /* now we need refs to all objects mentioned in this + * pydriver expression, to call 'dag_add_relation' + * for each of them */ + Object **obarray = BPY_pydriver_get_objects(icu->driver); + if (obarray) { + Object *ob, **oba = obarray; + + while (*oba) { + ob = *oba; + node1 = dag_get_node(dag, ob); + if (ob->type == OB_ARMATURE) + dag_add_relation(dag, node1, node, isdata?DAG_RL_DATA_DATA:DAG_RL_DATA_OB); + else + dag_add_relation(dag, node1, node, isdata?DAG_RL_OB_DATA:DAG_RL_OB_OB); + oba++; + } + + MEM_freeN(obarray); + } + } + } + else if (icu->driver->ob) { + node1 = dag_get_node(dag, icu->driver->ob); + if(icu->driver->blocktype==ID_AR) + dag_add_relation(dag, node1, node, isdata?DAG_RL_DATA_DATA:DAG_RL_DATA_OB); + else + dag_add_relation(dag, node1, node, isdata?DAG_RL_OB_DATA:DAG_RL_OB_OB); + } } } } diff --git a/source/blender/blenkernel/intern/ipo.c b/source/blender/blenkernel/intern/ipo.c index 9efb74ffa35..27bf9c1744f 100644 --- a/source/blender/blenkernel/intern/ipo.c +++ b/source/blender/blenkernel/intern/ipo.c @@ -75,6 +75,7 @@ #include "BKE_main.h" #include "BKE_mesh.h" #include "BKE_object.h" +#include "BPY_extern.h" /* for BPY_pydriver_eval() */ #define SMALL -1.0e-10 @@ -739,12 +740,18 @@ void berekenx(float *f, float *o, int b) static float eval_driver(IpoDriver *driver) { - if(driver->flag & IPO_DRIVER_PYTHON) { - printf("Execute %s\n", driver->name); + if(driver->type == IPO_DRIVER_TYPE_PYTHON) { + /* check for empty or invalid expression */ + if ((driver->name[0] == '\0') || + (driver->flag & IPO_DRIVER_FLAG_INVALID)) + return 0.0f; + /* this evals the expression and returns its result: + * (on errors it reports, then returns 0.0f) */ + return BPY_pydriver_eval(driver); } else { Object *ob= driver->ob; - + if(ob==NULL) return 0.0f; if(driver->blocktype==ID_OB) { |