3
0

maya_app.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. # -- Standard Python modules --
  12. import sys
  13. import os
  14. import inspect
  15. import logging as _logging
  16. # -- External Python modules --
  17. # none
  18. # -- Extension Modules --
  19. import azpy
  20. from azpy.env_bool import env_bool
  21. from azpy.constants import ENVAR_DCCSI_GDEBUG
  22. from azpy.constants import ENVAR_DCCSI_DEV_MODE
  23. # --------------------------------------------------------------------------
  24. # -- Global Definitions --
  25. _DCCSI_G_DCC_APP = None
  26. # set up global space, logging etc.
  27. _DCCSI_GDEBUG = env_bool(ENVAR_DCCSI_GDEBUG, False)
  28. _DCCSI_DEV_MODE = env_bool(ENVAR_DCCSI_DEV_MODE, False)
  29. _MODULENAME = 'azpy.dev.utils.check.maya_app'
  30. _LOGGER = _logging.getLogger(_MODULENAME)
  31. # -------------------------------------------------------------------------
  32. ###########################################################################
  33. ## These mini-functions need to be defined, before they are called
  34. # -------------------------------------------------------------------------
  35. # run this, if we are in Maya
  36. def set_dcc_app(dcc_app='maya'):
  37. """
  38. azpy.dev.utils.check.maya.set_dcc_app()
  39. this will set global _DCCSI_G_DCC_APP = 'maya'
  40. and os.environ["DCCSI_G_DCC_APP"] = 'maya'
  41. """
  42. _DCCSI_G_DCC_APP = dcc_app
  43. _LOGGER.info('Setting DCCSI_G_DCC_APP to: {0}'.format(dcc_app))
  44. return _DCCSI_G_DCC_APP
  45. # -------------------------------------------------------------------------
  46. # -------------------------------------------------------------------------
  47. def clear_dcc_app(dcc_app=False):
  48. """
  49. azpy.dev.utils.check.maya.set_dcc_app()
  50. this will set global _DCCSI_G_DCC_APP = False
  51. and os.environ["DCCSI_G_DCC_APP"] = False
  52. """
  53. _DCCSI_G_DCC_APP = dcc_app
  54. _LOGGER.info('Setting DCCSI_G_DCC_APP to: {0}'.format(dcc_app))
  55. return _DCCSI_G_DCC_APP
  56. # -------------------------------------------------------------------------
  57. # -------------------------------------------------------------------------
  58. def validate_state(DCCSI_G_DCC_APP=_DCCSI_G_DCC_APP):
  59. '''
  60. This will detect if we are running in Maya or not,
  61. then will call either, set_dcc_app('maya') or clear_dcc_app(dcc_app=False)
  62. '''
  63. if _DCCSI_GDEBUG:
  64. _LOGGER.debug(autolog())
  65. try:
  66. import maya.cmds as cmds
  67. DCCSI_G_DCC_APP = set_dcc_app('maya')
  68. except ImportError as e:
  69. _LOGGER.warning('Can not perform: import maya.cmds as cmds')
  70. DCCSI_G_DCC_APP = clear_dcc_app()
  71. else:
  72. try:
  73. if cmds.about(batch=True):
  74. DCCSI_G_DCC_APP = set_dcc_app('maya')
  75. except AttributeError as e:
  76. _LOGGER.warning("maya.cmds module isn't fully loaded/populated, "
  77. "(cmds populates only in batch, maya.standalone, or maya GUI)")
  78. # NO Maya
  79. DCCSI_G_DCC_APP=clear_dcc_app()
  80. return DCCSI_G_DCC_APP
  81. # -------------------------------------------------------------------------
  82. # -------------------------------------------------------------------------
  83. def autolog():
  84. '''Automatically log the current function details.'''
  85. # Get the previous frame in the stack, otherwise it would
  86. # be this function!!!
  87. func = inspect.currentframe().f_back.f_back.f_code
  88. # Dump the message + the name of this function to the log.
  89. output = ('{module} AUTOLOG:\r'
  90. 'Called from::\n{0}():\r'
  91. 'In file: {1},\r'
  92. 'At line: {2}\n'
  93. ''.format(func.co_name,
  94. func.co_filename,
  95. func.co_firstlineno,
  96. module=_MODULENAME))
  97. return output
  98. #-------------------------------------------------------------------------
  99. # -------------------------------------------------------------------------
  100. # run the check on import
  101. _DCCSI_G_DCC_APP = validate_state()
  102. # -------------------------------------------------------------------------
  103. ###########################################################################
  104. # Main Code Block, runs this script as main (testing)
  105. # -------------------------------------------------------------------------
  106. if __name__ == '__main__':
  107. # there are not really tests to run here due to this being a list of
  108. # constants for shared use.
  109. _DCCSI_GDEBUG = True
  110. _DCCSI_DEV_MODE = True
  111. _LOGGER.setLevel(_logging.DEBUG) # force debugging
  112. ## reduce cyclical azpy imports
  113. ## it only has a basic logger configured, add log to console
  114. #_handler = _logging.StreamHandler(sys.stdout)
  115. #_handler.setLevel(_logging.DEBUG)
  116. #FRMT_LOG_LONG = "[%(name)s][%(levelname)s] >> %(message)s (%(asctime)s; %(filename)s:%(lineno)d)"
  117. #_formatter = _logging.Formatter(FRMT_LOG_LONG)
  118. #_handler.setFormatter(_formatter)
  119. #_LOGGER.addHandler(_handler)
  120. #_LOGGER.debug('Loading: {0}.'.format({_MODULENAME}))
  121. # happy print
  122. from azpy.constants import STR_CROSSBAR
  123. _LOGGER.info(STR_CROSSBAR)
  124. _LOGGER.info('{} ... Running script as __main__'.format(_MODULENAME))
  125. _LOGGER.info(STR_CROSSBAR)
  126. _DCCSI_G_DCC_APP = validate_state()
  127. _LOGGER.info('Is Maya Running? _DCCSI_G_DCC_APP = {}'.format(_DCCSI_G_DCC_APP))