54
66
bpy_pydriver_Dict = d;
56
/* import some modules: builtins, bpy, math, (Blender.noise )*/
68
/* import some modules: builtins, bpy, math, (Blender.noise)*/
57
69
PyDict_SetItemString(d, "__builtins__", PyEval_GetBuiltins());
59
71
mod = PyImport_ImportModule("math");
61
PyDict_Merge(d, PyModule_GetDict(mod), 0); /* 0 - dont overwrite existing values */
73
PyDict_Merge(d, PyModule_GetDict(mod), 0); /* 0 - don't overwrite existing values */
65
77
/* add bpy to global namespace */
66
mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
78
mod = PyImport_ImportModuleLevel((char *)"bpy", NULL, NULL, NULL, 0);
68
80
PyDict_SetItemString(bpy_pydriver_Dict, "bpy", mod);
73
#if 0 // non existant yet
74
mod = PyImport_ImportModule("Blender.Noise");
84
/* add noise to global namespace */
85
mod = PyImport_ImportModuleLevel((char *)"mathutils", NULL, NULL, NULL, 0);
76
PyDict_SetItemString(d, "noise", mod);
77
PyDict_SetItemString(d, "n", mod);
87
PyObject *modsub = PyDict_GetItemString(PyModule_GetDict(mod), "noise");
88
PyDict_SetItemString(bpy_pydriver_Dict, "noise", modsub);
83
/* If there's a Blender text called pydrivers.py, import it.
84
* Users can add their own functions to this module.
86
if (G.f & G_SCRIPT_AUTOEXEC) {
87
mod = importText("pydrivers"); /* can also use PyImport_Import() */
89
PyDict_SetItemString(d, "pydrivers", mod);
90
PyDict_SetItemString(d, "p", mod);
96
#endif // non existant yet
95
/* note, this function should do nothing most runs, only when changing frame */
96
static PyObject *bpy_pydriver_InternStr__frame = NULL;
97
/* not thread safe but neither is python */
98
static float bpy_pydriver_evaltime_prev = FLT_MAX;
100
static void bpy_pydriver_update_dict(const float evaltime)
102
if (bpy_pydriver_evaltime_prev != evaltime) {
104
/* currently only update the frame */
105
if (bpy_pydriver_InternStr__frame == NULL) {
106
bpy_pydriver_InternStr__frame = PyUnicode_FromString("frame");
109
PyDict_SetItem(bpy_pydriver_Dict,
110
bpy_pydriver_InternStr__frame,
111
PyFloat_FromDouble(evaltime));
113
bpy_pydriver_evaltime_prev = evaltime;
101
117
/* Update function, it gets rid of pydrivers global dictionary, forcing
102
* BPY_eval_driver to recreate it. This function is used to force
118
* BPY_driver_exec to recreate it. This function is used to force
103
119
* reloading the Blender text module "pydrivers.py", if available, so
104
120
* updates in it reach pydriver evaluation.
106
void BPY_pydriver_update(void)
122
void BPY_driver_reset(void)
108
124
PyGILState_STATE gilstate;
109
int use_gil= 1; // (PyThreadState_Get()==NULL);
125
int use_gil = 1; /* !PYC_INTERPRETER_ACTIVE; */
112
128
gilstate = PyGILState_Ensure();
114
130
if (bpy_pydriver_Dict) { /* free the global dict used by pydrivers */
117
133
bpy_pydriver_Dict = NULL;
136
if (bpy_pydriver_InternStr__frame) {
137
Py_DECREF(bpy_pydriver_InternStr__frame);
138
bpy_pydriver_InternStr__frame = NULL;
139
bpy_pydriver_evaltime_prev = FLT_MAX;
121
143
PyGILState_Release(gilstate);
126
148
/* error return function for BPY_eval_pydriver */
127
static float pydriver_error(ChannelDriver *driver)
149
static void pydriver_error(ChannelDriver *driver)
129
if (bpy_pydriver_Dict) { /* free the global dict used by pydrivers */
130
PyDict_Clear(bpy_pydriver_Dict);
131
Py_DECREF(bpy_pydriver_Dict);
132
bpy_pydriver_Dict = NULL;
135
151
driver->flag |= DRIVER_FLAG_INVALID; /* py expression failed */
136
152
fprintf(stderr, "\nError in Driver: The following Python expression failed:\n\t'%s'\n\n", driver->expression);
138
154
// BPy_errors_to_report(NULL); // TODO - reports
145
159
/* This evals py driver expressions, 'expr' is a Python expression that
146
160
* should evaluate to a float number, which is returned.
148
* note: PyGILState_Ensure() isnt always called because python can call the
149
* bake operator which intern starts a thread which calls scene update which
150
* does a driver update. to avoid a deadlock check PyThreadState_Get() if PyGILState_Ensure() is needed.
162
* (old)note: PyGILState_Ensure() isn't always called because python can call
163
* the bake operator which intern starts a thread which calls scene update
164
* which does a driver update. to avoid a deadlock check PYC_INTERPRETER_ACTIVE
165
* if PyGILState_Ensure() is needed - see [#27683]
167
* (new)note: checking if python is running is not threadsafe [#28114]
168
* now release the GIL on python operator execution instead, using
169
* PyEval_SaveThread() / PyEval_RestoreThread() so we don't lock up blender.
152
float BPY_eval_driver (ChannelDriver *driver)
171
float BPY_driver_exec(ChannelDriver *driver, const float evaltime)
154
PyObject *driver_vars=NULL;
155
PyObject *retval= NULL;
173
PyObject *driver_vars = NULL;
174
PyObject *retval = NULL;
156
175
PyObject *expr_vars; /* speed up by pre-hashing string & avoids re-converting unicode strings for every execution */
157
176
PyObject *expr_code;
158
177
PyGILState_STATE gilstate;
162
181
double result = 0.0; /* default return */
163
182
char *expr = NULL;
183
short targets_ok = 1;
167
186
/* get the py expression to be evaluated */
168
187
expr = driver->expression;
169
if ((expr == NULL) || (expr[0]=='\0'))
188
if ((expr == NULL) || (expr[0] == '\0'))
172
if(!(G.f & G_SCRIPT_AUTOEXEC)) {
191
if (!(G.f & G_SCRIPT_AUTOEXEC)) {
173
192
printf("skipping driver '%s', automatic scripts are disabled\n", driver->expression);
177
use_gil= 1; //(PyThreadState_Get()==NULL);
196
use_gil = 1; /* !PYC_INTERPRETER_ACTIVE; */
180
199
gilstate = PyGILState_Ensure();
201
/* needed since drivers are updated directly after undo where 'main' is
202
* re-allocated [#28807] */
203
BPY_update_rna_module();
182
205
/* init global dictionary for py-driver evaluation settings */
183
206
if (!bpy_pydriver_Dict) {
184
207
if (bpy_pydriver_create_dict() != 0) {
185
208
fprintf(stderr, "Pydriver error: couldn't create Python dictionary");
187
210
PyGILState_Release(gilstate);
192
if(driver->expr_comp==NULL)
215
/* update global namespace */
216
bpy_pydriver_update_dict(evaltime);
219
if (driver->expr_comp == NULL)
193
220
driver->flag |= DRIVER_FLAG_RECOMPILE;
195
222
/* compile the expression first if it hasn't been compiled or needs to be rebuilt */
196
if(driver->flag & DRIVER_FLAG_RECOMPILE) {
223
if (driver->flag & DRIVER_FLAG_RECOMPILE) {
197
224
Py_XDECREF(driver->expr_comp);
198
driver->expr_comp= PyTuple_New(2);
225
driver->expr_comp = PyTuple_New(2);
200
expr_code= Py_CompileString(expr, "<bpy driver>", Py_eval_input);
227
expr_code = Py_CompileString(expr, "<bpy driver>", Py_eval_input);
201
228
PyTuple_SET_ITEM(((PyObject *)driver->expr_comp), 0, expr_code);
203
230
driver->flag &= ~DRIVER_FLAG_RECOMPILE;
204
231
driver->flag |= DRIVER_FLAG_RENAMEVAR; /* maybe this can be removed but for now best keep until were sure */
207
expr_code= PyTuple_GET_ITEM(((PyObject *)driver->expr_comp), 0);
234
expr_code = PyTuple_GET_ITEM(((PyObject *)driver->expr_comp), 0);
210
if(driver->flag & DRIVER_FLAG_RENAMEVAR) {
237
if (driver->flag & DRIVER_FLAG_RENAMEVAR) {
211
238
/* may not be set */
212
expr_vars= PyTuple_GET_ITEM(((PyObject *)driver->expr_comp), 1);
239
expr_vars = PyTuple_GET_ITEM(((PyObject *)driver->expr_comp), 1);
213
240
Py_XDECREF(expr_vars);
215
/* intern the arg names so creating the namespace for every run is faster */
216
expr_vars= PyTuple_New(BLI_countlist(&driver->variables));
242
expr_vars = PyTuple_New(BLI_countlist(&driver->variables));
217
243
PyTuple_SET_ITEM(((PyObject *)driver->expr_comp), 1, expr_vars);
219
for (dvar= driver->variables.first, i=0; dvar; dvar= dvar->next) {
220
PyTuple_SET_ITEM(expr_vars, i++, PyUnicode_InternFromString(dvar->name));
245
for (dvar = driver->variables.first, i = 0; dvar; dvar = dvar->next) {
246
PyTuple_SET_ITEM(expr_vars, i++, PyUnicode_FromString(dvar->name));
223
249
driver->flag &= ~DRIVER_FLAG_RENAMEVAR;
226
expr_vars= PyTuple_GET_ITEM(((PyObject *)driver->expr_comp), 1);
252
expr_vars = PyTuple_GET_ITEM(((PyObject *)driver->expr_comp), 1);
229
255
/* add target values to a dict that will be used as '__locals__' dict */
230
256
driver_vars = PyDict_New(); // XXX do we need to decref this?
231
for (dvar= driver->variables.first, i=0; dvar; dvar= dvar->next) {
257
for (dvar = driver->variables.first, i = 0; dvar; dvar = dvar->next) {
232
258
PyObject *driver_arg = NULL;
233
259
float tval = 0.0f;
235
261
/* try to get variable value */
236
tval= driver_get_variable_value(driver, dvar);
237
driver_arg= PyFloat_FromDouble((double)tval);
262
tval = driver_get_variable_value(driver, dvar);
263
driver_arg = PyFloat_FromDouble((double)tval);
239
265
/* try to add to dictionary */
240
266
/* if (PyDict_SetItemString(driver_vars, dvar->name, driver_arg)) { */
241
if (PyDict_SetItem(driver_vars, PyTuple_GET_ITEM(expr_vars, i++), driver_arg)) { /* use string interning for faster namespace creation */
267
if (PyDict_SetItem(driver_vars, PyTuple_GET_ITEM(expr_vars, i++), driver_arg) < 0) {
242
268
/* this target failed - bad name */
243
269
if (targets_ok) {
244
270
/* first one - print some extra info for easier identification */
245
fprintf(stderr, "\nBPY_eval_driver() - Error while evaluating PyDriver:\n");
271
fprintf(stderr, "\nBPY_driver_eval() - Error while evaluating PyDriver:\n");
249
fprintf(stderr, "\tBPY_eval_driver() - couldn't add variable '%s' to namespace\n", dvar->name);
275
fprintf(stderr, "\tBPY_driver_eval() - couldn't add variable '%s' to namespace\n", dvar->name);
250
276
// BPy_errors_to_report(NULL); // TODO - reports
256
283
#if 0 // slow, with this can avoid all Py_CompileString above.
257
284
/* execute expression to get a value */
258
285
retval = PyRun_String(expr, Py_eval_input, bpy_pydriver_Dict, driver_vars);
260
287
/* evaluate the compiled expression */
262
retval= PyEval_EvalCode((PyCodeObject *)expr_code, bpy_pydriver_Dict, driver_vars);
289
retval = PyEval_EvalCode((void *)expr_code, bpy_pydriver_Dict, driver_vars);
265
292
/* decref the driver vars first... */