# How can a blender driver python script access scene properties like FPS?

I would like to be able to access properties of the current scene such as render.fps, frame_start, and frame_end from within a python driver script. I have some data that is defined in terms of seconds, and I'd like the drivers consuming that data to work in two different scenes which have different frame rates.

edit: I tried using bpy.context.scene, but it turns out that is often wrong. Let me be specific: The driver is being evaluated to calculate the position of an object in scene "cave" and bpy.context.scene refers to scene "splash" . In a 1-scene .blend bpy.context.scene works fine. In a multi-scene .blend, bpy.context is not necessarily populated with what a reasonable person would consider 'the context of the driver'.

Inside script editor define your own function and register it for driver use:

import bpy

def driver_fce():
scn = bpy.context.scene
# You can use here:
scn.frame_current
scn.frame_start
scn.frame_end
scn.render.fps
# etc..

return 0    # some value

# Register it to driver namespace under name 'driver_fce':
bpy.app.driver_namespace['driver_fce'] = driver_fce


Use it like this:

Note that such drivers on node properties do not update correctly and its better to use frame change callback handler instead in that case.

• I was originally using that technique in my driver, but when I added more scenes to the .blend file, the driver started malfunctioning because bpy.context.scene referred to a different scene. I eventually determined that bpy.context is NOT what a reasonable person would call "the context of the driver". – Mutant Bob Apr 10 '15 at 15:52
• Could update this answer in that scn can be passed as self for scene drivers. – batFINGER Aug 7 '17 at 11:05
• Don't use bpy.context from drivers, it may work when working with the viewport in simpler files, but it can break (or even crash Blender) when rendering. – dr. Sybren Feb 21 at 11:12

You can do this by setting the Variable type to Single Property, and the prop type to Scene. As ive done in this image.

If you want to do it in the python you can address the scene directly, rather than using context.

eg;

bpy.data.scenes['Scene'].render.fps


• This will point to a specific scene..If you run this driver in different scene it will still point to 'Scene'.. – Jaroslav Jerryno Novotny Apr 11 '15 at 11:06
• I believe this is what the OP asked for. – wardrums Apr 12 '15 at 12:38
• I want the driver to adapt to the properties of the scene it is in. If I evaluate the driver in the 60Hz scene, I need scene.render.fps==60. If I evaluate the driver in the 25Hz scene, I need scene.render.fps==25. – Mutant Bob Apr 12 '15 at 15:20

Via the depsgraph in driver locals()

Not completely sure how dodgy this is, but will ensure context scene

This is a bit dodgy, and having a Scene depsgraph variable is the best approach (see one of the other answers). Such driver variables allow Blender to know the dependencies, and evaluate things in the correct order. However, since it's not possible to add a "current scene" variable, the approach in this answer is probably still the best way to do this.

In blender 2.90+ the depsgraph is added to the driver locals as "depsgraph" along with the variables (release notes). So, you can use this as Python expression:

depsgraph.scene.render.fps


Related

How can I use a driver for multiple objects?

Use an object as a Driver variable

• Attempting this gives me  File "<bpy driver>", line 1, in <module> File "/home/thoth/art/bridge-demo/driver.blend/Text", line 4, in x_for NameError: name 'depsgraph' is not defined  – Mutant Bob Feb 19 at 19:51
• Is that with version 2.49b Bob? – batFINGER Feb 20 at 2:11
• No, it is blender 2.8. I might have to edit this question to give the detailed example that failed, or ask a new question derived from this one. – Mutant Bob Feb 21 at 4:07
• Lol didn't think so. May only be available since 2.9, hard to keep up, or recall when it popped up.. in locals() using method defined in blender.stackexchange.com/a/199299/15543 Other things to look into: use of legacy depsgraph? – batFINGER Feb 21 at 4:16
• This is the right approach. Exactly the kind of thing I had in mind when I added this feature to Blender ;-) – dr. Sybren Feb 21 at 5:42

you can find most of those information in the outliner, in the data-block, you just need to search a little bit section

• This doesn't really address the question. While it does have a bit to do with it, it would be appreciated if you would flesh out your answer to explain a bit more about how you would apply the things that you have suggested. Thanks – TARDIS Maker Sep 10 '16 at 7:33