4

I've found some people mention that almost any blender value can have the magic value "#frame" to use the current frame number as the value.

for example, change an object's X location to '#frame/100" and it will rapidly fly away when walking through the frames.

What I'm looking for, is a list of available # (hashtags) that can be used in this manner, like #end_frame

I found this section in the documentaion, but it seems incomplete, I swear I've seen people use other values here beyond was listed in the docs:

https://docs.blender.org/manual/en/latest/animation/drivers/usage.html#expression

ThorSummoner
  • 344
  • 3
  • 14

2 Answers2

5

The other answer is perfectly valid although I would have added a list of the builtin available namespace functions :

print([k for k in bpy.app.driver_namespace.keys()])

>> 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log1p', 'log10', 'log2', 'modf', 'pow', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc', 'prod', 'perm', 'comb', 'nextafter', 'ulp', 'pi', 'e', 'tau', 'inf', 'nan', 'bpy', 'noise', 'clamp', 'lerp', 'smoothstep'

Additionnally you can access a list of "builtin" keys with

print([k for k in bpy.app.driver_namespace["__builtins__"]])

>> 'abs', 'all', 'any', 'ascii', 'bin', 'breakpoint', 'callable', 'chr', 'compile', 'delattr', 'dir', 'divmod', 'eval', 'exec', 'format', 'getattr', 'globals', 'hasattr', 'hash', 'hex', 'id', 'input', 'isinstance', 'issubclass', 'iter', 'aiter', 'len', 'locals', 'max', 'min', 'next', 'anext', 'oct', 'ord', 'pow', 'print', 'repr', 'round', 'setattr', 'sorted', 'sum', 'vars', 'None', 'Ellipsis', 'NotImplemented', 'False', 'True', 'bool', 'memoryview', 'bytearray', 'bytes', 'classmethod', 'complex', 'dict', 'enumerate', 'filter', 'float', 'frozenset', 'property', 'int', 'list', 'map', 'object', 'range', 'reversed', 'set', 'slice', 'staticmethod', 'str', 'super', 'tuple', 'type', 'zip', 'debug', 'BaseException', 'Exception', 'TypeError', 'StopAsyncIteration', 'StopIteration', 'GeneratorExit', 'SystemExit', 'KeyboardInterrupt', 'ImportError', 'ModuleNotFoundError', 'OSError', 'EnvironmentError', 'IOError', 'WindowsError', 'EOFError', 'RuntimeError', 'RecursionError', 'NotImplementedError', 'NameError', 'UnboundLocalError', 'AttributeError', 'SyntaxError', 'IndentationError', 'TabError', 'LookupError', 'IndexError', 'KeyError', 'ValueError', 'UnicodeError', 'UnicodeEncodeError', 'UnicodeDecodeError', 'UnicodeTranslateError', 'AssertionError', 'ArithmeticError', 'FloatingPointError', 'OverflowError', 'ZeroDivisionError', 'SystemError', 'ReferenceError', 'MemoryError', 'BufferError', 'Warning', 'UserWarning', 'EncodingWarning', 'DeprecationWarning', 'PendingDeprecationWarning', 'SyntaxWarning', 'RuntimeWarning', 'FutureWarning', 'ImportWarning', 'UnicodeWarning', 'BytesWarning', 'ResourceWarning', 'ConnectionError', 'BlockingIOError', 'BrokenPipeError', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionRefusedError', 'ConnectionResetError', 'FileExistsError', 'FileNotFoundError', 'IsADirectoryError', 'NotADirectoryError', 'InterruptedError', 'PermissionError', 'ProcessLookupError', 'TimeoutError', 'open', 'quit', 'exit', 'copyright', 'credits', 'license', 'help'

Note some of these functions don't return a number-compatible value (string, or custom classes) so I wouldn't advise trying to use them.

WARNING: Since it's a mini Python interpreter you risk inadvertently causing side effects. For example try typing quit() in a driver expression field (save your file first) :)

There is also a list of hardcoded functions (Source) : 'bool', 'float', 'int', 'clamp', 'lerp', 'smoothstep".

It seems there are three additional special cases,

  • 'frame' which returns the current frame. Source
  • 'self' which returns the evaluated object. Source
  • 'depsgraph' which can be used to retrieve data from the current context. Source

I would also add that the title and body of the question are misleading. You make the assumption that # grants access to the list of driver namespace functions, but the purpose of writing # in a property field is to automatically setup a driver. It saves you a few clicks you need to manually setup a driver :

  • Right click in the field
  • Add Driver
  • Then in the popup delete the variable
  • Write the driver expression in the field

For example you can write #min(2,cos(2)) or #2 to setup drivers. These two concepts (# and namespace functions) are independant of each other.

Blunder
  • 13,925
  • 4
  • 13
  • 36
Gorgious
  • 30,723
  • 2
  • 44
  • 101
  • but "frame" is not in the list. Do you know why or how to get the list with "frame" included (and eventually others)? – lemon Jan 13 '24 at 08:27
  • @lemon great question. I updated the answer. In addition to the builtins there is bool, float, int, clamp, smoothstep, lerp. Also there are two special cases, frame and self. I don't think there is any other hardcoded keys. – Gorgious Jan 13 '24 at 09:01
  • The reason why IMO is because vanilla namespace keys are directly imported from the python math and mathutils module, while others are treated as special cases – Gorgious Jan 13 '24 at 09:04
  • Gorgious, thanks a lot. It seems there is 'depsgraph' also https://github.com/blender/blender/blob/df8dea92873a7f02a7d9f662a86d204ea97b50f1/source/blender/python/intern/bpy_driver.cc#L211 ... just to complete the list! – lemon Jan 13 '24 at 09:06
  • @lemon Ah, yes missed that one ! Cheers – Gorgious Jan 13 '24 at 09:08
  • Here's a sandbox subset of the driver functionality - which can be used in projects that are considered unsafe (from unsafe source in a folder excluded from script auto-execution, e.g. downloads folder). Why does Blender think this driver expression is a security issue? – Markus von Broady Jan 13 '24 at 09:52
  • BTW I think it's a security vulnerability that Python imports all math functions with * to the sandbox. What kind of guarantee do we have that at some point Python won't be updated with something in builtin like eval? Hopefully there's a note about this in a todo list regarding updating Python version in Blender... – Markus von Broady Jan 13 '24 at 09:53
3

list the currently loaded names with the blender scripting console attrbiute bpy.app.driver_namespace, this is a python dictionary who's keys are valid "python expression" variable names in Driver Scripted Expressions

we can see the functions available to us, and our current frame

>>> bpy.app.driver_namespace.keys()
dict_keys([..., 'acos', 'acosh', 'asin', 'asinh', ...])
>>> bpy.app.driver_namespace['frame']
1
ThorSummoner
  • 344
  • 3
  • 14