Context in Timers #79690
Labels
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
7 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender#79690
Loading…
Reference in New Issue
No description provided.
Delete Branch "%!s(<nil>)"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Calling operators in [timers ]] is difficult currently, because very few attributes of
bpy.context
are initialized (which is expected and not a bug). The issue is, even when you [ https:*docs.blender.org/api/blender2.8/bpy.ops.html#overriding-context | override the context , problems remain. This task exists to keep track of these problems and to discuss solutions.Problem 1: Incomplete operator
poll
functionsMany operators have
poll
functions that are supposed to check if the operator can run in a given context. The issue is that many poll functions are incorrect, because they assume thatCTX_wm_window(C)
andCTX_wm_screen(C)
don't returnNULL
(maybe some more likeCTX_wm_workspace(C)
are missing here). Those context variables are almost always initialized, but not in timers. This is the main source of crashes when dealing with operators in timers.To reproduce the issue, run this script:
bpy.ops.mesh.primitive_uv_sphere_add()
This results in a crash, but it should throw an exception due to incorrect context. The issue is that this operator internally calls
ED_object_editmode_exit
inmake_prim_finish
, which callsCTX_data_edit_object
which returnsNULL
, becauseCTX_data_screen(C)
isNULL
.The script can be fixed as follows:
Solution 1
Try to find all the incorrect
poll
functions and fix them individually. This will definitely solve the issue, but might require a lot of effort (maybe it's also easy, I haven't tried yet).Solution 2
Define some "minimal context" that is required to run operators. For example, we could say that in order to run an operator the context must have a window and a screen, otherwise the
poll
function of the operatoris not even checked. It's fairly easy to implement such a check inWM_operator_poll
. The downside is that there might be some operators that can run without any context. To mitigate this downside, we could add a flag that can be added to operators that don't require any context.Problem 2: Context Override
We do support context overrides when calling operators from Python already. However, there is one severe limitation when calling a operator that is defined in Python that itself calls other operators. The issues is that the context override only applies to the immediately called operator, but not to the indirectly called operators.
To see this issue in action, run the following script and check the terminal output:
'''Create a minimum context override that is necessary to avoid crashes.'''
wm = bpy.data.window_managers[0]
window = wm.windows[0]
context = bpy.context.copy()
context["window"] = window
context["screen"] = window.screen
context["workspace"] = window.workspace
return context
context = get_base_context()
bpy.ops.object.my_operator1(context)
bl_idname = "object.my_operator1"
bl_label = "My Operator 1"
def execute(self, context):
print(1, context.screen)
bpy.ops.object.my_operator2()
return {"FINISHED"}
bl_idname = "object.my_operator2"
bl_label = "My Operator 2"
def execute(self, context):
print(2, context.screen)
return {"FINISHED"}
In my case it prints:
Solution 1
Apply this patch:
context_dict_back = CTX_py_dict_get(C);
Py_XINCREF(context_dict); /* so we done loose it */
if (WM_operator_poll_context((bContext *)C, ot, context) == false) {
With this change the python context override is not reset when an operator is called without an explicit context override. This fixes the example above, and might even work in general. One issue is when someone overwrites the context without calling
context.copy()
first, because then the underlying context might suddenly change in unexpected ways.Solution 2
Refactor
bContext
to properly support "context override stacking". The pattern that parts of the context are temporarily overridden is quite common in Blender. We could changebContext
so that it supports multiple layers of overrides natively. The API could look like so:An "override" could be a single overridden property, or a python dictionary or something else, that we do not know yet.
While this refactor is a larger project, it seems doable to me, because
bContext
is fairly well encapsulated.With this system, we could also support doing context overrides with a https://book.pythontips.com/en/latest/context_managers.html in python, instead of copying the entire context and passing it to the operator call.
Changed status from 'Needs Triage' to: 'Confirmed'
Added subscriber: @JacquesLucke
Added subscribers: @brecht, @ideasman42
Incomplete operator poll functions: perhaps we can just set the first window as context? Background mode is the same really. It has a window in its context which I guess makes most operators work there.
Content override: I think we should support context override stacking. I think it would be enough to define a function that can merge two contexts to create a new one? I don't think push/pop functions are needed.
That sounds like it could work. Didn't know if this was acceptable. Is there always at least one window? Will try this tomorrow.
That could work if you mean it the way I interpret it. Did you mean to make individual
bContext
instances more immutable? Can you show an example how one would override the area of a context with that approach (just some pseudocode)?I was thinking that, but actually I'm not sure it will work. It means
bpy.context
can pointer to a different context, which might break some Python code.Still, we could copy the entire context for backup, overwrite the context with the merged result, then restore the backup. No need to do push/pop on the level of individual context members, performance will not be a problem.
Added subscriber: @RedMser
Added subscriber: @Zweistein
I ran into this exact problem today. I have an expensive python operator. Therefore I dont run it on every mesh change, but I wait till the last mesh change of the users is >2s ago. something like an lazy update.
My timer starts the operator, but it gives the error that the context is not correct for editmode toggle.
Added subscriber: @oweissbarth
Regarding context override, suggest to stack the override dictionaries, so an operator that's called from Python will merge it's dictionary with the existing override dictionary (creating a new override dictionary).
There are some things to consider with data becoming invalid though, currently when context data is set (
CTX_data_scene_set
,CTX_wm_window_set
... etc) it creates an editable copy of the override dictionary and removes those context members. So the context-override doesn't prevent the operator modifying the context as part of it's own logic. Ideally this would act on all context overrides up the call stack.Added subscriber: @pauanyu_blender