Docs: update examples to use Context.temp_override

This commit is contained in:
Campbell Barton 2022-04-20 12:49:13 +10:00
parent f438344cf2
commit 9364e17936
3 changed files with 18 additions and 12 deletions

View File

@ -9,7 +9,7 @@ operator in the different part of the user interface.
The context overrides are passed as a dictionary, with keys matching the context
member names in bpy.context.
For example to override ``bpy.context.active_object``,
you would pass ``{'active_object': object}``.
you would pass ``{'active_object': object}`` to :class:`bpy.types.Context.temp_override`.
.. note::
@ -17,8 +17,10 @@ you would pass ``{'active_object': object}``.
(otherwise, you'll have to find and gather all needed data yourself).
"""
# remove all objects in scene rather than the selected ones
# Remove all objects in scene rather than the selected ones.
import bpy
override = bpy.context.copy()
override['selected_objects'] = list(bpy.context.scene.objects)
bpy.ops.object.delete(override)
from bpy import context
override = context.copy()
override["selected_objects"] = list(context.scene.objects)
with context.temp_override(**override):
bpy.ops.object.delete()

View File

@ -1,17 +1,16 @@
"""
It is also possible to run an operator in a particular part of the user
interface. For this we need to pass the window, screen, area and sometimes
a region.
interface. For this we need to pass the window, area and sometimes a region.
"""
# maximize 3d view in all windows
# Maximize 3d view in all windows.
import bpy
from bpy import context
for window in bpy.context.window_manager.windows:
for window in context.window_manager.windows:
screen = window.screen
for area in screen.areas:
if area.type == 'VIEW_3D':
override = {'window': window, 'screen': screen, 'area': area}
bpy.ops.screen.screen_full_area(override)
with context.temp_override(window=window, area=area):
bpy.ops.screen.screen_full_area()
break

View File

@ -33,6 +33,11 @@ There are 3 optional positional arguments (documented in detail below).
bpy.ops.test.operator(override_context, execution_context, undo)
- override_context - ``dict`` type.
.. deprecated:: 3.2
:class:`bpy.types.Context.temp_override` should be used instead of this argument.
- execution_context - ``str`` (enum).
- undo - ``bool`` type.