Logo
Explore Help
Sign In
1berry/blender
1
0
Fork 0
You've already forked blender
Code Issues Packages Projects Releases Wiki Activity
blender/scripts/modules/bl_ui_utils/layout.py

20 lines
500 B
Python
Raw Normal View History

Python: `bl_ui_utils.layout.operator_context` context manager Introduce a context manager that temporarily overrides a UILayout's operator context. Instead of writing this: ```py default_op_context = layout.context layout.context = 'OTHER_VALUE' layout.do_stuff() layout.context = default_op_context ``` you can now write this: ```py from bl_ui_utils.layout import operator_context with operator_context(layout, 'OTHER_VALUE'): layout.do_stuff() ``` This is also exception-safe; it will always ensure the layout's operator context is restored when the `with` body is exited, regardless of whether that's done with an exception or regularly. Idea in-the-hallway approved by @Sergey.
2023-04-07 11:33:56 +02:00
# SPDX-License-Identifier: GPL-2.0-or-later
import contextlib
@contextlib.contextmanager
def operator_context(layout, op_context):
"""Context manager that temporarily overrides the operator context.
>>> with operator_context(layout, 'INVOKE_REGION_CHANNELS'):
... layout.operator("anim.channels_delete")
"""
orig_context = layout.operator_context
layout.operator_context = op_context
try:
yield
finally:
layout.operator_context = orig_context
Reference in New Issue Copy Permalink
Powered by Gitea Version: 1.23.8 Page: 4261ms Template: 27ms
English
Bahasa Indonesia Deutsch English Español Français Gaeilge Italiano Latviešu Magyar nyelv Nederlands Polski Português de Portugal Português do Brasil Suomi Svenska Türkçe Čeština Ελληνικά Български Русский Українська فارسی മലയാളം 日本語 简体中文 繁體中文(台灣) 繁體中文(香港) 한국어
Licenses API