running_state.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 logging as _logging
  15. # -- External Python modules --
  16. # none
  17. # -- Extension Modules --
  18. # none (yet)
  19. # --------------------------------------------------------------------------
  20. # -- Global Definitions --
  21. _DCCSI_G_DCC_APP = None
  22. _MODULENAME = 'azpy.dev.utils.check.running_state'
  23. _LOGGER = _logging.getLogger(_MODULENAME)
  24. _LOGGER.debug('Initializing: {0}.'.format({_MODULENAME}))
  25. # -------------------------------------------------------------------------
  26. # -------------------------------------------------------------------------
  27. # First Class
  28. class CheckRunningState(object):
  29. """
  30. < To Do: document Class >
  31. """
  32. # Class Variables
  33. DCCSI_G_DCC_APP = None
  34. def __init__(self, *args, **kwargs):
  35. '''
  36. CheckRunningState Class Initialization
  37. < To Do: Need to document >
  38. Input Attributes:
  39. -----------------
  40. self. -> SCALAR: Description.
  41. Default =
  42. Keyword Arguments:
  43. ------------------
  44. self. -> STRING: Description.
  45. Default =
  46. self. -> OBJECT: Description.
  47. Default =
  48. Additional Attributes:
  49. ----------------------
  50. self. -> BOOLEAN: Description.
  51. Default =
  52. Documentation last updated: Month. Day, Year - Author
  53. '''
  54. # -- Default Values --
  55. # top level storage for whether or not we are running
  56. # in a DCC tool interpreter
  57. self._dcc_py = False
  58. # basic python info
  59. # if these can't run, we are in a bad state anyway
  60. self._python = sys.version
  61. self._py_version_info = sys.version_info
  62. # -- Input Checks --
  63. self.check_known()
  64. # ---------------------------------------------------------------------
  65. # --method-------------------------------------------------------------
  66. def check_known(self):
  67. # -- init --
  68. # first let's check if any of these DCC apps are running
  69. # To Do?: Add O3DE checks, treat as a DCC app?
  70. # 0 - maya first
  71. CheckRunningState.DCCSI_G_DCC_APP = self.maya_running()
  72. # 1 - then max
  73. if not CheckRunningState.DCCSI_G_DCC_APP:
  74. CheckRunningState.DCCSI_G_DCC_APP = self.max_running()
  75. else:
  76. _LOGGER.warning('DCCSI_G_DCC_APP is already set: {}'.format(CheckRunningState.DCCSI_G_DCC_APP))
  77. # 2 - then blender
  78. if not CheckRunningState.DCCSI_G_DCC_APP:
  79. CheckRunningState.DCCSI_G_DCC_APP = self.blender_running()
  80. else:
  81. _LOGGER.warning('DCCSI_G_DCC_APP is already set: {}'.format(CheckRunningState.DCCSI_G_DCC_APP))
  82. # store checks for DCC info
  83. if CheckRunningState.DCCSI_G_DCC_APP:
  84. self.dcc_py = True
  85. # store check for is maya running headless
  86. if CheckRunningState.DCCSI_G_DCC_APP == 'maya':
  87. self.maya_headless = self.is_maya_headless()
  88. # set a envar other modules can easily check
  89. if CheckRunningState.DCCSI_G_DCC_APP:
  90. os.environ['DCCSI_G_DCC_APP'] = CheckRunningState.DCCSI_G_DCC_APP
  91. # ---------------------------------------------------------------------
  92. #--properties----------------------------------------------------------
  93. @property
  94. def python(self):
  95. return self._python
  96. @python.setter
  97. def python(self, value):
  98. self._python = value
  99. return self._python
  100. @property
  101. def py_version_info(self):
  102. return self._py_version_info
  103. @py_version_info.setter
  104. def py_version_info(self, value):
  105. self._py_version_info = value
  106. return self._py_version_info
  107. @property
  108. def dcc_app(self):
  109. return self._dcc_py
  110. @dcc_app.setter
  111. def dcc_app(self, value):
  112. self._dcc_py = value
  113. return self._dcc_py
  114. @property
  115. def maya_headless(self):
  116. return self._maya_headless
  117. @maya_headless.setter
  118. def maya_headless(self, value):
  119. self._maya_headless = value
  120. return self._maya_headless
  121. # template property
  122. # @property
  123. # def foo(self):
  124. # return self._foo
  125. # @foo.setter
  126. # def foo(self, value):
  127. #self._foo = value
  128. # return self._foo
  129. # --properties----------------------------------------------------------
  130. # --method-------------------------------------------------------------
  131. def maya_running(self):
  132. """< To Do: Need to document >"""
  133. try:
  134. import azpy.dev.utils.check.maya_app as check_dcc
  135. DCCSI_G_DCC_APP = check_dcc.validate_state()
  136. except ImportError as e:
  137. _LOGGER.info('Not Implemented: azpy.dev.utils.check.maya_app')
  138. if DCCSI_G_DCC_APP:
  139. CheckRunningState.DCCSI_G_DCC_APP = check_dcc.validate_state()
  140. os.environ["DCCSI_G_DCC_APP"] = str(DCCSI_G_DCC_APP)
  141. return CheckRunningState.DCCSI_G_DCC_APP
  142. #----------------------------------------------------------------------
  143. # --method-------------------------------------------------------------
  144. def is_maya_headless(self):
  145. """< To Do: Need to document >"""
  146. if self.maya_app:
  147. import maya.cmds as mc
  148. try:
  149. if mc.about(batch=True):
  150. return True
  151. else:
  152. return False
  153. except Exception as e:
  154. # cmds module isn't fully loaded/populated
  155. # (which only happens in batch, maya.standalone, or maya GUI)
  156. # no maya
  157. return False
  158. else:
  159. return False
  160. # --method-------------------------------------------------------------
  161. # --method-------------------------------------------------------------
  162. def max_running(self):
  163. """
  164. < To Do: implement >
  165. """
  166. try:
  167. import azpy.dev.utils.check.max_app as check_dcc
  168. CheckRunningState.DCCSI_G_DCC_APP = check_dcc.validate_state()
  169. except ImportError as e:
  170. _LOGGER.info('Not Implemented: azpy.dev.utils.check.max')
  171. if CheckRunningState.DCCSI_G_DCC_APP:
  172. CheckRunningState.DCCSI_G_DCC_APP = check_dcc.validate_state()
  173. os.environ["DCCSI_G_DCC_APP"] = str(CheckRunningState.DCCSI_G_DCC_APP)
  174. return CheckRunningState.DCCSI_G_DCC_APP
  175. #----------------------------------------------------------------------
  176. # --method-------------------------------------------------------------
  177. def blender_running(self):
  178. """
  179. < To Do: implement >
  180. """
  181. try:
  182. import azpy.dev.utils.check.blender_app as check_dcc
  183. CheckRunningState.DCCSI_G_DCC_APP = check_dcc.validate_state()
  184. except ImportError as e:
  185. _LOGGER.info('Not Implemented: azpy.dev.utils.check.blender')
  186. if CheckRunningState.DCCSI_G_DCC_APP:
  187. CheckRunningState.DCCSI_G_DCC_APP = check_dcc.validate_state()
  188. os.environ["DCCSI_G_DCC_APP"] = str(CheckRunningState.DCCSI_G_DCC_APP)
  189. return CheckRunningState.DCCSI_G_DCC_APP
  190. #----------------------------------------------------------------------
  191. #==========================================================================
  192. # Class Test
  193. #==========================================================================
  194. if __name__ == '__main__':
  195. _DCCSI_GDEBUG = True
  196. _DCCSI_DEV_MODE = True
  197. _LOGGER.setLevel(_logging.DEBUG) # force debugging
  198. # -- Extend Logger
  199. #_handler = _logging.StreamHandler(sys.stdout)
  200. # _handler.setLevel(_logging.DEBUG)
  201. #FRMT_LOG_LONG = "[%(name)s][%(levelname)s] >> %(message)s (%(asctime)s; %(filename)s:%(lineno)d)"
  202. #_formatter = _logging.Formatter(FRMT_LOG_LONG)
  203. # _handler.setFormatter(_formatter)
  204. # _LOGGER.addHandler(_handler)
  205. #_LOGGER.debug('Loading: {0}.'.format({_MODULENAME}))
  206. # happy print
  207. from azpy.constants import STR_CROSSBAR
  208. _LOGGER.info(STR_CROSSBAR)
  209. _LOGGER.info('{} ... Running script as __main__'.format(_MODULENAME))
  210. _LOGGER.info(STR_CROSSBAR)
  211. foo = CheckRunningState()
  212. _LOGGER.info('DCCSI_G_DCC_APP: {}'.format(foo.DCCSI_G_DCC_APP))