undo_context.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # coding:utf-8
  2. #!/usr/bin/python
  3. #
  4. # Copyright (c) Contributors to the Open 3D Engine Project.
  5. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  6. #
  7. # SPDX-License-Identifier: Apache-2.0 OR MIT
  8. #
  9. #
  10. # -------------------------------------------------------------------------
  11. # <DCCsi>\\azpy\\maya\\\helpers\\undo_context.py
  12. # Maya undo context
  13. # Reference: Rob Galanakis, Tech-artists.org
  14. # -------------------------------------------------------------------------
  15. """
  16. This module creates a simple Class object for managing Maya Undo Chunking.
  17. """
  18. # -------------------------------------------------------------------------
  19. # -- Standard Python modules
  20. import os
  21. from functools import wraps
  22. # -- External Python modules
  23. # -- Lumberyard Extension Modules
  24. from azpy import initialize_logger
  25. from azpy.env_bool import env_bool
  26. from azpy.constants import ENVAR_DCCSI_GDEBUG
  27. from azpy.constants import ENVAR_DCCSI_DEV_MODE
  28. # -- Maya Modules --
  29. import maya.cmds as mc
  30. # -------------------------------------------------------------------------
  31. # -------------------------------------------------------------------------
  32. # -- Misc Global Space Definitions
  33. _DCCSI_GDEBUG = env_bool(ENVAR_DCCSI_GDEBUG, False)
  34. _DCCSI_DEV_MODE = env_bool(ENVAR_DCCSI_DEV_MODE, False)
  35. _PACKAGENAME = __name__
  36. if _PACKAGENAME == '__main__':
  37. _PACKAGENAME = 'azpy.dcc.maya.helpers.undo_context'
  38. _LOGGER = initialize_logger(_PACKAGENAME, default_log_level=int(20))
  39. _LOGGER.debug('Invoking:: {0}.'.format({_PACKAGENAME}))
  40. # -------------------------------------------------------------------------
  41. # =========================================================================
  42. # First Class
  43. # =========================================================================
  44. class UndoContext(object):
  45. """
  46. This Class creates a undo context chunk
  47. """
  48. def __enter__(self):
  49. mc.undoInfo(openChunk=True)
  50. def __exit__(self, *exc_info):
  51. mc.undoInfo(closeChunk=True)
  52. # -------------------------------------------------------------------------
  53. # =========================================================================
  54. # Undo Decorator ... makes a whole function call undoable
  55. # =========================================================================
  56. def undo(func, autoUndo=False):
  57. """
  58. Puts the wrapped `func` into a single Maya Undo action,
  59. then undoes it when the function enters the finally: block
  60. """
  61. @wraps(func)
  62. def _undofunc(*args, **kwargs):
  63. try:
  64. # start an undo chunk
  65. mc.undoInfo( openChunk = True )
  66. return func( *args, **kwargs )
  67. finally:
  68. # after calling the func, end the undo chunk and undo
  69. mc.undoInfo( closeChunk = True )
  70. if autoUndo:
  71. mc.undo()
  72. return _undofunc
  73. # -------------------------------------------------------------------------
  74. # =========================================================================
  75. # Public Functions
  76. # =========================================================================
  77. # --First Function---------------------------------------------------------
  78. def test():
  79. """test() example undo context """
  80. ## This is how you call to the UndoContext()
  81. with UndoContext():
  82. # Do a couple things, in a block
  83. # undo should step backwards clearing all of them at once
  84. mc.polySphere(sx=10, sy=15, r=20)
  85. mc.move( 1, 1, 1 )
  86. mc.move( 5, y=True )
  87. # -------------------------------------------------------------------------
  88. #==========================================================================
  89. # Module Tests
  90. #==========================================================================
  91. if __name__ == "__main__" :
  92. test()