set_shelf.py 4.3 KB

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