Context(bpy_struct)

base class — bpy_struct

class bpy.types.Context(bpy_struct)

Current windowmanager and data context

area

(readonly)

Type:

Area

asset

(readonly)

Type:

AssetRepresentation

blend_data

(readonly)

Type:

BlendData

collection

(readonly)

Type:

Collection

engine

(default “”, readonly, never None)

Type:

str

gizmo_group

(readonly)

Type:

GizmoGroup

layer_collection

(readonly)

Type:

LayerCollection

mode

(default 'EDIT_MESH', readonly)

Type:

Literal[Context Mode Items]

preferences

(readonly)

Type:

Preferences

region

(readonly)

Type:

Region

region_data

(readonly)

Type:

RegionView3D

region_popup

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

Type:

Region

scene

(readonly)

Type:

Scene

screen

(readonly)

Type:

Screen

space_data

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

Type:

Space

tool_settings

(readonly)

Type:

ToolSettings

view_layer

(readonly)

Type:

ViewLayer

window

(readonly)

Type:

Window

window_manager

(readonly)

Type:

WindowManager

workspace

(readonly)

Type:

WorkSpace

Buttons Context

texture_slot
Type:

TextureSlot

scene
Type:

Scene

world
Type:

World

object
Type:

Object

mesh
Type:

Mesh

armature
Type:

Armature

lattice
Type:

Lattice

curve
Type:

Curve

meta_ball
Type:

MetaBall

light
Type:

Light

speaker
Type:

Speaker

lightprobe
Type:

LightProbe

camera
Type:

Camera

material
Type:

Material

material_slot
Type:

MaterialSlot

texture
Type:

Texture

texture_user
Type:

ID

texture_user_property
Type:

Property

texture_node
Type:

Node

bone
Type:

Bone

edit_bone
Type:

EditBone

pose_bone
Type:

PoseBone

particle_system
Type:

ParticleSystem

particle_system_editable
Type:

ParticleSystem

particle_settings
Type:

ParticleSettings

cloth
Type:

ClothModifier

soft_body
Type:

SoftBodyModifier

fluid
Type:

FluidModifier

collision
Type:

CollisionModifier

brush
Type:

Brush

dynamic_paint
Type:

DynamicPaintModifier

line_style
Type:

FreestyleLineStyle

collection
Type:

LayerCollection

gpencil
Type:

GreasePencil

grease_pencil
Type:

GreasePencil

curves
Type:

Curves

pointcloud
Type:

PointCloud

volume
Type:

Volume

strip
Type:

Strip

strip_modifier
Type:

StripModifier

Clip Context

edit_movieclip
Type:

MovieClip

edit_mask
Type:

Mask

File Context

active_file
Type:

FileSelectEntry

selected_files
Type:

Sequence[FileSelectEntry]

asset_library_reference
Type:

AssetLibraryReference

asset
Type:

AssetRepresentation

selected_assets
Type:

Sequence[AssetRepresentation]

id
Type:

ID

selected_ids
Type:

Sequence[ID]

Image Context

edit_image
Type:

Image

edit_mask
Type:

Mask

Node Context

selected_nodes
Type:

Sequence[Node]

active_node
Type:

Node

light
Type:

Light

material
Type:

Material

world
Type:

World

Screen Context

scene
Type:

Scene

view_layer
Type:

ViewLayer

visible_objects
Type:

Sequence[Object]

selectable_objects
Type:

Sequence[Object]

selected_objects
Type:

Sequence[Object]

editable_objects
Type:

Sequence[Object]

selected_editable_objects
Type:

Sequence[Object]

objects_in_mode
Type:

Sequence[Object]

objects_in_mode_unique_data
Type:

Sequence[Object]

visible_bones
Type:

Sequence[EditBone]

editable_bones
Type:

Sequence[EditBone]

selected_bones
Type:

Sequence[EditBone]

selected_editable_bones
Type:

Sequence[EditBone]

visible_pose_bones
Type:

Sequence[PoseBone]

selected_pose_bones
Type:

Sequence[PoseBone]

selected_pose_bones_from_active_object
Type:

Sequence[PoseBone]

active_bone
Type:

EditBone | Bone

active_pose_bone
Type:

PoseBone

active_object
Type:

Object

object
Type:

Object

edit_object
Type:

Object

sculpt_object
Type:

Object

vertex_paint_object
Type:

Object

weight_paint_object
Type:

Object

image_paint_object
Type:

Object

particle_edit_object
Type:

Object

pose_object
Type:

Object

active_nla_track
Type:

NlaTrack

active_nla_strip
Type:

NlaStrip

selected_nla_strips
Type:

Sequence[NlaStrip]

selected_movieclip_tracks
Type:

Sequence[MovieTrackingTrack]

annotation_data
Type:

GreasePencil

annotation_data_owner
Type:

ID

active_annotation_layer
Type:

AnnotationLayer

grease_pencil
Type:

GreasePencil

active_operator
Type:

Operator

active_action
Type:

Action

selected_visible_actions
Type:

Sequence[Action]

selected_editable_actions
Type:

Sequence[Action]

visible_fcurves
Type:

Sequence[FCurve]

editable_fcurves
Type:

Sequence[FCurve]

selected_visible_fcurves
Type:

Sequence[FCurve]

selected_editable_fcurves
Type:

Sequence[FCurve]

active_editable_fcurve
Type:

FCurve

selected_editable_keyframes
Type:

Sequence[Keyframe]

ui_list
Type:

UIList

property
Type:

AnyType | str | int

Get the property associated with a hovered button. Returns a tuple of the data-block, data path to the property, and array index.

Note

When the property doesn’t have an associated bpy.types.ID non-ID data may be returned. This may occur when accessing windowing data, for example, operator properties.

import bpy

# Example inserting keyframe for the hovered property.
active_property = bpy.context.property
if active_property:
    datablock, data_path, index = active_property
    datablock.keyframe_insert(data_path=data_path, index=index, frame=1)
asset_library_reference
Type:

AssetLibraryReference

active_strip
Type:

Strip

strips
Type:

Sequence[Strip]

selected_strips
Type:

Sequence[Strip]

selected_editable_strips
Type:

Sequence[Strip]

sequencer_scene
Type:

Scene

Sequencer Context

edit_mask
Type:

Mask

tool_settings
Type:

ToolSettings

Text Context

edit_text
Type:

Text

View3D Context

active_object
Type:

Object

selected_ids
Type:

Sequence[ID]

Methods

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

Get context members as a dictionary.

Return type:

dict[str, Any]

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.

Returns:

Property value or property object.

Return type:

Any | bpy.types.bpy_prop

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

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, screen=None, area=None, region=None, **keywords)

Context manager to temporarily override members in the context.

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

  • screen (bpy.types.Screen | None) –

    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 | None) – Area override or None.

  • region (bpy.types.Region | None) – 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 be 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()

Logging Context Member Access

Context members can be logged by calling logging_set(True) on the “with” target of a temporary override. This will log the members that are being accessed during the operation and may assist in debugging when it is unclear which members need to be overridden.

In the event an operator fails to execute because of a missing context member, logging may help identify which member is required.

This example shows how to log which context members are being accessed. Log statements are printed to your system’s console.

Important

Not all operators rely on Context Members and therefore will not be affected by bpy.types.Context.temp_override, use logging to what members if any are accessed.

import bpy
from bpy import context

my_objects = [context.scene.camera]

with context.temp_override(selected_objects=my_objects) as override:
    override.logging_set(
        True,  # Enable logging.
        hide_missing=True,  # Don't show failed attempts.
    )
    bpy.ops.object.delete()

Inherited Properties

Inherited Functions

References