Quick way to force context area in test script

Hey I’m using the following utility context manager to run test code in other contexts:

import bpy
from contextlib import contextmanager

@contextmanager
def context(area_type: str):
    area = bpy.context.area
    former_area_type = area.type
    area.type = area_type
    try:
        yield area
    finally:
        area.type = former_area_type

And then to use:

with context('NLA_EDITOR'):
    print(bpy.context.selected_nla_strips)
3 Likes