Blender V5.3
bpy_driver.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
12
13#include <Python.h>
14
15#include "DNA_anim_types.h"
16
17#include "BLI_listbase.hh"
18#include "BLI_string_utf8.hh"
19
20#include "BKE_animsys.hh"
21#include "BKE_fcurve_driver.h"
22#include "BKE_global.hh"
23#include "BKE_idtype.hh"
24
25#include "RNA_access.hh"
26#include "RNA_prototypes.hh"
27
28#include "bpy_rna_driver.hh" /* For #pyrna_driver_get_variable_value. */
29
30#include "bpy_intern_string.hh"
31
32#include "bpy_driver.hh"
34#include "bpy_rna.hh"
35
36#include "BPY_extern.hh"
37
38namespace blender {
39
40#define USE_RNA_AS_PYOBJECT
41
42PyObject *bpy_pydriver_Dict = nullptr;
43
44#ifdef USE_BYTECODE_SECURE
45static PyObject *bpy_pydriver_Dict__secure_ids = nullptr;
46#endif
47
49{
50 PyObject *d, *mod;
51
52 /* Validate name-space for driver evaluation. */
54 return -1;
55 }
56
57 d = PyDict_New();
58 if (d == nullptr) {
59 return -1;
60 }
61
63
64 /* Import some modules: `builtins`, `bpy`, `math`, `mathutils.noise`. */
65 PyDict_SetItemString(d, "__builtins__", PyEval_GetBuiltins());
66
67 mod = PyImport_ImportModule("math");
68 if (mod) {
69 PyDict_Merge(d, PyModule_GetDict(mod), 0); /* 0 - don't overwrite existing values */
70 Py_DECREF(mod);
71 }
72#ifdef USE_BYTECODE_SECURE
73 PyObject *mod_math = mod;
74#endif
75
76 /* Add `bpy` to global name-space. */
77 mod = PyImport_ImportModuleLevel("bpy", nullptr, nullptr, nullptr, 0);
78 if (mod) {
79 PyDict_SetItemString(bpy_pydriver_Dict, "bpy", mod);
80 Py_DECREF(mod);
81 }
82
83 /* Add noise to global name-space. */
84 mod = PyImport_ImportModuleLevel("mathutils", nullptr, nullptr, nullptr, 0);
85 if (mod) {
86 PyObject *modsub = PyDict_GetItemString(PyModule_GetDict(mod), "noise");
87 PyDict_SetItemString(bpy_pydriver_Dict, "noise", modsub);
88 Py_DECREF(mod);
89 }
90
91 /* Add math utility functions. */
92 mod = PyImport_ImportModuleLevel("bl_math", nullptr, nullptr, nullptr, 0);
93 if (mod) {
94 static const char *names[] = {"clamp", "lerp", "smoothstep", nullptr};
95
96 for (const char **pname = names; *pname; ++pname) {
97 PyObject *func = PyDict_GetItemString(PyModule_GetDict(mod), *pname);
98 PyDict_SetItemString(bpy_pydriver_Dict, *pname, func);
99 }
100
101 Py_DECREF(mod);
102 }
103
104#ifdef USE_BYTECODE_SECURE
105 /* Setup the secure_ids. */
106 {
107 bpy_pydriver_Dict__secure_ids = PyDict_New();
108 const char *secure_ids[] = {
109 /* builtins (basic) */
110 "all",
111 "any",
112 "len",
113 /* builtins (numeric) */
114 "max",
115 "min",
116 "pow",
117 "round",
118 "sum",
119 /* types */
120 "bool",
121 "float",
122 "int",
123 /* bl_math */
124 "clamp",
125 "lerp",
126 "smoothstep",
127
128 nullptr,
129 };
130
131 for (int i = 0; secure_ids[i]; i++) {
132 PyDict_SetItemString(bpy_pydriver_Dict__secure_ids, secure_ids[i], Py_None);
133 }
134
135 /* Add all of `math` functions. */
136 if (mod_math != nullptr) {
137 PyObject *mod_math_dict = PyModule_GetDict(mod_math);
138 PyObject *arg_key, *arg_value;
139 Py_ssize_t arg_pos = 0;
140 while (PyDict_Next(mod_math_dict, &arg_pos, &arg_key, &arg_value)) {
141 const char *arg_str = PyUnicode_AsUTF8(arg_key);
142 if (arg_str[0] && arg_str[1] != '_') {
143 PyDict_SetItem(bpy_pydriver_Dict__secure_ids, arg_key, Py_None);
144 }
145 }
146 }
147 }
148#endif /* USE_BYTECODE_SECURE */
149
150 return 0;
151}
152
157static struct {
158 float evaltime = FLT_MAX;
159
160 /* Borrowed reference to the `self` in `bpy_pydriver_Dict`
161 * keep for as long as the same self is used. */
162 PyObject *self = nullptr;
163 BPy_StructRNA *depsgraph = nullptr;
165
166static void bpy_pydriver_namespace_update_frame(const float evaltime)
167{
168 if (g_pydriver_state_prev.evaltime != evaltime) {
169 PyObject *item = PyFloat_FromDouble(evaltime);
170 PyDict_SetItem(bpy_pydriver_Dict, bpy_intern_str_frame, item);
171 Py_DECREF(item);
172
173 g_pydriver_state_prev.evaltime = evaltime;
174 }
175}
176
178{
179 if ((g_pydriver_state_prev.self == nullptr) ||
180 (pyrna_driver_is_equal_anim_rna(anim_rna, g_pydriver_state_prev.self) == false))
181 {
182 PyObject *item = pyrna_driver_self_from_anim_rna(anim_rna);
183 PyDict_SetItem(bpy_pydriver_Dict, bpy_intern_str_self, item);
184 Py_DECREF(item);
185
186 g_pydriver_state_prev.self = item;
187 }
188}
189
191{
192 if (g_pydriver_state_prev.self) {
193 PyDict_DelItem(bpy_pydriver_Dict, bpy_intern_str_self);
194
195 g_pydriver_state_prev.self = nullptr;
196 }
197}
198
199static PyObject *bpy_pydriver_depsgraph_as_pyobject(Depsgraph *depsgraph)
200{
201 PointerRNA depsgraph_ptr = RNA_pointer_create_discrete(nullptr, RNA_Depsgraph, depsgraph);
202 return pyrna_struct_CreatePyObject(&depsgraph_ptr);
203}
204
209static void bpy_pydriver_namespace_update_depsgraph(Depsgraph *depsgraph)
210{
211 /* This should never happen, but it's probably better to have None in Python
212 * than a nullptr-wrapping Depsgraph Python struct. */
213 BLI_assert(depsgraph != nullptr);
214 if (depsgraph == nullptr) [[unlikely]] {
215 PyDict_SetItem(bpy_pydriver_Dict, bpy_intern_str_depsgraph, Py_None);
216 g_pydriver_state_prev.depsgraph = nullptr;
217 return;
218 }
219
220 if ((g_pydriver_state_prev.depsgraph == nullptr) ||
221 (depsgraph != g_pydriver_state_prev.depsgraph->ptr->data))
222 {
223 PyObject *item = bpy_pydriver_depsgraph_as_pyobject(depsgraph);
224 PyDict_SetItem(bpy_pydriver_Dict, bpy_intern_str_depsgraph, item);
225 Py_DECREF(item);
226
227 g_pydriver_state_prev.depsgraph = reinterpret_cast<BPy_StructRNA *>(item);
228 }
229}
230
232{
233 if (bpy_pydriver_Dict) { /* Free the global dict used by python-drivers. */
234 PyDict_Clear(bpy_pydriver_Dict);
235 Py_DECREF(bpy_pydriver_Dict);
236 bpy_pydriver_Dict = nullptr;
237 }
238
239#ifdef USE_BYTECODE_SECURE
241 PyDict_Clear(bpy_pydriver_Dict__secure_ids);
244 }
245#endif
246
248
249 /* Freed when clearing driver dictionary. */
250 g_pydriver_state_prev.self = nullptr;
251 g_pydriver_state_prev.depsgraph = nullptr;
252}
253
255{
256 PyGILState_STATE gilstate;
257 const bool use_gil = true; /* !PyC_IsInterpreterActive(); */
258 if (use_gil) {
259 gilstate = PyGILState_Ensure();
260 }
261
262 /* Currently exit/reset are practically the same besides the GIL check. */
264
265 if (use_gil) {
266 PyGILState_Release(gilstate);
267 }
268}
269
275static void pydriver_error(ChannelDriver *driver, const PathResolvedRNA *anim_rna)
276{
277 driver->flag |= DRIVER_FLAG_INVALID; /* Python expression failed. */
278
279 const char *null_str = "<null>";
280 const ID *id = anim_rna->ptr.owner_id;
281 fprintf(stderr,
282 "\n"
283 "Error in PyDriver: expression failed: %s\n"
284 "For target: (type=%s, name=\"%s\", property=%s, property_index=%d)\n"
285 "\n",
286 driver->expression,
287 id ? BKE_idtype_idcode_to_name(GS(id->name)) : null_str,
288 id ? id->name + 2 : null_str,
289 anim_rna->prop ? RNA_property_identifier(anim_rna->prop) : null_str,
290 anim_rna->prop_index);
291
292 // BPy_errors_to_report(nullptr); /* TODO: reports. */
293 PyErr_Print();
294}
295
296bool BPY_driver_secure_bytecode_test(PyObject *expr_code,
297 PyObject *py_namespace,
298 const bool verbose)
299{
300
301 if (!bpy_pydriver_Dict) {
302 if (bpy_pydriver_create_dict() != 0) {
303 fprintf(stderr, "%s: couldn't create Python dictionary\n", __func__);
304 return false;
305 }
306 }
307#ifdef USE_BYTECODE_SECURE
308 PyObject *py_namespaces[] = {
311 py_namespace,
312 nullptr,
313 };
314 return BPY_driver_secure_bytecode_test_ex(expr_code, py_namespaces, verbose, __func__);
315#else
316 UNUSED_VARS(expr_code, py_namespace, verbose);
317 return false;
318#endif
319}
320
322 ChannelDriver *driver,
323 ChannelDriver *driver_orig,
324 const AnimationEvalContext *anim_eval_context)
325{
326 /* (old) NOTE: PyGILState_Ensure() isn't always called because python can call
327 * the bake operator which intern starts a thread which calls scene update
328 * which does a driver update. to avoid a deadlock check #PyC_IsInterpreterActive()
329 * if #PyGILState_Ensure() is needed, see #27683.
330 *
331 * (new) NOTE: checking if python is running is not thread-safe #28114
332 * now release the GIL on python operator execution instead, using
333 * #PyEval_SaveThread() / #PyEval_RestoreThread() so we don't lock up blender.
334 *
335 * For copy-on-evaluation we always cache expressions and write errors in the
336 * original driver, otherwise these would get freed while editing.
337 * Due to the GIL this is thread-safe. */
338
339 PyObject *driver_vars = nullptr;
340 PyObject *retval = nullptr;
341
342 /* Speed up by pre-hashing string & avoids re-converting unicode strings for every execution. */
343 PyObject *expr_vars;
344
345 PyObject *expr_code;
346 PyGILState_STATE gilstate;
347 bool use_gil;
348
349 DriverVar *dvar;
350 double result = 0.0; /* Default return. */
351 const char *expr;
352 bool targets_ok = true;
353 int i;
354
355 /* Get the python expression to be evaluated. */
356 expr = driver_orig->expression;
357 if (expr[0] == '\0') {
358 return 0.0f;
359 }
360
361#ifndef USE_BYTECODE_SECURE
362 if (!(G.f & G_FLAG_SCRIPT_AUTOEXEC)) {
365 SNPRINTF_UTF8(G.autoexec_fail, "Driver '%s'", expr);
366
367 printf("skipping driver '%s', automatic scripts are disabled\n", expr);
368 }
369 driver_orig->flag |= DRIVER_FLAG_PYTHON_BLOCKED;
370 return 0.0f;
371 }
372#else
373 bool is_recompile = false;
374#endif
375
376 use_gil = true; /* !PyC_IsInterpreterActive(); */
377
378 if (use_gil) {
379 gilstate = PyGILState_Ensure();
380 }
381
382 /* Needed since drivers are updated directly after undo where `main` is re-allocated #28807. */
384
385 /* Initialize global dictionary for Python driver evaluation settings. */
386 if (!bpy_pydriver_Dict) {
387 if (bpy_pydriver_create_dict() != 0) {
388 fprintf(stderr, "%s: couldn't create Python dictionary\n", __func__);
389 if (use_gil) {
390 PyGILState_Release(gilstate);
391 }
392 return 0.0f;
393 }
394 }
395
396 /* Update global name-space. */
398
399 if (driver_orig->flag & DRIVER_FLAG_USE_SELF) {
401 }
402 else {
404 }
405
407
408 if (driver_orig->expr_comp == nullptr) {
409 driver_orig->flag |= DRIVER_FLAG_RECOMPILE;
410 }
411
412 /* Compile the expression first if it hasn't been compiled or needs to be rebuilt. */
413 if (driver_orig->flag & DRIVER_FLAG_RECOMPILE) {
414 Py_XDECREF(driver_orig->expr_comp);
415 driver_orig->expr_comp = PyTuple_New(2);
416
417 expr_code = Py_CompileString(expr, "<bpy driver>", Py_eval_input);
418 PyTuple_SET_ITEM(((PyObject *)driver_orig->expr_comp), 0, expr_code);
419
420 driver_orig->flag &= ~DRIVER_FLAG_RECOMPILE;
421
422 /* Maybe this can be removed but for now best keep until were sure. */
423 driver_orig->flag |= DRIVER_FLAG_RENAMEVAR;
424 driver_orig->flag &= ~DRIVER_FLAG_PYTHON_BLOCKED;
425
426#ifdef USE_BYTECODE_SECURE
427 is_recompile = true;
428#endif
429 }
430 else {
431 expr_code = PyTuple_GET_ITEM(((PyObject *)driver_orig->expr_comp), 0);
432 }
433
434 if (driver_orig->flag & DRIVER_FLAG_RENAMEVAR) {
435 /* May not be set. */
436 PyObject *expr_vars_prev = PyTuple_GET_ITEM(((PyObject *)driver_orig->expr_comp), 1);
437
438 expr_vars = PyTuple_New(driver_orig->variables.count());
439 PyTuple_SET_ITEM(((PyObject *)driver_orig->expr_comp), 1, expr_vars);
440 Py_XDECREF(expr_vars_prev);
441
442 for (dvar = driver_orig->variables.first(), i = 0; dvar; dvar = dvar->next) {
443 PyTuple_SET_ITEM(expr_vars, i++, PyUnicode_FromString(dvar->name));
444 }
445
446 driver_orig->flag &= ~DRIVER_FLAG_RENAMEVAR;
447 }
448 else {
449 expr_vars = PyTuple_GET_ITEM(((PyObject *)driver_orig->expr_comp), 1);
450 }
451
452 /* Add target values to a dict that will be used as `__locals__` dict. */
453 driver_vars = _PyDict_NewPresized(PyTuple_GET_SIZE(expr_vars));
454 for (dvar = driver->variables.first(), i = 0; dvar; dvar = dvar->next) {
455 PyObject *driver_arg = nullptr;
456
457/* Support for any RNA data. */
458#ifdef USE_RNA_AS_PYOBJECT
459 if (dvar->type == DVAR_TYPE_SINGLE_PROP) {
461 anim_eval_context, driver, dvar, &dvar->targets[0]);
462
463 if (driver_arg == nullptr) {
464 driver_arg = PyFloat_FromDouble(0.0);
465 dvar->curval = 0.0f;
466 }
467 else {
468 /* No need to worry about overflow here, values from RNA are within limits. */
469 if (PyFloat_CheckExact(driver_arg)) {
470 dvar->curval = float(PyFloat_AsDouble(driver_arg));
471 }
472 else if (PyLong_CheckExact(driver_arg)) {
473 dvar->curval = float(PyLong_AsLong(driver_arg));
474 }
475 else if (PyBool_Check(driver_arg)) {
476 dvar->curval = float(driver_arg == Py_True);
477 }
478 else {
479 dvar->curval = 0.0f;
480 }
481 }
482 }
483 else
484#endif
485 {
486 /* Try to get variable value. */
487 const float tval = driver_get_variable_value(anim_eval_context, driver, dvar);
488 driver_arg = PyFloat_FromDouble(double(tval));
489 }
490
491 /* Try to add to dictionary. */
492 /* `if (PyDict_SetItemString(driver_vars, dvar->name, driver_arg)) {` */
493 if (PyDict_SetItem(driver_vars, PyTuple_GET_ITEM(expr_vars, i++), driver_arg) != -1) {
494 /* Pass. */
495 }
496 else {
497 /* This target failed - bad name. */
498 if (targets_ok) {
499 /* First one, print some extra info for easier identification. */
500 fprintf(stderr, "\n%s: Error while evaluating PyDriver:\n", __func__);
501 targets_ok = false;
502 }
503
504 fprintf(stderr, "\t%s: couldn't add variable '%s' to namespace\n", __func__, dvar->name);
505 // BPy_errors_to_report(nullptr); /* TODO: reports. */
506 PyErr_Print();
507 }
508 Py_DECREF(driver_arg);
509 }
510
511#ifdef USE_BYTECODE_SECURE
512 if (is_recompile && expr_code) {
513 if (!(G.f & G_FLAG_SCRIPT_AUTOEXEC)) {
514 PyObject *py_namespaces[] = {
515 bpy_pydriver_Dict, bpy_pydriver_Dict__secure_ids, driver_vars, nullptr};
517 expr_code,
518 py_namespaces,
519 /* Always be verbose since this can give hints to why evaluation fails. */
520 true,
521 __func__))
522 {
525 SNPRINTF_UTF8(G.autoexec_fail, "Driver '%s'", expr);
526 }
527
528 PyTuple_SET_ITEM(((PyObject *)driver_orig->expr_comp), 0, nullptr);
529 Py_DECREF(expr_code);
530 expr_code = nullptr;
531 driver_orig->flag |= DRIVER_FLAG_PYTHON_BLOCKED;
532 }
533 }
534 }
535#endif /* USE_BYTECODE_SECURE */
536
537#if 0 /* slow, with this can avoid all Py_CompileString above. */
538 /* execute expression to get a value */
539 retval = PyRun_String(expr, Py_eval_input, bpy_pydriver_Dict, driver_vars);
540#else
541 /* Evaluate the compiled expression. */
542 if (expr_code) {
543 retval = PyEval_EvalCode(
544 static_cast<PyObject *>(static_cast<void *>(expr_code)), bpy_pydriver_Dict, driver_vars);
545 }
546#endif
547
548 /* Decref the driver variables first. */
549 Py_DECREF(driver_vars);
550
551 /* Process the result. */
552 if (retval == nullptr) {
553 pydriver_error(driver, anim_rna);
554 }
555 else {
556 if ((result = PyFloat_AsDouble(retval)) == -1.0 && PyErr_Occurred()) [[unlikely]] {
557 pydriver_error(driver, anim_rna);
558 result = 0.0;
559 }
560 else {
561 /* All fine, make sure the "invalid expression" flag is cleared. */
562 driver->flag &= ~DRIVER_FLAG_INVALID;
563 }
564 Py_DECREF(retval);
565 }
566
567 if (use_gil) {
568 PyGILState_Release(gilstate);
569 }
570
571 if (!isfinite(result)) [[unlikely]] {
572 fprintf(stderr, "\t%s: driver '%s' evaluates to '%f'\n", __func__, driver->expression, result);
573 return 0.0f;
574 }
575
576 return float(result);
577}
578
579} // namespace blender
#define SNPRINTF_UTF8(dst, format,...)
#define UNUSED_VARS(...)
#define FLT_MAX
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
BMesh Iterators.
const char * BKE_idtype_idcode_to_name(short idcode)
Definition idtype.cc:261
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ DVAR_TYPE_SINGLE_PROP
@ DRIVER_FLAG_PYTHON_BLOCKED
@ DRIVER_FLAG_RENAMEVAR
@ DRIVER_FLAG_RECOMPILE
@ DRIVER_FLAG_INVALID
@ DRIVER_FLAG_USE_SELF
static void bpy_pydriver_namespace_clear_self()
float BPY_driver_exec(PathResolvedRNA *anim_rna, ChannelDriver *driver, ChannelDriver *driver_orig, const AnimationEvalContext *anim_eval_context)
static struct blender::@036126075304071072024120154372327152374236036151 g_pydriver_state_prev
const char * RNA_property_identifier(const PropertyRNA *prop)
int bpy_pydriver_create_dict()
Definition bpy_driver.cc:48
static void pydriver_error(ChannelDriver *driver, const PathResolvedRNA *anim_rna)
PyObject * pyrna_struct_CreatePyObject(PointerRNA *ptr)
Definition bpy_rna.cc:8874
PyObject * bpy_intern_str_depsgraph
Global G
Definition blender.cc:57
bool BPY_driver_secure_bytecode_test(PyObject *expr_code, PyObject *py_namespace, const bool verbose)
PyObject * bpy_intern_str_frame
bool BPY_driver_secure_bytecode_test_ex(PyObject *expr_code, PyObject *py_namespace_array[], const bool verbose, const char *error_prefix)
PyObject * bpy_pydriver_Dict
Definition bpy_driver.cc:42
PyObject * pyrna_driver_get_variable_value(const AnimationEvalContext *anim_eval_context, ChannelDriver *driver, DriverVar *dvar, DriverTarget *dtar)
void BPY_driver_reset()
static PyObject * bpy_pydriver_Dict__secure_ids
Definition bpy_driver.cc:45
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
static void bpy_pydriver_namespace_update_self(PathResolvedRNA *anim_rna)
bool pyrna_driver_is_equal_anim_rna(const PathResolvedRNA *anim_rna, const PyObject *py_anim_rna)
PyObject * bpy_intern_str_self
float driver_get_variable_value(const struct AnimationEvalContext *anim_eval_context, struct ChannelDriver *driver, struct DriverVar *dvar)
PyObject * pyrna_driver_self_from_anim_rna(PathResolvedRNA *anim_rna)
void BPY_driver_exit()
@ G_FLAG_SCRIPT_AUTOEXEC_FAIL_QUIET
@ G_FLAG_SCRIPT_AUTOEXEC_FAIL
@ G_FLAG_SCRIPT_AUTOEXEC
static void bpy_pydriver_namespace_update_depsgraph(Depsgraph *depsgraph)
static void bpy_pydriver_namespace_update_frame(const float evaltime)
ID_Type GS(const char *name)
Definition DNA_ID.h:410
static PyObject * bpy_pydriver_depsgraph_as_pyobject(Depsgraph *depsgraph)
void BPY_update_rna_module()
Definition bpy_rna.cc:9093
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ListBaseT< DriverVar > variables
struct DriverVar * next
eDriverVar_Types type
DriverTarget targets[8]