set_callbacks.py 7.3 KB

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