Drivers: add lerp and clamp functions to namespace.

Implementation of lerp without a function requires repeating one of
the arguments, which is not ideal. To avoid that, add a new function
to the driver namespace. In addition, provide a function for clamping
between 0 and 1 to support easy clamped lerp, and a smoothstep function
from GLSL that is somewhat related.

The function implementations are added to a new bl_math module.
As an aside, add the round function and two-argument log to the
pylike expression subset.

Differential Revision: https://developer.blender.org/D8205
This commit is contained in:
2020-07-04 13:20:59 +03:00
parent 8369adabc0
commit f8cc01595d
7 changed files with 329 additions and 0 deletions

View File

@@ -114,6 +114,19 @@ int bpy_pydriver_create_dict(void)
Py_DECREF(mod);
}
/* Add math utility functions. */
mod = PyImport_ImportModuleLevel("bl_math", NULL, NULL, NULL, 0);
if (mod) {
static const char *names[] = {"clamp", "lerp", "smoothstep", NULL};
for (const char **pname = names; *pname; ++pname) {
PyObject *func = PyDict_GetItemString(PyModule_GetDict(mod), *pname);
PyDict_SetItemString(bpy_pydriver_Dict, *pname, func);
}
Py_DECREF(mod);
}
#ifdef USE_BYTECODE_WHITELIST
/* setup the whitelist */
{
@@ -133,6 +146,10 @@ int bpy_pydriver_create_dict(void)
"bool",
"float",
"int",
/* bl_math */
"clamp",
"lerp",
"smoothstep",
NULL,
};