Context(bpy_struct)

base class — bpy_struct

class bpy.types.Context(bpy_struct)

Current windowmanager and data context

area
Type:

Area, (readonly)

asset
Type:

AssetRepresentation, (readonly)

blend_data
Type:

BlendData, (readonly)

collection
Type:

Collection, (readonly)

engine
Type:

string, default “”, (readonly, never None)

gizmo_group
Type:

GizmoGroup, (readonly)

layer_collection
Type:

LayerCollection, (readonly)

mode
Type:

enum in Context Mode Items, default ‘EDIT_MESH’, (readonly)

preferences
Type:

Preferences, (readonly)

region
Type:

Region, (readonly)

region_data
Type:

RegionView3D, (readonly)

region_popup

The temporary region for pop-ups (including menus and pop-overs)

Type:

Region, (readonly)

scene
Type:

Scene, (readonly)

screen
Type:

Screen, (readonly)

space_data

The current space, may be None in background-mode, when the cursor is outside the window or when using menu-search

Type:

Space, (readonly)

tool_settings
Type:

ToolSettings, (readonly)

view_layer
Type:

ViewLayer, (readonly)

window
Type:

Window, (readonly)

window_manager
Type:

WindowManager, (readonly)

workspace
Type:

WorkSpace, (readonly)

evaluated_depsgraph_get()

Get the dependency graph for the current scene and view layer, to access to data-blocks with animation and modifiers applied. If any data-blocks have been edited, the dependency graph will be updated. This invalidates all references to evaluated data-blocks from the dependency graph.

Returns:

Evaluated dependency graph

Return type:

Depsgraph

copy()
path_resolve(path, coerce=True)

Returns the property from the path, raise an exception when not found.

Parameters:
  • path (str) – patch which this property resolves.

  • coerce (bool) – optional argument, when True, the property will be converted into its Python representation.

classmethod bl_rna_get_subclass(id, default=None)
Parameters:

id (str) – The RNA type identifier.

Returns:

The RNA type or default when not found.

Return type:

bpy.types.Struct subclass

classmethod bl_rna_get_subclass_py(id, default=None)
Parameters:

id (str) – The RNA type identifier.

Returns:

The class or default when not found.

Return type:

type

temp_override(*, window=None, area=None, region=None, **keywords)

Context manager to temporarily override members in the context.

Parameters:
  • window (bpy.types.Window) – Window override or None.

  • screen (bpy.types.Screen) –

    Screen override or None.

    Note

    Switching to or away from full-screen areas & temporary screens isn’t supported. Passing in these screens will raise an exception, actions that leave the context such screens won’t restore the prior screen.

    Note

    Changing the screen has wider implications than other arguments as it will also change the works-space and potentially the scene (when pinned).

  • area (bpy.types.Area) – Area override or None.

  • region (bpy.types.Region) – Region override or None.

  • keywords – Additional keywords override context members.

Returns:

The context manager .

Return type:

ContextTempOverride

Overriding the context can be used to temporarily activate another window / area & region, as well as other members such as the active_object or bone.

Notes:

  • When overriding window, area and regions: the arguments must be consistent, so any region argument that’s passed in must be contained by the current area or the area passed in. The same goes for the area needing to be contained in the current window.

  • Temporary context overrides may be nested, when this is done, members will be added to the existing overrides.

  • Context members are restored outside the scope of the context-manager. The only exception to this is when the data is no longer available.

    In the event windowing data was removed (for example), the state of the context is left as-is. While this isn’t likely to happen, explicit window operation such as closing windows or loading a new file remove the windowing data that was set before the temporary context was created.

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')

This example shows how it’s possible to add an object to the scene in another window.

import bpy
from bpy import context

win_active = context.window
win_other = None
for win_iter in context.window_manager.windows:
    if win_iter != win_active:
        win_other = win_iter
        break

# Add cube in the other window.
with context.temp_override(window=win_other):
    bpy.ops.mesh.primitive_cube_add()

Inherited Properties

Inherited Functions

References