bpy_restrict_state.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # ##### BEGIN GPL LICENSE BLOCK #####
  2. #
  3. # This program is free software; you can redistribute it and/or
  4. # modify it under the terms of the GNU General Public License
  5. # as published by the Free Software Foundation; either version 2
  6. # of the License, or (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software Foundation,
  15. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. #
  17. # ##### END GPL LICENSE BLOCK #####
  18. # <pep8-80 compliant>
  19. """
  20. This module contains RestrictBlend context manager.
  21. """
  22. __all__ = (
  23. "RestrictBlend",
  24. )
  25. import bpy as _bpy
  26. class _RestrictContext:
  27. __slots__ = ()
  28. _real_data = _bpy.data
  29. # safe, the pointer never changes
  30. _real_pref = _bpy.context.preferences
  31. @property
  32. def window_manager(self):
  33. return self._real_data.window_managers[0]
  34. @property
  35. def preferences(self):
  36. return self._real_pref
  37. class _RestrictData:
  38. __slots__ = ()
  39. _context_restrict = _RestrictContext()
  40. _data_restrict = _RestrictData()
  41. class RestrictBlend:
  42. __slots__ = ("context", "data")
  43. def __enter__(self):
  44. self.data = _bpy.data
  45. self.context = _bpy.context
  46. _bpy.data = _data_restrict
  47. _bpy.context = _context_restrict
  48. def __exit__(self, type, value, traceback):
  49. _bpy.data = self.data
  50. _bpy.context = self.context