Page MenuHome

`.draw()` method allows arbitrary scene changes after calling `.template_list()` with custom list type.
Confirmed, NormalPublicBUG

Description

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:

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

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.

Event Timeline

Germano Cavalcante (mano-wii) changed the task status from Needs Triage to Confirmed.Jun 18 2021, 8:10 PM
Germano Cavalcante (mano-wii) changed the subtype of this task from "Report" to "Bug".

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.