Context(bpy_struct)

base class — bpy_struct

class bpy.types.Context(bpy_struct)

Current windowmanager and data context

area
Type

Area, (readonly)

asset_file_handle

The file of an active asset. Avoid using this, it will be replaced by a proper AssetHandle design

Type

FileSelectEntry, (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 [‘EDIT_MESH’, ‘EDIT_CURVE’, ‘EDIT_CURVES’, ‘EDIT_SURFACE’, ‘EDIT_TEXT’, ‘EDIT_ARMATURE’, ‘EDIT_METABALL’, ‘EDIT_LATTICE’, ‘POSE’, ‘SCULPT’, ‘PAINT_WEIGHT’, ‘PAINT_VERTEX’, ‘PAINT_TEXTURE’, ‘PARTICLE’, ‘OBJECT’, ‘PAINT_GPENCIL’, ‘EDIT_GPENCIL’, ‘SCULPT_GPENCIL’, ‘WEIGHT_GPENCIL’, ‘VERTEX_GPENCIL’, ‘SCULPT_CURVES’], default ‘EDIT_MESH’, (readonly)

preferences
Type

Preferences, (readonly)

region
Type

Region, (readonly)

region_data
Type

RegionView3D, (readonly)

scene
Type

Scene, (readonly)

screen
Type

Screen, (readonly)

space_data
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 (string) – patch which this property resolves.

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

classmethod bl_rna_get_subclass(id, default=None)
Parameters

id (string) – 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 (string) – The RNA type identifier.

Returns

The class or default when not found.

Return type

type

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

Context manager to temporarily override members in the context.

Parameters
Returns

The context manager .

Return type

context manager

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