7

I wrote "PI" in a driver expression, and Blender threw up this warning message. I don't understand why it thinks there's a problem, nor what implications any of the choices have.

What is the security issue with using PI in a driver expression? What "unexpected behavior" is it talking about? If I choose "Ignore", will my driver stop working? If I choose "Allow Execution", am I inadvertently allowing other scripts I'm unaware of to run? And what does "Permanently allow execution of scripts" mean, is it saying any python scripts in any blender file can run henceforth, or what?

"For security reasons, automatic execution of Python scripts in this file was disabled: Driver '90*(var-1.0)*PI/180'. This may lead to unexpected behavior.

adurdin
  • 258
  • 3
  • 5

2 Answers2

6

The problem isn't with the specific driver you've implemented, but with python scripts in general. The automatic execution of all python scripts is disabled by default.

From the Blender 2.90 Manual:

The ability to include Python scripts within blend-files is valuable for advanced tasks such as rigging and automation. However, it poses a security risk since Python does not restrict what a script can do.

Therefore, you should only run scripts from sources you know and trust.

Automatic execution is disabled by default, however, some blend-files need this to function properly.

This is mostly an issue when you are opening a Blender file which you didn't yourself make. It could contain any conceivable script.

In Blender Preferences > Save & Load there is a checkbox to Auto Run Python Scripts. You can add excluded paths to, for example, your downloads folder, so that foreign blend files are still restricted.

You can also manually enable scripts in specific blend files as you open then, by checking the Trusted Source box in the File Browser.

Rekov
  • 2,522
  • 1
  • 12
  • 23
  • I still don't understand what it objected to in this case, nor why it's prompting me about a blender file I just created and am working in (rather than opening). But the bigger picture makes sense now, thanks. – adurdin Sep 20 '20 at 10:26
  • Blender by default objects to all python scripts, and your driver counts as a python script, regardless of its contents. You can configure Blender to allow python scripts by default in all files, and then exclude files in certain folders if you want. – Rekov Sep 20 '20 at 21:57
  • 1
    I have encountered this a number of times since, and its not all drivers that it objects to. Turns out it is objecting here because the driver expression contains a variable ('PI', here) that it doesn't recognise. In the same way, it objects if the expression uses a function it doesn't recognise. Still doesn't make sense that thats a "security" issue, since the error just means the driver won't work. And its awful that the message doesn't explain the actual problem. Oh well. – adurdin Sep 28 '21 at 22:55
2

Solution

From the context - PI / 180 - it seems by PI you mean the $π$. In Python the constant has the name pi, and the letter case matters (Pi, pI and PI won't work).

So your problem is just that, use pi.

Why the error is confusing?

The reason the problem is misreported is just the logical order of the driver evaluation:

  1. First Blender tries to parse it into tokens to be evaluated by its own stack machine, but it doesn't recognize PI, fails to convert it to a token, and proceeds to p. 2.
  2. The expression is compiled to Python interpreter opcodes. Now Blender checks if script auto-execution is enabled, and the .blend file's path is not excluded from the auto-execution. In such case it goes to p. 4.
  3. All opcodes and names are checked against a whitelist - PI is not there, so the driver is not enabled and you get a security error and p. 4 is skipped.
  4. Ever time the driver is evaluated, the compiled code is sent to the Python interpreter, and Blender waits for the result. If the expression produces an error, the driver is disabled.

Your driver fails at p. 3. and never proceeds further.

Some tests

  • a driver 5 / 0 is a simple expression that will yield a non-python error:

ERROR (bke.fcurve): C:\Users\blender\git\blender-v310\blender.git\source\blender\blenkernel\intern\fcurve_driver.c:1030 driver_evaluate_simple_expr: Division by Zero in Driver: '5/0'

  • a driver 1 / frame will work until you go to frame #0 - then it will report an error (and stop working from now on, even if you change the frame):

ERROR (bke.fcurve): C:\Users\blender\git\blender-v310\blender.git\source\blender\blenkernel\intern\fcurve_driver.c:1030 driver_evaluate_simple_expr: Division by Zero in Driver: '1/frame'

  • a driver 1 % frame will be a slow expression due to the usage of %, which is unsupported by the simple parser. It will again work only until you hit frame 0, then stop working forever (you need to reapply the driver), but this time you will get a Python error:

Error in Driver: The following Python expression failed:

  '1 % frame'

Traceback (most recent call last):

File "", line 1, in

ZeroDivisionError: float modulo

  • And finally if you enable script auto-execution and try your driver:

Error in Driver: The following Python expression failed:

  '90*(var - 1.0)*PI/180'

Traceback (most recent call last):

File "", line 1, in

NameError: name 'PI' is not defined. Did you mean: 'pi'?

Further reading

Markus von Broady
  • 36,563
  • 3
  • 30
  • 99