2
$\begingroup$

I need to write a custom driver function (because the algorithm I need is too complex to put into a single line). So, I thought I'd try making a custom driver...

I opened the file editor, pasted my function:

import bpy

def spinnerSlowdown(frame, cutoff, slowdownframes, offset):
  r = radians(frame * -18) + offset 
  if frame < cutoff:
    return r
  elif frame < cutoff + slowdownframes:
    factor =  ((frame - cutoff) / slowdownframes)
    factor = 1 - (factor - 1)  * (factor - 1)
    newFrame = cutoff + (slowdownframes * factor)
    ret = radians(newFrame * -18) + offset
    return ret
  else:
    newFrame = cutoff + slowdownframes
    return radians(newFrame * -18) + offset
    
    
bpy.app.driver_namespace['spinnerSlowdown'] = spinnerSlowdown

... saved it, then ran the script (both with the arrow and Text / Run Script). No errors. (If I type some garbage into the script and run, I get an error, so I know it's at least compiling).

But, when I try to call that fn from a driver: spinnerSlowdown(#frame, 200, 100, radians(130))

It gives me the "Error: Invalid Python expression" message.

If I try to call it from the Python console:

>>> spinnerSlowdown(1,2,3,4)
Traceback (most recent call last):
  File "<blender_console>", line 1, in <module>
NameError: name 'spinnerSlowdown' is not defined

So, it looks like my fn isn't visible.

Any ideas?

I'm a complete noob to writing Python inside Blender... so be kind.

$\endgroup$

2 Answers 2

3
$\begingroup$

Your driver expression spinnerSlowdown(#frame, 200, 100, radians(130)) has two little problems:

  • First, remove the # before frame. The # is used to create a driver automatically, but you already are in a driver, so to say.
  • radians is unknown in your function. Add this line right after import bpy in your script: from math import radians.

And if you want to call your function from the console, you need to do

>>> bpy.app.driver_namespace['spinnerSlowdown'](...)

$\endgroup$
3
  • 1
    $\begingroup$ radians is not unknown in drivers, all functions from math module are available (also added to the whitelist in the safe python). Indeed the script needs to import radians. $\endgroup$ Sep 30 at 22:32
  • 1
    $\begingroup$ @MarkusvonBroady yes, you're right, i misread the error message first (it was unknown in the custom function, not in the expression). I fixed my answer, thx again. $\endgroup$
    – taiyo
    Sep 30 at 22:44
  • $\begingroup$ The 'import radians' thing was the trick (the # was just an experiment). Thanks! $\endgroup$
    – cleek
    Oct 1 at 13:30
2
$\begingroup$

spinnerSlowdown(#frame, 200, 100, radians(130)) is indeed not a valid Python expression. Specifically the # character is unexpected.

The #frame means two things:

  • # - create a driver with the following expression
  • frame - current frame number

So either type #spinnerSlowdown(frame, 200, 100, radians(130)) in a normal field to create a new driver with that expression, or skip the # and type spinnerSlowdown(frame, 200, 100, radians(130)) in an already driven field (highlighted in pink) or in the driver editor.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .