set_menu.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. """This module creates a DccScriptingInterface menu in Lumberyard.
  12. This module is designed to only run in Lumberyard and py3.7+"""
  13. # -- Standard Python modules
  14. import sys
  15. import os
  16. import importlib.util
  17. # -- External Python modules
  18. from pathlib import Path
  19. # -- DCCsi Extension Modules
  20. import azpy
  21. # Lumberyard extension modules
  22. import azlmbr
  23. import azlmbr.bus
  24. # -------------------------------------------------------------------------
  25. # -------------------------------------------------------------------------
  26. # some basic DCCsi module setup, DCCsi bootstrap already performed some
  27. from dynaconf import settings
  28. settings.setenv()
  29. # -------------------------------------------------------------------------
  30. _log_level = int(settings.DCCSI_LOGLEVEL)
  31. if settings.DCCSI_GDEBUG:
  32. _log_level = int(10) # force debug level
  33. _MODULENAME = r'DCCsi.SDK.Lumberyard.Scripts.set_menu'
  34. _LOGGER = azpy.initialize_logger(_MODULENAME, default_log_level=_log_level)
  35. _LOGGER.debug(f'Invoking:: {0}.'.format({_MODULENAME}))
  36. # early attach WingIDE debugger (can refactor to include other IDEs later)
  37. if settings.DCCSI_DEV_MODE:
  38. from azpy.env_bool import env_bool
  39. if not env_bool('DCCSI_DEBUGGER_ATTACHED', False):
  40. # if not already attached lets do it here
  41. from azpy.test.entry_test import connect_wing
  42. foo = connect_wing()
  43. # -------------------------------------------------------------------------
  44. # -------------------------------------------------------------------------
  45. # validate pyside before continuing
  46. try:
  47. azlmbr.qt.QtForPythonRequestBus(azlmbr.bus.Broadcast, 'IsActive')
  48. params = azlmbr.qt.QtForPythonRequestBus(azlmbr.bus.Broadcast, 'GetQtBootstrapParameters')
  49. params is not None and params.mainWindowId != 0
  50. from PySide2 import QtWidgets
  51. except Exception as e:
  52. _LOGGER.error(f'Pyside not available, exception: {e}')
  53. raise e
  54. # keep going, import the other PySide2 bits we will use
  55. from PySide2 import QtGui
  56. from PySide2.QtCore import Slot
  57. from shiboken2 import wrapInstance, getCppPointer
  58. # -------------------------------------------------------------------------
  59. # -------------------------------------------------------------------------
  60. def get_ly_mainwindow():
  61. widget_main_window = QtWidgets.QWidget.find(params.mainWindowId)
  62. widget_main_window = wrapInstance(int(getCppPointer(widget_main_window)[0]), QtWidgets.QMainWindow)
  63. return widget_main_window
  64. # -------------------------------------------------------------------------
  65. # -------------------------------------------------------------------------
  66. class SampleUI(QtWidgets.QDialog):
  67. """Lightweight UI Test Class created a button"""
  68. def __init__(self, parent, title='Not Set'):
  69. super(SampleUI, self).__init__(parent)
  70. self.setWindowTitle(title)
  71. self.initUI()
  72. def initUI(self):
  73. mainLayout = QtWidgets.QHBoxLayout()
  74. testBtn = QtWidgets.QPushButton("I am just a Button man!")
  75. mainLayout.addWidget(testBtn)
  76. self.setLayout(mainLayout)
  77. # -------------------------------------------------------------------------
  78. # -------------------------------------------------------------------------
  79. _tag_str = 'DccScriptingInterface'
  80. _LOGGER.info(f'Creating {_tag_str} Menu')
  81. # Lumberyards main window
  82. widget_main_window = get_ly_mainwindow()
  83. # create our own menuBar
  84. dccsi_menu = widget_main_window.menuBar().addMenu(f"&{_tag_str}")
  85. # nest a menu for util/tool launching
  86. dccsi_launch_menu = dccsi_menu.addMenu("Launch Utility")
  87. # (1) add the first utility: Substance Builder
  88. # to do, make adding this a data-driven dynaconf option configured per-project?
  89. action_launch_sub_builder = dccsi_launch_menu.addAction("Substance Builder")
  90. @Slot()
  91. def clicked_launch_sub_builder():
  92. # breadcrumbs
  93. if settings.DCCSI_GDEBUG:
  94. debug_msg = "Clicked action_launch_sub_builder"
  95. print(debug_msg)
  96. _LOGGER.debug(debug_msg)
  97. _SUB_BUILDER_PATH = Path(settings.PATH_DCCSIG,
  98. 'SDK',
  99. 'Substance',
  100. 'builder')
  101. _SUB_BUILDER_BOOTSTRAP = Path(_SUB_BUILDER_PATH, 'bootstrap.py')
  102. # bootstrap is a generic name and we have many of them
  103. # this ensures we are loading a specific one
  104. _spec_bootstrap = importlib.util.spec_from_file_location("dccsi.sdk.substance.builder.bootstrap",
  105. _SUB_BUILDER_BOOTSTRAP)
  106. _app_bootstrap = importlib.util.module_from_spec(_spec_bootstrap)
  107. _spec_bootstrap.loader.exec_module(_app_bootstrap)
  108. while 1: # simple PySide2 test, set to 0 to disable
  109. ui = SampleUI(parent=widget_main_window, title='Atom: Substance Builder')
  110. ui.show()
  111. break
  112. return
  113. # Add click event to menu bar
  114. action_launch_sub_builder.triggered.connect(clicked_launch_sub_builder)
  115. # -----------------------------------------------------------------
  116. # add launching the dcc material converter util to the menu
  117. # to do, make adding this a data-driven dynaconf option?
  118. action_launch_dcc_mat_converter = dccsi_launch_menu.addAction("DCC Material Converter")
  119. @Slot()
  120. def clicked_launch_dcc_mat_converter():
  121. if settings.DCCSI_GDEBUG:
  122. debug_msg = "Clicked action_launch_dcc_mat_converter"
  123. print(debug_msg)
  124. _LOGGER.debug(debug_msg)
  125. while 1: # simple PySide2 test, set to 0 to disable
  126. ui = SampleUI(parent=widget_main_window, title='Atom: DCC Material Converter')
  127. ui.show()
  128. break
  129. return
  130. # Add click event to menu bar
  131. action_launch_dcc_mat_converter.triggered.connect(clicked_launch_dcc_mat_converter)