.draw() method allows arbitrary scene changes after calling .template_list() with custom list type. #89253

Closed
opened 2021-06-18 02:40:56 +02:00 by Will · 6 comments

System Information
Operating system: Linux
Graphics card: Intel

Blender Version
Broken: 2.92, 2.93.0

Short description of error

Usually Blender forbids property changes in .draw() methods. But they're not caught after a call to .template_list() with a custom list type.

This allows some weird and fragile behaviours to be implemented, both on purpose and accidentally. For example, the below panel has a draw method that increments its list's index on every redraw:

DrawAbuse.gif

Code examples:

def draw(self, context):
	context.scene.SomeProp = 5

Fails: AttributeError: Writing to ID classes in this context is not allowed: Scene, Scene datablock, error setting Scene.SomeProp

def draw(self, context):
	context.scene.SomeProp = 5
	self.layout.template_list(*someargs)

Fails: AttributeError: Writing to ID classes in this context is not allowed: Scene, Scene datablock, error setting Scene.SomeProp

def draw(self, context):
	self.layout.template_list(listtype_name="ACustomList", *someargs)
	context.scene.SomeProp = 5

Works, but probably isn't meant to.

Exact steps for others to reproduce the error

DrawAbuse.blend

Download the attached .BLEND file, then mouse over the open "Draw Method Abuse" panel in the 3DView to trigger its draw method.

The displayed EnumProperty can be set to "Fail" to exhibit the usual behaviour, or "Abuse" to show the anomalous behaviour.

**System Information** Operating system: Linux Graphics card: Intel **Blender Version** Broken: 2.92, 2.93.0 **Short description of error** Usually Blender forbids property changes in `.draw()` methods. But they're not caught after a call to `.template_list()` with a custom list type. This allows some weird and fragile behaviours to be implemented, both on purpose and accidentally. For example, the below panel has a draw method that increments its list's index on every redraw: ![DrawAbuse.gif](https://archive.blender.org/developer/F10178894/DrawAbuse.gif) Code examples: ``` def draw(self, context): context.scene.SomeProp = 5 ``` Fails: `AttributeError: Writing to ID classes in this context is not allowed: Scene, Scene datablock, error setting Scene.SomeProp` ``` def draw(self, context): context.scene.SomeProp = 5 self.layout.template_list(*someargs) ``` Fails: `AttributeError: Writing to ID classes in this context is not allowed: Scene, Scene datablock, error setting Scene.SomeProp` ``` def draw(self, context): self.layout.template_list(listtype_name="ACustomList", *someargs) context.scene.SomeProp = 5 ``` Works, but probably isn't meant to. **Exact steps for others to reproduce the error** [DrawAbuse.blend](https://archive.blender.org/developer/F10178891/DrawAbuse.blend) Download the attached `.BLEND` file, then mouse over the open "Draw Method Abuse" panel in the 3DView to trigger its draw method. The displayed `EnumProperty` can be set to "Fail" to exhibit the usual behaviour, or "Abuse" to show the anomalous behaviour.
Author

Added subscriber: @WCN

Added subscriber: @WCN

Added subscriber: @mano-wii

Added subscriber: @mano-wii

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

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

I can confirm.
This is because bpy_class_call is being called like recursively and so rna_disallow_writes is reset ahead of time.

This could fix:

diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c
index fb1cb823964..89515e409e1 100644
--- a/source/blender/python/intern/bpy_rna.c
+++ b/source/blender/python/intern/bpy_rna.c
@@ -8557,6 +8557,7 @@ static int bpy_class_call(bContext *C, PointerRNA *ptr, FunctionRNA *func, Param
       }
 
 #ifdef USE_PEDANTIC_WRITE
+      const int prev_write = rna_disallow_writes;
       rna_disallow_writes = is_readonly ? true : false;
 #endif
       /* ***Main Caller*** */
@@ -8566,7 +8567,7 @@ static int bpy_class_call(bContext *C, PointerRNA *ptr, FunctionRNA *func, Param
       /* ***Done Calling*** */
 
 #ifdef USE_PEDANTIC_WRITE
-      rna_disallow_writes = false;
+      rna_disallow_writes = prev_write;
 #endif
 
       RNA_parameter_list_end(&iter);

This seems to be something for the #python_api team.

I can confirm. This is because `bpy_class_call` is being called like recursively and so `rna_disallow_writes` is reset ahead of time. This could fix: ``` diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index fb1cb823964..89515e409e1 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -8557,6 +8557,7 @@ static int bpy_class_call(bContext *C, PointerRNA *ptr, FunctionRNA *func, Param } #ifdef USE_PEDANTIC_WRITE + const int prev_write = rna_disallow_writes; rna_disallow_writes = is_readonly ? true : false; #endif /* ***Main Caller*** */ @@ -8566,7 +8567,7 @@ static int bpy_class_call(bContext *C, PointerRNA *ptr, FunctionRNA *func, Param /* ***Done Calling*** */ #ifdef USE_PEDANTIC_WRITE - rna_disallow_writes = false; + rna_disallow_writes = prev_write; #endif RNA_parameter_list_end(&iter); ``` This seems to be something for the #python_api team.

This issue was referenced by 895d3cd11e

This issue was referenced by 895d3cd11e19b2ec361daad0064155a366359f98

Changed status from 'Confirmed' to: 'Resolved'

Changed status from 'Confirmed' to: 'Resolved'
Campbell Barton self-assigned this 2021-08-10 11:56:10 +02: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
4 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#89253
No description provided.