Page MenuHome

python unreliable result for bpy.context.view_layer.name when used in drivers
Confirmed, NormalPublic

Description

System Information
Operating system: Linux-4.15.0-96-generic-x86_64-with-debian-buster-sid 64 Bits
Graphics card: GeForce GTX 980/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 430.50

Blender Version
Broken: version: 2.82 (sub 7), branch: master, commit date: 2020-03-12 05:06, hash: rB375c7dc4caf4
Worked: Never (2.8+)

Short description of error
python gives unreliable results for bpy.context.view_layer.name when used in drivers

Exact steps for others to reproduce the error
1 Run the text script to record the python method isViewLayer1 for drivers
2 Edit the driver for the custum property "prop" in the scene panel (bottom right) -> shows invalid python expression (weird but not the core of the problem)
3 Press "update dependencies" -> gives result 1 as expected
4 Switch to "View Layer2" -> gives result 0 or 1 at random
5 Press "update dependencies" mulptiple time -> gives result 0 or 1 at random

For reference, this is the script:

import bpy
from bpy.app.handlers import persistent
from bpy.app import driver_namespace
print (bpy.context.view_layer.name)


@persistent
def isViewLayer1():
   
    if "View Layer 1" in bpy.context.view_layer.name:
        return 1
    else:
        return 0

@persistent
def load_handler(dummy):
    print("Load Handler")
    #dns = bpy.app.driver_namespace
    # register your drivers
    bpy.app.driver_namespace['isViewLayer1'] = isViewLayer1


def register():
    print("Register")
    load_handler(None)
    bpy.app.handlers.load_pre.append(load_handler)
    bpy.app.handlers.load_post.append(load_handler)


register()

Event Timeline

Richard Antalik (ISS) changed the task status from Needs Triage to Confirmed.Apr 14 2020, 12:58 PM
Richard Antalik (ISS) updated the task description. (Show Details)

Accessing bpy.context from within a driver function is problematic, as it's unaware of which context is actually active at the time. For example, Blender could be rendering one view layer while displaying a different one in the 3D viewport.

Evaluating bpy.context.view_layer depends on the current window to obtain the current view layer, so it will fail (by returning the default view layer) when there is no window. Why this happens seemingly randomly when updating the relations is still unknown to me.

The underlying issue is that the driver functions don't have access to the dependency graph. I have written T77086: Passing Dependency Graph to Drivers for this.