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.

  • default (bpy.types.Struct | None) – The value to return when not found.

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.

  • default (type | None) – The value to return when not found.

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ΒΆ