Edit Mesh: Fix buggy occlusion when in xray mode.
[blender.git] / source / blender / python / intern / bpy_driver.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Willian P. Germano, Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/python/intern/bpy_driver.c
24  *  \ingroup pythonintern
25  *
26  * This file defines the 'BPY_driver_exec' to execute python driver expressions,
27  * called by the animation system, there are also some utility functions
28  * to deal with the namespace used for driver execution.
29  */
30
31 /* ****************************************** */
32 /* Drivers - PyExpression Evaluation */
33
34 #include <Python.h>
35
36 #include "DNA_anim_types.h"
37
38 #include "BLI_listbase.h"
39 #include "BLI_math_base.h"
40 #include "BLI_string.h"
41
42 #include "BKE_fcurve.h"
43 #include "BKE_global.h"
44
45 #include "bpy_rna_driver.h"  /* for pyrna_driver_get_variable_value */
46
47 #include "bpy_intern_string.h"
48
49 #include "bpy_driver.h"
50
51 #include "BPY_extern.h"
52
53 extern void BPY_update_rna_module(void);
54
55 #define USE_RNA_AS_PYOBJECT
56
57 /* for pydrivers (drivers using one-line Python expressions to express relationships between targets) */
58 PyObject *bpy_pydriver_Dict = NULL;
59
60 /* For faster execution we keep a special dictionary for pydrivers, with
61  * the needed modules and aliases.
62  */
63 int bpy_pydriver_create_dict(void)
64 {
65         PyObject *d, *mod;
66
67         /* validate namespace for driver evaluation */
68         if (bpy_pydriver_Dict) return -1;
69
70         d = PyDict_New();
71         if (d == NULL)
72                 return -1;
73         else
74                 bpy_pydriver_Dict = d;
75
76         /* import some modules: builtins, bpy, math, (Blender.noise)*/
77         PyDict_SetItemString(d, "__builtins__", PyEval_GetBuiltins());
78
79         mod = PyImport_ImportModule("math");
80         if (mod) {
81                 PyDict_Merge(d, PyModule_GetDict(mod), 0); /* 0 - don't overwrite existing values */
82                 Py_DECREF(mod);
83         }
84
85         /* add bpy to global namespace */
86         mod = PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
87         if (mod) {
88                 PyDict_SetItemString(bpy_pydriver_Dict, "bpy", mod);
89                 Py_DECREF(mod);
90         }
91
92         /* add noise to global namespace */
93         mod = PyImport_ImportModuleLevel("mathutils", NULL, NULL, NULL, 0);
94         if (mod) {
95                 PyObject *modsub = PyDict_GetItemString(PyModule_GetDict(mod), "noise");
96                 PyDict_SetItemString(bpy_pydriver_Dict, "noise", modsub);
97                 Py_DECREF(mod);
98         }
99
100         return 0;
101 }
102
103 /* note, this function should do nothing most runs, only when changing frame */
104 /* not thread safe but neither is python */
105 static struct {
106         float evaltime;
107
108         /* borrowed reference to the 'self' in 'bpy_pydriver_Dict'
109          * keep for as long as the same self is used. */
110         PyObject *self;
111 } g_pydriver_state_prev = {
112         .evaltime = FLT_MAX,
113         .self = NULL,
114 };
115
116 static void bpy_pydriver_namespace_update_frame(const float evaltime)
117 {
118         if (g_pydriver_state_prev.evaltime != evaltime) {
119                 PyObject *item = PyFloat_FromDouble(evaltime);
120                 PyDict_SetItem(bpy_pydriver_Dict, bpy_intern_str_frame, item);
121                 Py_DECREF(item);
122
123                 g_pydriver_state_prev.evaltime = evaltime;
124         }
125 }
126
127 static void bpy_pydriver_namespace_update_self(struct PathResolvedRNA *anim_rna)
128 {
129         if ((g_pydriver_state_prev.self == NULL) ||
130             (pyrna_driver_is_equal_anim_rna(anim_rna, g_pydriver_state_prev.self) == false))
131         {
132                 PyObject *item = pyrna_driver_self_from_anim_rna(anim_rna);
133                 PyDict_SetItem(bpy_pydriver_Dict, bpy_intern_str_self, item);
134                 Py_DECREF(item);
135
136                 g_pydriver_state_prev.self = item;
137         }
138 }
139
140 static void bpy_pydriver_namespace_clear_self(void)
141 {
142         if (g_pydriver_state_prev.self) {
143                 PyDict_DelItem(bpy_pydriver_Dict, bpy_intern_str_self);
144
145                 g_pydriver_state_prev.self = NULL;
146         }
147 }
148
149 /* Update function, it gets rid of pydrivers global dictionary, forcing
150  * BPY_driver_exec to recreate it. This function is used to force
151  * reloading the Blender text module "pydrivers.py", if available, so
152  * updates in it reach pydriver evaluation.
153  */
154 void BPY_driver_reset(void)
155 {
156         PyGILState_STATE gilstate;
157         bool use_gil = true; /* !PyC_IsInterpreterActive(); */
158
159         if (use_gil)
160                 gilstate = PyGILState_Ensure();
161
162         if (bpy_pydriver_Dict) { /* free the global dict used by pydrivers */
163                 PyDict_Clear(bpy_pydriver_Dict);
164                 Py_DECREF(bpy_pydriver_Dict);
165                 bpy_pydriver_Dict = NULL;
166         }
167
168         g_pydriver_state_prev.evaltime = FLT_MAX;
169
170         /* freed when clearing driver dict */
171         g_pydriver_state_prev.self = NULL;
172
173         if (use_gil)
174                 PyGILState_Release(gilstate);
175
176         return;
177 }
178
179 /* error return function for BPY_eval_pydriver */
180 static void pydriver_error(ChannelDriver *driver)
181 {
182         driver->flag |= DRIVER_FLAG_INVALID; /* py expression failed */
183         fprintf(stderr, "\nError in Driver: The following Python expression failed:\n\t'%s'\n\n", driver->expression);
184
185         // BPy_errors_to_report(NULL); // TODO - reports
186         PyErr_Print();
187         PyErr_Clear();
188 }
189
190 /* This evals py driver expressions, 'expr' is a Python expression that
191  * should evaluate to a float number, which is returned.
192  *
193  * (old)note: PyGILState_Ensure() isn't always called because python can call
194  * the bake operator which intern starts a thread which calls scene update
195  * which does a driver update. to avoid a deadlock check PyC_IsInterpreterActive()
196  * if PyGILState_Ensure() is needed - see [#27683]
197  *
198  * (new)note: checking if python is running is not threadsafe [#28114]
199  * now release the GIL on python operator execution instead, using
200  * PyEval_SaveThread() / PyEval_RestoreThread() so we don't lock up blender.
201  *
202  * For copy-on-write we always cache expressions and write errors in the
203  * original driver, otherwise these would get freed while editing. Due to
204  * the GIL this is thread-safe.
205  */
206 float BPY_driver_exec(struct PathResolvedRNA *anim_rna, ChannelDriver *driver, ChannelDriver *driver_orig, const float evaltime)
207 {
208         PyObject *driver_vars = NULL;
209         PyObject *retval = NULL;
210         PyObject *expr_vars; /* speed up by pre-hashing string & avoids re-converting unicode strings for every execution */
211         PyObject *expr_code;
212         PyGILState_STATE gilstate;
213         bool use_gil;
214
215         DriverVar *dvar;
216         double result = 0.0; /* default return */
217         const char *expr;
218         short targets_ok = 1;
219         int i;
220
221         /* get the py expression to be evaluated */
222         expr = driver_orig->expression;
223         if (expr[0] == '\0')
224                 return 0.0f;
225
226         if (!(G.f & G_SCRIPT_AUTOEXEC)) {
227                 if (!(G.f & G_SCRIPT_AUTOEXEC_FAIL_QUIET)) {
228                         G.f |= G_SCRIPT_AUTOEXEC_FAIL;
229                         BLI_snprintf(G.autoexec_fail, sizeof(G.autoexec_fail), "Driver '%s'", expr);
230
231                         printf("skipping driver '%s', automatic scripts are disabled\n", expr);
232                 }
233                 return 0.0f;
234         }
235
236         use_gil = true;  /* !PyC_IsInterpreterActive(); */
237
238         if (use_gil)
239                 gilstate = PyGILState_Ensure();
240
241         /* needed since drivers are updated directly after undo where 'main' is
242          * re-allocated [#28807] */
243         BPY_update_rna_module();
244
245         /* init global dictionary for py-driver evaluation settings */
246         if (!bpy_pydriver_Dict) {
247                 if (bpy_pydriver_create_dict() != 0) {
248                         fprintf(stderr, "PyDriver error: couldn't create Python dictionary\n");
249                         if (use_gil)
250                                 PyGILState_Release(gilstate);
251                         return 0.0f;
252                 }
253         }
254
255         /* update global namespace */
256         bpy_pydriver_namespace_update_frame(evaltime);
257
258         if (driver_orig->flag & DRIVER_FLAG_USE_SELF) {
259                 bpy_pydriver_namespace_update_self(anim_rna);
260         }
261         else {
262                 bpy_pydriver_namespace_clear_self();
263         }
264
265         if (driver_orig->expr_comp == NULL)
266                 driver_orig->flag |= DRIVER_FLAG_RECOMPILE;
267
268         /* compile the expression first if it hasn't been compiled or needs to be rebuilt */
269         if (driver_orig->flag & DRIVER_FLAG_RECOMPILE) {
270                 Py_XDECREF(driver_orig->expr_comp);
271                 driver_orig->expr_comp = PyTuple_New(2);
272
273                 expr_code = Py_CompileString(expr, "<bpy driver>", Py_eval_input);
274                 PyTuple_SET_ITEM(((PyObject *)driver_orig->expr_comp), 0, expr_code);
275
276                 driver_orig->flag &= ~DRIVER_FLAG_RECOMPILE;
277                 driver_orig->flag |= DRIVER_FLAG_RENAMEVAR; /* maybe this can be removed but for now best keep until were sure */
278         }
279         else {
280                 expr_code = PyTuple_GET_ITEM(((PyObject *)driver_orig->expr_comp), 0);
281         }
282
283         if (driver_orig->flag & DRIVER_FLAG_RENAMEVAR) {
284                 /* may not be set */
285                 expr_vars = PyTuple_GET_ITEM(((PyObject *)driver_orig->expr_comp), 1);
286                 Py_XDECREF(expr_vars);
287
288                 expr_vars = PyTuple_New(BLI_listbase_count(&driver_orig->variables));
289                 PyTuple_SET_ITEM(((PyObject *)driver_orig->expr_comp), 1, expr_vars);
290
291                 for (dvar = driver_orig->variables.first, i = 0; dvar; dvar = dvar->next) {
292                         PyTuple_SET_ITEM(expr_vars, i++, PyUnicode_FromString(dvar->name));
293                 }
294
295                 driver_orig->flag &= ~DRIVER_FLAG_RENAMEVAR;
296         }
297         else {
298                 expr_vars = PyTuple_GET_ITEM(((PyObject *)driver_orig->expr_comp), 1);
299         }
300
301         /* add target values to a dict that will be used as '__locals__' dict */
302         driver_vars = _PyDict_NewPresized(PyTuple_GET_SIZE(expr_vars));
303         for (dvar = driver->variables.first, i = 0; dvar; dvar = dvar->next) {
304                 PyObject *driver_arg = NULL;
305
306         /* support for any RNA data */
307 #ifdef USE_RNA_AS_PYOBJECT
308                 if (dvar->type == DVAR_TYPE_SINGLE_PROP) {
309                         driver_arg = pyrna_driver_get_variable_value(driver, &dvar->targets[0]);
310
311                         if (driver_arg == NULL) {
312                                 driver_arg = PyFloat_FromDouble(0.0);
313                                 dvar->curval = 0.0f;
314                         }
315                         else {
316                                 /* no need to worry about overflow here, values from RNA are within limits. */
317                                 if (PyFloat_CheckExact(driver_arg)) {
318                                         dvar->curval = (float)PyFloat_AsDouble(driver_arg);
319                                 }
320                                 else if (PyLong_CheckExact(driver_arg)) {
321                                         dvar->curval = (float)PyLong_AsLong(driver_arg);
322                                 }
323                                 else if (PyBool_Check(driver_arg)) {
324                                         dvar->curval = (driver_arg == Py_True);
325                                 }
326                                 else {
327                                         dvar->curval = 0.0f;
328                                 }
329                         }
330                 }
331                 else
332 #endif
333                 {
334                         /* try to get variable value */
335                         float tval = driver_get_variable_value(driver, dvar);
336                         driver_arg = PyFloat_FromDouble((double)tval);
337                 }
338
339                 /* try to add to dictionary */
340                 /* if (PyDict_SetItemString(driver_vars, dvar->name, driver_arg)) { */
341                 if (PyDict_SetItem(driver_vars, PyTuple_GET_ITEM(expr_vars, i++), driver_arg) != -1) {
342                         Py_DECREF(driver_arg);
343                 }
344                 else {
345                         /* this target failed - bad name */
346                         if (targets_ok) {
347                                 /* first one - print some extra info for easier identification */
348                                 fprintf(stderr, "\nBPY_driver_eval() - Error while evaluating PyDriver:\n");
349                                 targets_ok = 0;
350                         }
351
352                         fprintf(stderr, "\tBPY_driver_eval() - couldn't add variable '%s' to namespace\n", dvar->name);
353                         // BPy_errors_to_report(NULL); // TODO - reports
354                         PyErr_Print();
355                         PyErr_Clear();
356                 }
357         }
358
359
360 #if 0  /* slow, with this can avoid all Py_CompileString above. */
361         /* execute expression to get a value */
362         retval = PyRun_String(expr, Py_eval_input, bpy_pydriver_Dict, driver_vars);
363 #else
364         /* evaluate the compiled expression */
365         if (expr_code)
366                 retval = PyEval_EvalCode((void *)expr_code, bpy_pydriver_Dict, driver_vars);
367 #endif
368
369         /* decref the driver vars first...  */
370         Py_DECREF(driver_vars);
371
372         /* process the result */
373         if (retval == NULL) {
374                 pydriver_error(driver);
375         }
376         else if ((result = PyFloat_AsDouble(retval)) == -1.0 && PyErr_Occurred()) {
377                 pydriver_error(driver);
378                 Py_DECREF(retval);
379                 result = 0.0;
380         }
381         else {
382                 /* all fine, make sure the "invalid expression" flag is cleared */
383                 driver->flag &= ~DRIVER_FLAG_INVALID;
384                 Py_DECREF(retval);
385         }
386
387         if (use_gil)
388                 PyGILState_Release(gilstate);
389
390         if (isfinite(result)) {
391                 return (float)result;
392         }
393         else {
394                 fprintf(stderr, "\tBPY_driver_eval() - driver '%s' evaluates to '%f'\n", dvar->name, result);
395                 return 0.0f;
396         }
397 }