diff options
author | Campbell Barton <ideasman42@gmail.com> | 2012-12-20 07:56:22 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2012-12-20 07:56:22 +0400 |
commit | 423994bf396869f9eab2ad252abe5c034ca8181e (patch) | |
tree | 60a5589534a5dd1a03991e2a89479cc3a130b482 /release/scripts/modules/bpy_restrict_state.py | |
parent | e4728bf9100f1ea5b701ecdf8b052be3cfab4f2b (diff) |
py api: add restrict state context manager (thats python's context not blenders context),
which restricts bpy.context and bpy.data.
enable this for loading scripts in 'startup' too.
Diffstat (limited to 'release/scripts/modules/bpy_restrict_state.py')
-rw-r--r-- | release/scripts/modules/bpy_restrict_state.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/release/scripts/modules/bpy_restrict_state.py b/release/scripts/modules/bpy_restrict_state.py new file mode 100644 index 00000000000..21c69212731 --- /dev/null +++ b/release/scripts/modules/bpy_restrict_state.py @@ -0,0 +1,57 @@ +# ##### BEGIN GPL LICENSE BLOCK ##### +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# ##### END GPL LICENSE BLOCK ##### + +# <pep8-80 compliant> + +""" +This module contains RestrictBlend context manager. +""" + +__all__ = ( + "RestrictBlend", + ) + +import bpy as _bpy + +class _RestrictContext(): + __slots__ = () + _real_data = _bpy.data + @property + def window_manager(self): + return self._real_data.window_managers[0] + + +class _RestrictData(): + __slots__ = () + + +_context_restrict = _RestrictContext() +_data_restrict = _RestrictData() + + +class RestrictBlend(): + __slots__ = ("context", "data") + def __enter__(self): + self.data = _bpy.data + self.context = _bpy.context + _bpy.data = _data_restrict + _bpy.context = _context_restrict + + def __exit__(self, type, value, traceback): + _bpy.data = self.data + _bpy.context = self.context |