3
0

set_shelf.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_shelf.py
  14. This module manages a custom shelf in maya for the DCCsi
  15. Reference: https://gist.github.com/vshotarov/1c3176fe9e38dcaadd1e56c2f15c95d9
  16. """
  17. # -------------------------------------------------------------------------
  18. # -- Standard Python modules
  19. import logging as _logging
  20. # -- External Python modules
  21. # none
  22. # -- DCCsi Extension Modules
  23. # none
  24. # -- Maya Extension Modules
  25. import maya.cmds as mc
  26. # -------------------------------------------------------------------------
  27. # -------------------------------------------------------------------------
  28. # global scope
  29. from DccScriptingInterface.Tools.DCC.Maya import _PACKAGENAME
  30. _MODULENAME = f'{_PACKAGENAME}.set_shelf'
  31. _LOGGER = _logging.getLogger(_MODULENAME)
  32. _LOGGER.info(f'Initializing: {_MODULENAME}')
  33. from DccScriptingInterface.globals import *
  34. # -------------------------------------------------------------------------
  35. def _null(*args):
  36. pass
  37. # -------------------------------------------------------------------------
  38. class customShelf(_Custom_Shelf):
  39. '''This is an example shelf.'''
  40. def build(self):
  41. self.add_button(label="button1")
  42. self.add_button("button2")
  43. self.add_button("popup")
  44. p = mc.popupMenu(b=1)
  45. self.add_menu_item(p, "popupMenuItem1")
  46. self.add_menu_item(p, "popupMenuItem2")
  47. sub = self.add_submenu(p, "subMenuLevel1")
  48. self.add_menu_item(sub, "subMenuLevel1Item1")
  49. sub2 = self.add_submenu(sub, "subMenuLevel2")
  50. self.add_menu_item(sub2, "subMenuLevel2Item1")
  51. self.add_menu_item(sub2, "subMenuLevel2Item2")
  52. self.add_menu_item(sub, "subMenuLevel1Item2")
  53. self.add_menu_item(p, "popupMenuItem3")
  54. self.add_button("button3")
  55. # -------------------------------------------------------------------------
  56. class _Custom_Shelf():
  57. '''A simple class to build custom shelves in maya.
  58. The build method is empty and an inheriting class should override'''
  59. def __init__(self, name="DCCsi", icon_path=""):
  60. self._name = name
  61. self._icon_path = icon_path
  62. self._label_background_color = (0, 0, 0, 0)
  63. self._label_colour = (.9, .9, .9)
  64. self._clean_old_shlef()
  65. mc.setParent(self._name)
  66. self.build()
  67. def build(self):
  68. '''Override this method in custom class.
  69. Otherwise, nothing is added to the shelf.'''
  70. pass
  71. def add_button(self,
  72. label='<NotSet>',
  73. icon="commandButton.png",
  74. command=_null,
  75. doubleCommand=_null):
  76. '''Adds a shelf button with the specified label,
  77. command, double click command and image.'''
  78. mc.setParent(self._name)
  79. if icon:
  80. icon = self._icon_path + icon
  81. mc.shelfButton(width=37, height=37,
  82. image=icon,
  83. label=label,
  84. command=command,
  85. doubleClickCommand=doubleCommand,
  86. imageOverlayLabel=label,
  87. overlayLabelBackColor=self._label_background_color,
  88. overlayLabelColor=self._label_colour)
  89. def add_menu_item(self, parent, label, command=_null, icon=""):
  90. '''Adds a shelf button with the specified label,
  91. command, double click command and image.'''
  92. if icon:
  93. icon = self._icon_path + icon
  94. return mc.menuItem(p=parent, l=label, c=command, i="")
  95. def add_submenu(self, parent, label, icon=None):
  96. '''Adds a sub menu item with the specified label and icon
  97. to the specified parent popup menu.'''
  98. if icon:
  99. icon = self._icon_path + icon
  100. return mc.menuItem(p=parent, l=label, i=icon, subMenu=1)
  101. def _clean_old_shlef(self):
  102. '''Checks if the shelf exists and empties it if it does or
  103. creates it if it does not.'''
  104. if mc.shelfLayout(self._name, ex=1):
  105. if mc.shelfLayout(self._name, q=1, ca=1):
  106. for each in mc.shelfLayout(self._name, q=1, ca=1):
  107. mc.deleteUI(each)
  108. else:
  109. mc.shelfLayout(self._name, p="ShelfLayout")
  110. # ==========================================================================
  111. # Module Tests
  112. # ==========================================================================
  113. if __name__ == '__main__':
  114. customShelf()
  115. pass