python unreliable result for bpy.context.view_layer.name when used in drivers #75553

Closed
opened 2020-04-09 15:11:31 +02:00 by thierrry kobyleski · 6 comments

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: 375c7dc4ca
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

active_layer.blend

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()
**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: `375c7dc4ca` 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 [active_layer.blend](https://archive.blender.org/developer/F8459718/active_layer.blend) 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() ```

Added subscriber: @thierrykobyleski

Added subscriber: @thierrykobyleski

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'
Sybren A. Stüvel self-assigned this 2020-05-26 10:45:47 +02:00

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 #77086 (Passing Dependency Graph to Drivers) for this.

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 #77086 (Passing Dependency Graph to Drivers) for this.

D8047: #77086 Animation: Passing Dependency Graph to Drivers has a fix for this issue. It just needs to be reviewed. In practice, this means that drivers will have a variable depsgraph in their local scope, which should be passed to the function that needs it. So, the driver expression would be isViewLayer1(depsgraph), and the function would be:

def isViewLayer1(depsgraph):
    return "View Layer 1" in depsgraph.view_layer.name

(In Python True == 1 and False == 0, and bool is actually a subclass of int, so there is no need for the if/return 1/return 0 construct ;-) )

By using the depsgraph that is passed to the driver, things keep working even when during rendering there are multiple view layers being evaluated at the same time.

[D8047: #77086 Animation: Passing Dependency Graph to Drivers](https://archive.blender.org/developer/D8047) has a fix for this issue. It just needs to be reviewed. In practice, this means that drivers will have a variable `depsgraph` in their local scope, which should be passed to the function that needs it. So, the driver expression would be `isViewLayer1(depsgraph)`, and the function would be: ``` def isViewLayer1(depsgraph): return "View Layer 1" in depsgraph.view_layer.name ``` (In Python `True == 1` and `False == 0`, and `bool` is actually a subclass of `int`, so there is no need for the `if`/`return 1`/`return 0` construct ;-) ) By using the depsgraph that is passed to the driver, things keep working even when during rendering there are multiple view layers being evaluated at the same time.

Changed status from 'Confirmed' to: 'Resolved'

Changed status from 'Confirmed' to: 'Resolved'

Resolved in 686ab4c940.

Resolved in 686ab4c940.
Thomas Dinges added this to the 2.90 milestone 2023-02-08 16:27:21 +01:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#75553
No description provided.