Cleanup: trailing space cmake, make.bat
[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 extern void BPY_update_rna_module(void);
52
53 #define USE_RNA_AS_PYOBJECT
54
55 #define USE_BYTECODE_WHITELIST
56
57 #ifdef USE_BYTECODE_WHITELIST
58 #  include <opcode.h>
59 #endif
60
61 /* for pydrivers (drivers using one-line Python expressions to express relationships between targets) */
62 PyObject *bpy_pydriver_Dict = NULL;
63
64 #ifdef USE_BYTECODE_WHITELIST
65 static PyObject *bpy_pydriver_Dict__whitelist = NULL;
66 #endif
67
68 /* For faster execution we keep a special dictionary for pydrivers, with
69  * the needed modules and aliases.
70  */
71 int bpy_pydriver_create_dict(void)
72 {
73         PyObject *d, *mod;
74
75         /* validate namespace for driver evaluation */
76         if (bpy_pydriver_Dict) return -1;
77
78         d = PyDict_New();
79         if (d == NULL)
80                 return -1;
81         else
82                 bpy_pydriver_Dict = d;
83
84         /* import some modules: builtins, bpy, math, (Blender.noise)*/
85         PyDict_SetItemString(d, "__builtins__", PyEval_GetBuiltins());
86
87         mod = PyImport_ImportModule("math");
88         if (mod) {
89                 PyDict_Merge(d, PyModule_GetDict(mod), 0); /* 0 - don't overwrite existing values */
90                 Py_DECREF(mod);
91         }
92 #ifdef USE_BYTECODE_WHITELIST
93         PyObject *mod_math = mod;
94 #endif
95
96         /* add bpy to global namespace */
97         mod = PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
98         if (mod) {
99                 PyDict_SetItemString(bpy_pydriver_Dict, "bpy", mod);
100                 Py_DECREF(mod);
101         }
102
103         /* add noise to global namespace */
104         mod = PyImport_ImportModuleLevel("mathutils", NULL, NULL, NULL, 0);
105         if (mod) {
106                 PyObject *modsub = PyDict_GetItemString(PyModule_GetDict(mod), "noise");
107                 PyDict_SetItemString(bpy_pydriver_Dict, "noise", modsub);
108                 Py_DECREF(mod);
109         }
110
111 #ifdef USE_BYTECODE_WHITELIST
112         /* setup the whitelist */
113         {
114                 bpy_pydriver_Dict__whitelist = PyDict_New();
115                 const char *whitelist[] = {
116                         /* builtins (basic) */
117                         "all",
118                         "any",
119                         "len",
120                         /* builtins (numeric) */
121                         "max",
122                         "min",
123                         "pow",
124                         "round",
125                         "sum",
126                         /* types */
127                         "bool",
128                         "float",
129                         "int",
130
131                         NULL,
132                 };
133
134                 for (int i = 0; whitelist[i]; i++) {
135                         PyDict_SetItemString(bpy_pydriver_Dict__whitelist, whitelist[i], Py_None);
136                 }
137
138                 /* Add all of 'math' functions. */
139                 if (mod_math != NULL) {
140                         PyObject *mod_math_dict = PyModule_GetDict(mod_math);
141                         PyObject *arg_key, *arg_value;
142                         Py_ssize_t arg_pos = 0;
143                         while (PyDict_Next(mod_math_dict, &arg_pos, &arg_key, &arg_value)) {
144                                 const char *arg_str = _PyUnicode_AsString(arg_key);
145                                 if (arg_str[0] && arg_str[1] != '_') {
146                                         PyDict_SetItem(bpy_pydriver_Dict__whitelist, arg_key, Py_None);
147                                 }
148                         }
149                 }
150         }
151 #endif  /* USE_BYTECODE_WHITELIST */
152
153         return 0;
154 }
155
156 /* note, this function should do nothing most runs, only when changing frame */
157 /* not thread safe but neither is python */
158 static struct {
159         float evaltime;
160
161         /* borrowed reference to the 'self' in 'bpy_pydriver_Dict'
162          * keep for as long as the same self is used. */
163         PyObject *self;
164 } g_pydriver_state_prev = {
165         .evaltime = FLT_MAX,
166         .self = NULL,
167 };
168
169 static void bpy_pydriver_namespace_update_frame(const float evaltime)
170 {
171         if (g_pydriver_state_prev.evaltime != evaltime) {
172                 PyObject *item = PyFloat_FromDouble(evaltime);
173                 PyDict_SetItem(bpy_pydriver_Dict, bpy_intern_str_frame, item);
174                 Py_DECREF(item);
175
176                 g_pydriver_state_prev.evaltime = evaltime;
177         }
178 }
179
180 static void bpy_pydriver_namespace_update_self(struct PathResolvedRNA *anim_rna)
181 {
182         if ((g_pydriver_state_prev.self == NULL) ||
183             (pyrna_driver_is_equal_anim_rna(anim_rna, g_pydriver_state_prev.self) == false))
184         {
185                 PyObject *item = pyrna_driver_self_from_anim_rna(anim_rna);
186                 PyDict_SetItem(bpy_pydriver_Dict, bpy_intern_str_self, item);
187                 Py_DECREF(item);
188
189                 g_pydriver_state_prev.self = item;
190         }
191 }
192
193 static void bpy_pydriver_namespace_clear_self(void)
194 {
195         if (g_pydriver_state_prev.self) {
196                 PyDict_DelItem(bpy_pydriver_Dict, bpy_intern_str_self);
197
198                 g_pydriver_state_prev.self = NULL;
199         }
200 }
201
202 /* Update function, it gets rid of pydrivers global dictionary, forcing
203  * BPY_driver_exec to recreate it. This function is used to force
204  * reloading the Blender text module "pydrivers.py", if available, so
205  * updates in it reach pydriver evaluation.
206  */
207 void BPY_driver_reset(void)
208 {
209         PyGILState_STATE gilstate;
210         bool use_gil = true; /* !PyC_IsInterpreterActive(); */
211
212         if (use_gil)
213                 gilstate = PyGILState_Ensure();
214
215         if (bpy_pydriver_Dict) { /* free the global dict used by pydrivers */
216                 PyDict_Clear(bpy_pydriver_Dict);
217                 Py_DECREF(bpy_pydriver_Dict);
218                 bpy_pydriver_Dict = NULL;
219         }
220
221 #ifdef USE_BYTECODE_WHITELIST
222         if (bpy_pydriver_Dict__whitelist) {
223                 PyDict_Clear(bpy_pydriver_Dict__whitelist);
224                 Py_DECREF(bpy_pydriver_Dict__whitelist);
225                 bpy_pydriver_Dict__whitelist = NULL;
226         }
227 #endif
228
229         g_pydriver_state_prev.evaltime = FLT_MAX;
230
231         /* freed when clearing driver dict */
232         g_pydriver_state_prev.self = NULL;
233
234         if (use_gil)
235                 PyGILState_Release(gilstate);
236
237         return;
238 }
239
240 /* error return function for BPY_eval_pydriver */
241 static void pydriver_error(ChannelDriver *driver)
242 {
243         driver->flag |= DRIVER_FLAG_INVALID; /* py expression failed */
244         fprintf(stderr, "\nError in Driver: The following Python expression failed:\n\t'%s'\n\n", driver->expression);
245
246         // BPy_errors_to_report(NULL); // TODO - reports
247         PyErr_Print();
248         PyErr_Clear();
249 }
250
251 #ifdef USE_BYTECODE_WHITELIST
252
253 #define OK_OP(op) [op] = 1
254
255 const char secure_opcodes[255] = {
256         OK_OP(0),
257         OK_OP(POP_TOP),
258         OK_OP(ROT_TWO),
259         OK_OP(ROT_THREE),
260         OK_OP(DUP_TOP),
261         OK_OP(DUP_TOP_TWO),
262         OK_OP(NOP),
263         OK_OP(UNARY_POSITIVE),
264         OK_OP(UNARY_NEGATIVE),
265         OK_OP(UNARY_NOT),
266         OK_OP(UNARY_INVERT),
267         OK_OP(BINARY_MATRIX_MULTIPLY),
268         OK_OP(INPLACE_MATRIX_MULTIPLY),
269         OK_OP(BINARY_POWER),
270         OK_OP(BINARY_MULTIPLY),
271         OK_OP(BINARY_MODULO),
272         OK_OP(BINARY_ADD),
273         OK_OP(BINARY_SUBTRACT),
274         OK_OP(BINARY_SUBSCR),
275         OK_OP(BINARY_FLOOR_DIVIDE),
276         OK_OP(BINARY_TRUE_DIVIDE),
277         OK_OP(INPLACE_FLOOR_DIVIDE),
278         OK_OP(INPLACE_TRUE_DIVIDE),
279         OK_OP(INPLACE_ADD),
280         OK_OP(INPLACE_SUBTRACT),
281         OK_OP(INPLACE_MULTIPLY),
282         OK_OP(INPLACE_MODULO),
283         OK_OP(BINARY_LSHIFT),
284         OK_OP(BINARY_RSHIFT),
285         OK_OP(BINARY_AND),
286         OK_OP(BINARY_XOR),
287         OK_OP(BINARY_OR),
288         OK_OP(INPLACE_POWER),
289         OK_OP(INPLACE_LSHIFT),
290         OK_OP(INPLACE_RSHIFT),
291         OK_OP(INPLACE_AND),
292         OK_OP(INPLACE_XOR),
293         OK_OP(INPLACE_OR),
294         OK_OP(RETURN_VALUE),
295         OK_OP(BUILD_TUPLE),
296         OK_OP(BUILD_LIST),
297         OK_OP(BUILD_SET),
298         OK_OP(BUILD_MAP),
299         OK_OP(COMPARE_OP),
300         OK_OP(JUMP_FORWARD),
301         OK_OP(JUMP_IF_FALSE_OR_POP),
302         OK_OP(JUMP_IF_TRUE_OR_POP),
303         OK_OP(JUMP_ABSOLUTE),
304         OK_OP(POP_JUMP_IF_FALSE),
305         OK_OP(POP_JUMP_IF_TRUE),
306         OK_OP(LOAD_GLOBAL),
307         OK_OP(LOAD_FAST),
308         OK_OP(STORE_FAST),
309         OK_OP(DELETE_FAST),
310         OK_OP(LOAD_DEREF),
311         OK_OP(STORE_DEREF),
312
313         /* special cases */
314         OK_OP(LOAD_CONST),         /* ok because constants are accepted */
315         OK_OP(LOAD_NAME),          /* ok, because PyCodeObject.names is checked */
316         OK_OP(CALL_FUNCTION),      /* ok, because we check its 'name' before calling */
317         OK_OP(CALL_FUNCTION_KW),
318         OK_OP(CALL_FUNCTION_EX),
319 };
320
321 #undef OK_OP
322
323 static bool bpy_driver_secure_bytecode_validate(PyObject *expr_code, PyObject *dict_arr[])
324 {
325         PyCodeObject *py_code = (PyCodeObject *)expr_code;
326
327         /* Check names. */
328         {
329                 for (int i = 0; i < PyTuple_GET_SIZE(py_code->co_names); i++) {
330                         PyObject *name = PyTuple_GET_ITEM(py_code->co_names, i);
331
332                         bool contains_name = false;
333                         for (int j = 0; dict_arr[j]; j++) {
334                                 if (PyDict_Contains(dict_arr[j], name)) {
335                                         contains_name = true;
336                                         break;
337                                 }
338                         }
339
340                         if (contains_name == false) {
341                                 fprintf(stderr, "\tBPY_driver_eval() - restructed access disallows name '%s', "
342                                                 "enable auto-execution to support\n", _PyUnicode_AsString(name));
343                                 return false;
344                         }
345                 }
346         }
347
348         /* Check opcodes. */
349         {
350                 const char *codestr;
351                 Py_ssize_t  code_len;
352
353                 PyBytes_AsStringAndSize(py_code->co_code, (char **)&codestr, &code_len);
354
355 #define CODESIZE(op) (HAS_ARG(op) ? 3 : 1)
356
357                 for (Py_ssize_t i = 0; i < code_len; i += CODESIZE(codestr[i])) {
358                         const int opcode = codestr[i];
359                         if (secure_opcodes[opcode] == 0) {
360                                 fprintf(stderr, "\tBPY_driver_eval() - restructed access disallows opcode '%d', "
361                                                 "enable auto-execution to support\n", opcode);
362                                 return false;
363                         }
364                 }
365
366 #undef CODESIZE
367         }
368
369         return true;
370 }
371
372 #endif  /* USE_BYTECODE_WHITELIST */
373
374
375 /* This evals py driver expressions, 'expr' is a Python expression that
376  * should evaluate to a float number, which is returned.
377  *
378  * (old)note: PyGILState_Ensure() isn't always called because python can call
379  * the bake operator which intern starts a thread which calls scene update
380  * which does a driver update. to avoid a deadlock check PyC_IsInterpreterActive()
381  * if PyGILState_Ensure() is needed - see [#27683]
382  *
383  * (new)note: checking if python is running is not threadsafe [#28114]
384  * now release the GIL on python operator execution instead, using
385  * PyEval_SaveThread() / PyEval_RestoreThread() so we don't lock up blender.
386  */
387 float BPY_driver_exec(struct PathResolvedRNA *anim_rna, ChannelDriver *driver, const float evaltime)
388 {
389         PyObject *driver_vars = NULL;
390         PyObject *retval = NULL;
391         PyObject *expr_vars; /* speed up by pre-hashing string & avoids re-converting unicode strings for every execution */
392         PyObject *expr_code;
393         PyGILState_STATE gilstate;
394         bool use_gil;
395
396         DriverVar *dvar;
397         double result = 0.0; /* default return */
398         const char *expr;
399         short targets_ok = 1;
400         int i;
401
402         /* get the py expression to be evaluated */
403         expr = driver->expression;
404         if (expr[0] == '\0')
405                 return 0.0f;
406
407 #ifndef USE_BYTECODE_WHITELIST
408         if (!(G.f & G_SCRIPT_AUTOEXEC)) {
409                 if (!(G.f & G_SCRIPT_AUTOEXEC_FAIL_QUIET)) {
410                         G.f |= G_SCRIPT_AUTOEXEC_FAIL;
411                         BLI_snprintf(G.autoexec_fail, sizeof(G.autoexec_fail), "Driver '%s'", expr);
412
413                         printf("skipping driver '%s', automatic scripts are disabled\n", expr);
414                 }
415                 return 0.0f;
416         }
417 #else
418         bool is_recompile = false;
419 #endif
420
421         use_gil = true;  /* !PyC_IsInterpreterActive(); */
422
423         if (use_gil)
424                 gilstate = PyGILState_Ensure();
425
426         /* needed since drivers are updated directly after undo where 'main' is
427          * re-allocated [#28807] */
428         BPY_update_rna_module();
429
430         /* init global dictionary for py-driver evaluation settings */
431         if (!bpy_pydriver_Dict) {
432                 if (bpy_pydriver_create_dict() != 0) {
433                         fprintf(stderr, "PyDriver error: couldn't create Python dictionary\n");
434                         if (use_gil)
435                                 PyGILState_Release(gilstate);
436                         return 0.0f;
437                 }
438         }
439
440         /* update global namespace */
441         bpy_pydriver_namespace_update_frame(evaltime);
442
443         if (driver->flag & DRIVER_FLAG_USE_SELF) {
444                 bpy_pydriver_namespace_update_self(anim_rna);
445         }
446         else {
447                 bpy_pydriver_namespace_clear_self();
448         }
449
450         if (driver->expr_comp == NULL)
451                 driver->flag |= DRIVER_FLAG_RECOMPILE;
452
453         /* compile the expression first if it hasn't been compiled or needs to be rebuilt */
454         if (driver->flag & DRIVER_FLAG_RECOMPILE) {
455                 Py_XDECREF(driver->expr_comp);
456                 driver->expr_comp = PyTuple_New(2);
457
458                 expr_code = Py_CompileString(expr, "<bpy driver>", Py_eval_input);
459                 PyTuple_SET_ITEM(((PyObject *)driver->expr_comp), 0, expr_code);
460
461                 driver->flag &= ~DRIVER_FLAG_RECOMPILE;
462                 driver->flag |= DRIVER_FLAG_RENAMEVAR; /* maybe this can be removed but for now best keep until were sure */
463 #ifdef USE_BYTECODE_WHITELIST
464                 is_recompile = true;
465 #endif
466         }
467         else {
468                 expr_code = PyTuple_GET_ITEM(((PyObject *)driver->expr_comp), 0);
469         }
470
471         if (driver->flag & DRIVER_FLAG_RENAMEVAR) {
472                 /* may not be set */
473                 expr_vars = PyTuple_GET_ITEM(((PyObject *)driver->expr_comp), 1);
474                 Py_XDECREF(expr_vars);
475
476                 expr_vars = PyTuple_New(BLI_listbase_count(&driver->variables));
477                 PyTuple_SET_ITEM(((PyObject *)driver->expr_comp), 1, expr_vars);
478
479                 for (dvar = driver->variables.first, i = 0; dvar; dvar = dvar->next) {
480                         PyTuple_SET_ITEM(expr_vars, i++, PyUnicode_FromString(dvar->name));
481                 }
482
483                 driver->flag &= ~DRIVER_FLAG_RENAMEVAR;
484         }
485         else {
486                 expr_vars = PyTuple_GET_ITEM(((PyObject *)driver->expr_comp), 1);
487         }
488
489         /* add target values to a dict that will be used as '__locals__' dict */
490         driver_vars = _PyDict_NewPresized(PyTuple_GET_SIZE(expr_vars));
491         for (dvar = driver->variables.first, i = 0; dvar; dvar = dvar->next) {
492                 PyObject *driver_arg = NULL;
493
494         /* support for any RNA data */
495 #ifdef USE_RNA_AS_PYOBJECT
496                 if (dvar->type == DVAR_TYPE_SINGLE_PROP) {
497                         driver_arg = pyrna_driver_get_variable_value(driver, &dvar->targets[0]);
498
499                         if (driver_arg == NULL) {
500                                 driver_arg = PyFloat_FromDouble(0.0);
501                                 dvar->curval = 0.0f;
502                         }
503                         else {
504                                 /* no need to worry about overflow here, values from RNA are within limits. */
505                                 if (PyFloat_CheckExact(driver_arg)) {
506                                         dvar->curval = (float)PyFloat_AsDouble(driver_arg);
507                                 }
508                                 else if (PyLong_CheckExact(driver_arg)) {
509                                         dvar->curval = (float)PyLong_AsLong(driver_arg);
510                                 }
511                                 else if (PyBool_Check(driver_arg)) {
512                                         dvar->curval = (driver_arg == Py_True);
513                                 }
514                                 else {
515                                         dvar->curval = 0.0f;
516                                 }
517                         }
518                 }
519                 else
520 #endif
521                 {
522                         /* try to get variable value */
523                         float tval = driver_get_variable_value(driver, dvar);
524                         driver_arg = PyFloat_FromDouble((double)tval);
525                 }
526
527                 /* try to add to dictionary */
528                 /* if (PyDict_SetItemString(driver_vars, dvar->name, driver_arg)) { */
529                 if (PyDict_SetItem(driver_vars, PyTuple_GET_ITEM(expr_vars, i++), driver_arg) != -1) {
530                         Py_DECREF(driver_arg);
531                 }
532                 else {
533                         /* this target failed - bad name */
534                         if (targets_ok) {
535                                 /* first one - print some extra info for easier identification */
536                                 fprintf(stderr, "\nBPY_driver_eval() - Error while evaluating PyDriver:\n");
537                                 targets_ok = 0;
538                         }
539
540                         fprintf(stderr, "\tBPY_driver_eval() - couldn't add variable '%s' to namespace\n", dvar->name);
541                         // BPy_errors_to_report(NULL); // TODO - reports
542                         PyErr_Print();
543                         PyErr_Clear();
544                 }
545         }
546
547 #ifdef USE_BYTECODE_WHITELIST
548         if (is_recompile) {
549                 if (!(G.f & G_SCRIPT_AUTOEXEC)) {
550                         if (!bpy_driver_secure_bytecode_validate(
551                                     expr_code, (PyObject *[]){
552                                         bpy_pydriver_Dict,
553                                         bpy_pydriver_Dict__whitelist,
554                                         driver_vars,
555                                         NULL,}
556                                 ))
557                         {
558                                 Py_DECREF(expr_code);
559                                 expr_code = NULL;
560                                 PyTuple_SET_ITEM(((PyObject *)driver->expr_comp), 0, NULL);
561                         }
562                 }
563         }
564 #endif  /* USE_BYTECODE_WHITELIST */
565
566 #if 0  /* slow, with this can avoid all Py_CompileString above. */
567         /* execute expression to get a value */
568         retval = PyRun_String(expr, Py_eval_input, bpy_pydriver_Dict, driver_vars);
569 #else
570         /* evaluate the compiled expression */
571         if (expr_code)
572                 retval = PyEval_EvalCode((void *)expr_code, bpy_pydriver_Dict, driver_vars);
573 #endif
574
575         /* decref the driver vars first...  */
576         Py_DECREF(driver_vars);
577
578         /* process the result */
579         if (retval == NULL) {
580                 pydriver_error(driver);
581         }
582         else if ((result = PyFloat_AsDouble(retval)) == -1.0 && PyErr_Occurred()) {
583                 pydriver_error(driver);
584                 Py_DECREF(retval);
585                 result = 0.0;
586         }
587         else {
588                 /* all fine, make sure the "invalid expression" flag is cleared */
589                 driver->flag &= ~DRIVER_FLAG_INVALID;
590                 Py_DECREF(retval);
591         }
592
593         if (use_gil)
594                 PyGILState_Release(gilstate);
595
596         if (isfinite(result)) {
597                 return (float)result;
598         }
599         else {
600                 fprintf(stderr, "\tBPY_driver_eval() - driver '%s' evaluates to '%f'\n", dvar->name, result);
601                 return 0.0f;
602         }
603 }