Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWillian Padovani Germano <wpgermano@gmail.com>2006-04-30 20:22:31 +0400
committerWillian Padovani Germano <wpgermano@gmail.com>2006-04-30 20:22:31 +0400
commit89dab4397d9eb2b9970ba28edeaf95c578dcaab4 (patch)
tree267c2faf6a9f2475630df2894a5e24832bc9ace4 /source/blender/python
parent3b84767824d4f9666dfc5eea1fcf134d7643fb27 (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/python')
-rw-r--r--source/blender/python/BPY_extern.h5
-rw-r--r--source/blender/python/BPY_interface.c190
-rw-r--r--source/blender/python/api2_2x/EXPP_interface.c99
-rw-r--r--source/blender/python/api2_2x/EXPP_interface.h13
-rw-r--r--source/blender/python/api2_2x/Object.c13
5 files changed, 319 insertions, 1 deletions
diff --git a/source/blender/python/BPY_extern.h b/source/blender/python/BPY_extern.h
index ffb5f258d8d..cc187ea3a0d 100644
--- a/source/blender/python/BPY_extern.h
+++ b/source/blender/python/BPY_extern.h
@@ -37,6 +37,8 @@ extern char bprogname[]; /* holds a copy of argv[0], from creator.c */
struct Text; /* defined in DNA_text_types.h */
struct ID; /* DNA_ID.h */
+struct Object; /* DNA_object_types.h */
+struct IpoDriver; /* DNA_curve_types.h */
struct ScriptLink; /* DNA_scriptlink_types.h */
struct ListBase; /* DNA_listBase.h */
struct SpaceText; /* DNA_space_types.h */
@@ -79,6 +81,9 @@ extern "C" {
int BPY_do_spacehandlers(struct ScrArea *sa, unsigned short event,
unsigned short space_event);
+ float BPY_pydriver_eval(struct IpoDriver *driver);
+ struct Object **BPY_pydriver_get_objects(struct IpoDriver *driver);
+
/* format importer hook */
int BPY_call_importloader( char *name );
diff --git a/source/blender/python/BPY_interface.c b/source/blender/python/BPY_interface.c
index ff262b1a581..6a79e673345 100644
--- a/source/blender/python/BPY_interface.c
+++ b/source/blender/python/BPY_interface.c
@@ -43,6 +43,7 @@
#include "BKE_library.h"
#include "BKE_object.h" /* during_scriptlink() */
#include "BKE_text.h"
+#include "DNA_curve_types.h" /* for struct IpoDriver */
#include "DNA_screen_types.h"
#include "DNA_userdef_types.h" /* for U.pythondir */
#include "MEM_guardedalloc.h"
@@ -73,6 +74,9 @@
*/
//#include "api2_2x/Registry.h"
+/* for pydrivers (ipo drivers defined by one-line Python expressions) */
+PyObject *bpy_pydriver_Dict = NULL;
+
/*Declares the modules and their initialization functions
*These are TOP-LEVEL modules e.g. import `module` - there is no
*support for packages here e.g. import `package.module` */
@@ -175,6 +179,11 @@ void BPY_end_python( void )
bpy_registryDict = NULL;
}
+ if( bpy_pydriver_Dict ) {
+ Py_DECREF( bpy_pydriver_Dict );
+ bpy_pydriver_Dict = NULL;
+ }
+
Py_Finalize( );
BPyMenu_RemoveAllEntries( ); /* freeing bpymenu mem */
@@ -919,6 +928,187 @@ void BPY_clear_script( Script * script )
unlink_script( script );
}
+/* PyDrivers */
+
+/* PyDrivers are Ipo Drivers governed by expressions written in Python.
+ * Expressions here are one-liners that evaluate to a float value. */
+
+/* For faster execution we keep a special dictionary for pydrivers, with
+ * the needed modules and aliases. */
+static int bpy_pydriver_create_dict(void)
+{
+ PyObject *d, *mod;
+
+ if (bpy_pydriver_Dict) return -1;
+
+ d = PyDict_New();
+ if (!d) return -1;
+
+ bpy_pydriver_Dict = d;
+
+ /* import some modules: builtins, Blender, math, Blender.noise */
+
+ PyDict_SetItemString(d, "__builtins__", PyEval_GetBuiltins());
+
+ mod = PyImport_ImportModule("Blender");
+ if (mod) {
+ PyDict_SetItemString(d, "Blender", mod);
+ PyDict_SetItemString(d, "b", mod);
+ Py_DECREF(mod);
+ }
+
+ mod = PyImport_ImportModule("math");
+ if (mod) {
+ PyDict_SetItemString(d, "math", mod);
+ PyDict_SetItemString(d, "m", mod);
+ Py_DECREF(mod);
+ }
+
+ mod = PyImport_ImportModule("Blender.Noise");
+ if (mod) {
+ PyDict_SetItemString(d, "noise", mod);
+ PyDict_SetItemString(d, "n", mod);
+ Py_DECREF(mod);
+ }
+
+ /* If there's a Blender text called pydrivers.py, import it.
+ * Users can add their own functions to this module. */
+ mod = importText("pydrivers"); /* can also use PyImport_Import() */
+ if (mod) {
+ PyDict_SetItemString(d, "pydrivers", mod);
+ PyDict_SetItemString(d, "p", mod);
+ Py_DECREF(mod);
+ }
+ else
+ PyErr_Clear();
+
+ /* short aliases for some Get() functions: */
+
+ /* ob(obname) == Blender.Object.Get(obname) */
+ mod = PyImport_ImportModule("Blender.Object");
+ if (mod) {
+ PyObject *fcn = PyObject_GetAttrString(mod, "Get");
+ Py_DECREF(mod);
+ if (fcn)
+ PyDict_SetItemString(d, "ob", fcn);
+ }
+
+ /* me(meshname) == Blender.Mesh.Get(meshname) */
+ mod = PyImport_ImportModule("Blender.Mesh");
+ if (mod) {
+ PyObject *fcn = PyObject_GetAttrString(mod, "Get");
+ Py_DECREF(mod);
+ if (fcn)
+ PyDict_SetItemString(d, "me", fcn);
+ }
+
+ /* ma(matname) == Blender.Material.Get(matname) */
+ mod = PyImport_ImportModule("Blender.Material");
+ if (mod) {
+ PyObject *fcn = PyObject_GetAttrString(mod, "Get");
+ Py_DECREF(mod);
+ if (fcn)
+ PyDict_SetItemString(d, "ma", fcn);
+ }
+
+ return 0;
+}
+
+/* error return function for BPY_eval_pydriver */
+static float pydriver_error(IpoDriver *driver) {
+
+ if (bpy_pydriver_oblist)
+ bpy_pydriver_freeList();
+
+ if (bpy_pydriver_Dict) { /* free the global dict used by pydrivers */
+ Py_DECREF(bpy_pydriver_Dict);
+ bpy_pydriver_Dict = NULL;
+ }
+
+ driver->flag |= IPO_DRIVER_FLAG_INVALID; /* py expression failed */
+
+ fprintf(stderr, "\nError in Ipo Driver: Object %s\nThis is the failed Python expression:\n'%s'\n\n", driver->ob->id.name+2, driver->name);
+
+ PyErr_Print();
+
+ return 0.0f;
+}
+
+/* for depsgraph.c, runs py expr once to collect all refs. made
+ * to objects (self refs. to the object that owns the py driver
+ * are not allowed). */
+struct Object **BPY_pydriver_get_objects(IpoDriver *driver)
+{
+ /*if (!driver || !driver->ob || driver->name[0] == '\0')
+ return NULL;*/
+
+ /*PyErr_Clear();*/
+
+ /* clear the flag that marks invalid python expressions */
+ driver->flag &= ~IPO_DRIVER_FLAG_INVALID;
+
+ /* tell we're running a pydriver, so Get() functions know they need
+ * to add the requested obj to our list */
+ bpy_pydriver_running(1);
+
+ /* append driver owner object as the 1st ob in the list;
+ * we put it there to make sure it is not itself referenced in
+ * its pydriver expression */
+ bpy_pydriver_appendToList(driver->ob);
+
+ /* this will append any other ob referenced in expr (driver->name)
+ * or set the driver's error flag if driver's py expression fails */
+ BPY_pydriver_eval(driver);
+
+ bpy_pydriver_running(0); /* ok, we're done */
+
+ return bpy_pydriver_obArrayFromList(); /* NULL if eval failed */
+}
+
+/* This evals py driver expressions, 'expr' is a Python expression that
+ * should evaluate to a float number, which is returned. */
+float BPY_pydriver_eval(IpoDriver *driver)
+{
+ char *expr = NULL;
+ PyObject *retval, *floatval;
+ float result = 0.0f; /* default return */
+
+ if (!driver) return result;
+
+ expr = driver->name; /* the py expression to be evaluated */
+ if (!expr || expr[0]=='\0') return result;
+
+ if (!bpy_pydriver_Dict) {
+ if (bpy_pydriver_create_dict() != 0) {
+ fprintf(stderr, "Pydriver error: couldn't create Python dictionary");
+ return result;
+ }
+ }
+
+ retval = PyRun_String(expr, Py_eval_input, bpy_pydriver_Dict,
+ bpy_pydriver_Dict);
+
+ if (retval == NULL) {
+ return pydriver_error(driver);
+ }
+ else {
+ floatval = PyNumber_Float(retval);
+ Py_DECREF(retval);
+ }
+
+ if (floatval == NULL)
+ return pydriver_error(driver);
+ else {
+ result = (float)PyFloat_AsDouble(floatval);
+ Py_DECREF(floatval);
+ }
+
+ /* all fine, make sure the "invalid expression" flag is cleared */
+ driver->flag &= ~IPO_DRIVER_FLAG_INVALID;
+
+ return result;
+}
+
/*****************************************************************************/
/* ScriptLinks */
/*****************************************************************************/
diff --git a/source/blender/python/api2_2x/EXPP_interface.c b/source/blender/python/api2_2x/EXPP_interface.c
index 9f20db03c07..ee8b04ebde9 100644
--- a/source/blender/python/api2_2x/EXPP_interface.c
+++ b/source/blender/python/api2_2x/EXPP_interface.c
@@ -34,6 +34,9 @@
#include "EXPP_interface.h"
#include "BLI_blenlib.h"
+#include "MEM_guardedalloc.h"
+#include "BLI_linklist.h" /* linked list: LinkNode struct and functions */
+#include "DNA_object_types.h"
#include "DNA_space_types.h" /* for FILE_MAXDIR, FILE_MAXFILE */
#include "Blender.h"
@@ -129,3 +132,99 @@ char *bpy_gethome(int append_scriptsdir)
return NULL;
}
+
+/* PyDrivers */
+
+/*
+ * Pydrivers are Blender Ipo Drivers defined by Python expressions.
+ * We need to tell DAG about objects used in these expressions, so we
+ * eval each expression to collect the ob refs. in it.
+ */
+
+/* these are checked for example in Object.c: M_Object_Get (Object.Get())
+ * to collect the refs. */
+static int pydriver_running = 0;
+
+int bpy_during_pydriver(void)
+{
+ return pydriver_running;
+}
+
+void bpy_pydriver_running(int state)
+{
+ pydriver_running = state;
+}
+
+/* Obj references are collected in this extern linked list: */
+LinkNode *bpy_pydriver_oblist = NULL;
+
+void bpy_pydriver_freeList(void)
+{
+ BLI_linklist_free(bpy_pydriver_oblist, NULL);
+ bpy_pydriver_oblist = NULL;
+}
+
+void bpy_pydriver_appendToList(struct Object *ob)
+{
+ LinkNode *ln = bpy_pydriver_oblist;
+
+ /* check that the expression is not referencing its owner object */
+
+/* XXX COMMENTED OUT TO TEST IF WE REALLY NEED TO IMPOSE THIS RESTRICTION
+ if (ln && ln->link) {
+ if (ob == (Object *)ln->link) {
+ PyErr_SetString(PyExc_AttributeError,
+ "Python driver expression can't reference its own object");
+ return;
+ }
+ else
+ ln = ln->next;
+ }
+*/
+ while (ln) { /* is ob already in list? ... */
+ if (ob == (Object *)ln->link)
+ break;
+ ln = ln->next;
+ }
+
+ if (!ln) /* ... not yet, append it */
+ BLI_linklist_append(&bpy_pydriver_oblist, (void *)ob);
+
+ return;
+}
+
+/* Get an array from our linked list of objs referenced in the
+ * current pydriver. The first node in the list is discarded,
+ * since it is the actual pydriver owner, which shouldn't be
+ * passed to the depsgraph (no self references). */
+struct Object **bpy_pydriver_obArrayFromList(void)
+{
+ Object **obarray = NULL;
+
+ if (bpy_pydriver_oblist) {
+ int i;
+ short len = BLI_linklist_length(bpy_pydriver_oblist);
+
+ if (len > 1) {
+
+ obarray = (Object **)MEM_mallocN(sizeof(Object*)*len,
+ "pydriver array");
+
+ if (obarray) {
+ LinkNode *ln = bpy_pydriver_oblist;
+ ln = ln->next; /* skip first ob, which is the pydriver owner */
+
+ for (i = 0; i < len-1; i++) {
+ obarray[i] = (Object *)ln->link;
+ ln = ln->next;
+ }
+
+ obarray[len-1] = NULL; /* NULL-terminated array */
+ }
+ }
+ bpy_pydriver_freeList();
+ }
+
+ return obarray;
+}
+
diff --git a/source/blender/python/api2_2x/EXPP_interface.h b/source/blender/python/api2_2x/EXPP_interface.h
index dfcf7abf1da..80767af1c01 100644
--- a/source/blender/python/api2_2x/EXPP_interface.h
+++ b/source/blender/python/api2_2x/EXPP_interface.h
@@ -33,11 +33,24 @@
#ifndef EXPP_INTERFACE_H
#define EXPP_INTERFACE_H
+struct Object;
struct Script;
+struct LinkNode;
+
+extern struct LinkNode *bpy_pydriver_oblist;
void initBlenderApi2_2x( void );
char *bpy_gethome( int append_scriptsdir );
void discardFromBDict( char *key );
void EXPP_Library_Close( void ); /* in Library.c, used by BPY_end_python */
+/* PyDrivers */
+
+void bpy_pydriver_freeList(void);
+void bpy_pydriver_appendToList(struct Object *ob);
+struct Object **bpy_pydriver_obArrayFromList(void);
+
+int bpy_during_pydriver(void);
+void bpy_pydriver_running(int state);
+
#endif /* EXPP_INTERFACE_H */
diff --git a/source/blender/python/api2_2x/Object.c b/source/blender/python/api2_2x/Object.c
index db012729da6..1132f07a511 100644
--- a/source/blender/python/api2_2x/Object.c
+++ b/source/blender/python/api2_2x/Object.c
@@ -110,6 +110,7 @@ struct rctf;
#include "Group.h"
#include "Modifier.h"
#include "gen_utils.h"
+#include "EXPP_interface.h"
#include "BIF_editkey.h"
/* Defines for insertIpoKey */
@@ -787,6 +788,10 @@ PyObject *M_Object_Get( PyObject * self, PyObject * args )
buffer );
}
+ /* objects used in pydriver expressions need this */
+ if (bpy_during_pydriver())
+ bpy_pydriver_appendToList(object);
+
return Object_CreatePyObject( object );
} else {
/* No argument has been given. Return a list of all objects. */
@@ -794,11 +799,17 @@ PyObject *M_Object_Get( PyObject * self, PyObject * args )
Link *link;
int index;
+ /* do not allow Get() (w/o arguments) inside pydriver, otherwise
+ * we'd have to update all objects in the DAG */
+ if (bpy_during_pydriver())
+ return EXPP_ReturnPyObjError( PyExc_AttributeError,
+ "Object.Get requires an argument when used in pydrivers" );
+
obj_list = PyList_New( BLI_countlist( &( G.main->object ) ) );
if( !obj_list )
return EXPP_ReturnPyObjError( PyExc_SystemError,
- "List creation failed." );
+ "List creation failed." );
link = G.main->object.first;
index = 0;