Override Context, very simple way
Added 2021-11-19 11:48:58 +0000 UTCJust use the code below (got it from the link: https://devtalk.blender.org/t/quick-way-to-force-context-area-in-test-script/11139/4 )
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
Then you can override the context using:
with context('GRAPH_EDITOR'):
bpy.ops.graph.clean(threshold=0.02, channels=False)
List from the site: https://docs.blender.org/api/current/bpy.types.Area.html#bpy.types.Area.type
- EMPTY Empty.
- VIEW_3D 3D Viewport, Manipulate objects in a 3D environment.
- IMAGE_EDITOR UV/Image Editor, View and edit images and UV Maps.
- NODE_EDITOR Node Editor, Editor for node-based shading and compositing tools.
- SEQUENCE_EDITOR Video Sequencer, Video editing tools.
- CLIP_EDITOR Movie Clip Editor, Motion tracking tools.
- DOPESHEET_EDITOR Dope Sheet, Adjust timing of keyframes.
- GRAPH_EDITOR Graph Editor, Edit drivers and keyframe interpolation.
- NLA_EDITOR Nonlinear Animation, Combine and layer Actions.
- TEXT_EDITOR Text Editor, Edit scripts and in-file documentation.
- CONSOLE Python Console, Interactive programmatic console for advanced editing and script development.
- INFO Info, Log of operations, warnings and error messages.
- TOPBAR Top Bar, Global bar at the top of the screen for global per-window settings.
- STATUSBAR Status Bar, Global bar at the bottom of the screen for general status information.
- OUTLINER Outliner, Overview of scene graph and all available data-blocks.
- PROPERTIES Properties, Edit properties of active object and related data-blocks.
- FILE_BROWSER File Browser, Browse for files and assets.
- SPREADSHEET Spreadsheet, Explore geometry data in a table.
- PREFERENCES Preferences, Edit persistent configuration settings.