3
0

set_callbacks.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. """
  12. Module Documentation:
  13. DccScriptingInterface:: SDK//maya//scripts//set_callbacks.py
  14. This module manages a set of predefined callbacks for maya
  15. """
  16. # -------------------------------------------------------------------------
  17. # -- Standard Python modules
  18. import os
  19. import sys
  20. import logging as _logging
  21. # -- External Python modules
  22. from box import Box
  23. # maya imports
  24. import maya.cmds as mc
  25. import maya.api.OpenMaya as om
  26. # -- DCCsi Extension Modules
  27. from DccScriptingInterface.azpy.constants import *
  28. import DccScriptingInterface.azpy.dcc.maya
  29. DccScriptingInterface.azpy.dcc.maya.init() # <-- should have already run?
  30. import DccScriptingInterface.azpy.dcc.maya.callbacks.event_callback_handler as azEvCbH
  31. import DccScriptingInterface.azpy.dcc.maya.callbacks.node_message_callback_handler as azNdMsH
  32. # Node Message Callback Setup
  33. import DccScriptingInterface.azpy.dcc.maya.callbacks.on_shader_rename as oSR
  34. from DccScriptingInterface.Tools.DCC.Maya.Scripts.set_defaults import set_defaults
  35. # -------------------------------------------------------------------------
  36. # -------------------------------------------------------------------------
  37. # global scope
  38. from DccScriptingInterface.Tools.DCC.Maya import _PACKAGENAME
  39. _MODULENAME = f'{_PACKAGENAME}.set_callbacks'
  40. _LOGGER = _logging.getLogger(_MODULENAME)
  41. _LOGGER.info(f'Initializing: {_MODULENAME}')
  42. from DccScriptingInterface.globals import *
  43. # -------------------------------------------------------------------------
  44. # -------------------------------------------------------------------------
  45. # global scope callbacks, set up set and initialize all to None
  46. # To Do: should callback initialization use data-driven settings?
  47. # To Do: should we move callback initialization to a sub-module?
  48. # To Do: move the callback key like 'NewSceneOpened' here (instead of None)
  49. # ^ this would provide ability to loop through and replace key with CB object
  50. _G_CALLBACKS = Box(box_dots=True) # global scope container
  51. _G_PRIMEKEY = 'DCCsi_callbacks'
  52. _G_CALLBACKS[_G_PRIMEKEY] = True # required prime key
  53. # -------------------------------------------------------------------------
  54. def init_callbacks(_callbacks=_G_CALLBACKS):
  55. # store as a dict (Box is a fancy dict)
  56. _callbacks[_G_PRIMEKEY] = True # required prime key
  57. # signature dict['callback key'] = ('CallBack'(type), func, callbackObj)
  58. _callbacks['on_new_file'] = ['NewSceneOpened', set_defaults, None]
  59. _callbacks['new_scene_fix_paths'] = ['NewSceneOpened', install_fix_paths, None]
  60. _callbacks['post_scene_fix_paths'] = ['PostSceneRead', install_fix_paths, None]
  61. _callbacks['workspace_changed'] = ['workspaceChanged', update_workspace, None]
  62. _callbacks['quit_app'] = ['quitApplication', uninstall_callbacks, None]
  63. # nodeMessage style callbacks
  64. # fire a function
  65. _func_00 = oSR.on_shader_rename_rename_shading_group
  66. # using a nodeMessage callback trigger
  67. _cb_00 = om.MNodeMessage.addNameChangedCallback
  68. # all nodeMessage type callbacks can use 'nodeMessageType' key
  69. _callbacks['shader_rename'] = ['nodeMessageType', (_func_00, _cb_00), None]
  70. return _callbacks
  71. # -------------------------------------------------------------------------
  72. # -------------------------------------------------------------------------
  73. def uninstall_callbacks():
  74. """Bulk uninstalls hte globally defined set of callbacks:
  75. _G_callbacks"""
  76. global _G_CALLBACKS
  77. _LOGGER.debug('uninstall_callbacks() fired')
  78. for key, value in _G_CALLBACKS:
  79. if value[2] is not None: # have a cb
  80. value[2].uninstall() # so uninstall it
  81. else:
  82. _LOGGER.warning('No callback in: key {0}, value:{1}'
  83. ''.format(key, value))
  84. _G_CALLBACKS = None
  85. _LOGGER.info('DCCSI CALLBACKS UNINSTALLED ... EXITING')
  86. return _G_CALLBACKS
  87. # -------------------------------------------------------------------------
  88. # -------------------------------------------------------------------------
  89. def install_callbacks(_callbacks=_G_CALLBACKS):
  90. """Bulk installs the globally defined set of callbacks:
  91. _G_callbacks"""
  92. _LOGGER.debug('install_callback_set() fired')
  93. _callbacks = init_callbacks(_callbacks)
  94. # we initialized the box with this so pop it
  95. if 'box_dots' in _callbacks:
  96. _callbacks.pop('box_dots')
  97. # don't pass anything but carefully considered dict
  98. if _G_PRIMEKEY in _callbacks:
  99. _primekey = _callbacks.pop(_G_PRIMEKEY)
  100. else:
  101. _LOGGER.error('No prime key, use a correct dictionary')
  102. #To Do: implement error handling and return codes
  103. return _callbacks[None]
  104. for key, value in _G_CALLBACKS.items():
  105. # we popped the prime key should the rest should be safe
  106. if value[0] != 'nodeMessageType':
  107. # set callback up
  108. _cb = azEvCbH.EventCallbackHandler(value[0],
  109. value[1])
  110. # ^ installs by default
  111. # stash it back into managed dict
  112. value[2] = _cb
  113. # value[2].install()
  114. else:
  115. # set up callback, value[1] should be tupple(func, trigger)
  116. _cb = azNdMsH.NodeMessageCallbackHandler(value[1][0],
  117. value[1][1])
  118. # ^ installs by default
  119. # stash it back into managed dict
  120. value[2] = _cb
  121. # value[2].install()
  122. return _callbacks
  123. # -------------------------------------------------------------------------
  124. # -------------------------------------------------------------------------
  125. def install_fix_paths(foo=None):
  126. """Installs and triggers a fix paths module.
  127. This can repair broken reference paths in shaders"""
  128. global _fix_paths
  129. _fix_paths = None
  130. _LOGGER.debug('install_fix_paths() fired')
  131. # if we don't have it already, this function is potentially triggered
  132. # by a callback, so we don't need to keep importing it.
  133. try:
  134. _fix_paths
  135. reload(_fix_paths)
  136. except Exception as e:
  137. try:
  138. import fixPaths as _fix_paths
  139. except Exception as e:
  140. # To Do: not implemented yet
  141. _LOGGER.warning('NOT IMPLEMENTED: {0}'.format(e))
  142. # if we have it, use it
  143. if _fix_paths:
  144. return _fix_paths.main()
  145. else:
  146. # To Do: implement error handling and return codes
  147. return 1
  148. # -------------------------------------------------------------------------
  149. # -------------------------------------------------------------------------
  150. def update_workspace(foo=None):
  151. """Forces and update of the workspace (workspace.mel)"""
  152. _LOGGER.debug('update_workspace() fired')
  153. result = mc.workspace(update=True)
  154. return result
  155. # -------------------------------------------------------------------------
  156. # install and init callbacks on an import obj
  157. _G_CALLBACKS = install_callbacks(_G_CALLBACKS)
  158. # ==========================================================================
  159. # Module Tests
  160. #==========================================================================
  161. if __name__ == '__main__':
  162. _G_CALLBACKS = install_callbacks(_G_CALLBACKS)