f438344cf2
Support a way to temporarily override the context from Python. - Added method `Context.temp_override` context manager. - Special support for windowing variables "window", "area" and "region", other context members such as "active_object". - Nesting context overrides is supported. - Previous windowing members are restored when the context exists unless they have been removed. - Overriding context members by passing a dictionary into operators in `bpy.ops` has been deprecated and warns when used. This allows the window in a newly loaded file to be used, see: T92464 Reviewed by: mont29 Ref D13126
16 lines
504 B
Python
16 lines
504 B
Python
"""
|
|
Overriding the context can be useful to set the context after loading files
|
|
(which would otherwise by None). For example:
|
|
"""
|
|
|
|
import bpy
|
|
from bpy import context
|
|
|
|
# Reload the current file and select all.
|
|
bpy.ops.wm.open_mainfile(filepath=bpy.data.filepath)
|
|
window = context.window_manager.windows[0]
|
|
with context.temp_override(window=window):
|
|
bpy.ops.mesh.primitive_uv_sphere_add()
|
|
# The context override is needed so it's possible to set edit-mode.
|
|
bpy.ops.object.mode_set(mode='EDIT')
|