Python API 2.0 Reference
python/api2/demoSequencerCustomMenuCallbacks.py
1 import maya.cmds as cmds
2 import maya.api.OpenMaya as om
3 
4 PLUGIN_NAME = "demoSequencerCustomMenuCallbacks"
5 
6 def shot_count():
7  """
8  Get the total number of shots in the sequencer
9  """
10  shots = cmds.ls(type="shot")
11  return len(shots)
12 
13 def demo_top_menu_callback(editor):
14  """
15  Function to add a custom menu to the sequencer UI.
16  This function will be called by Maya when building the sequencer's top menu.
17  This example adds a menu item that displays the total shot count when selected.
18  Parameters:
19  editor (str): The name of the Sequencer editor where the menu is being built.
20  """
21  print("Adding custom menu to editor: {}".format(editor))
22  cmds.menu(label="Demo Menu")
23  cmds.menuItem(label="Shot Count", command=lambda *args: om.MGlobal.displayInfo("Total Shots: {}".format(shot_count())))
24 
25 def demo_toolbar_buttons_callback(editor):
26  """
27  Function to add custom toolbar buttons to the sequencer UI.
28  This function will be called by Maya when building the sequencer's toolbar.
29  This example adds a button that displays the total shot count when clicked.
30  Parameters:
31  editor (str): The name of the Sequencer editor where the toolbar is being built.
32  """
33  print("Adding custom toolbar buttons to editor: {}".format(editor))
34  cmds.separator(style="toolbar", horizontal=False, height=20)
35  cmds.iconTextButton(ann="Shot Count", label="Shot Count", image1="item_add.png", command=lambda *args: om.MGlobal.displayInfo("Total Shots: {}".format(shot_count())))
36 
37 def demo_context_menu_callback(editor, parent_menu, context, id, track_type):
38  """
39  Function to add custom right-click menu items to shots in the sequencer.
40  This function will be called by Maya when building the context menu for shots in the sequencer.
41  This example adds a menu item that displays information about the context of the menu.
42  Parameters:
43  editor (str): The name of the Sequencer editor where the context menu is being built.
44  parent_menu (str): The parent context menu to which new menu items should be added.
45  context (str): The context in which the right-click occurred. See cameraSequencer menuContext flag documentation. Possible values include:
46  - "timeSlider": Right-clicked on the time slider.
47  - "nothing": Right-clicked outside of any specific UI element.
48  - "clip": Right-clicked on a clip.
49  - "audio": Right-clicked on an audio element.
50  - "track": Right-clicked on a track.
51  id (str): The identifier of the item under the cursor (e.g., clip name or track index), if applicable.
52  track_type (str): The type of track if in a track context. Can be "audio" or empty, indicating a sequence track.
53  """
54  cmds.setParent(parent_menu, menu=True)
55  cmds.menuItem(divider=True)
56 
57  # Determine what the context is and create menu items accordingly.
58  # For this example, we'll just show some info based on the current context
59  message = ""
60  match context:
61  case "timeSlider":
62  message = "Right-clicked on Time Slider"
63  case "nothing":
64  message = "Outside of any specific UI element"
65  case "clip":
66  message = f"Right-clicked on the clip: {id}"
67  case "audio":
68  message = f"Right-clicked on the audio: {id}"
69  case "track":
70  track_type_label = "audio" if track_type == "audio" else "sequence"
71  message = f"Right-clicked on {track_type_label} track number {id}"
72 
73  cmds.menuItem(label=message, enable=False)
74 
75 def register_ui_callbacks():
76  """
77  Register UI callbacks using Maya's callbacks command.
78  This function will be called when the plugin is initialized to ensure that
79  the custom menus and toolbar buttons are added to the sequencer UI.
80  Each callback is associated with a specific hook that Maya will call at the
81  appropriate time when building the sequencer UI.
82  The owner parameter is used to group callbacks together, allowing for easy
83  removal when the plugin is uninitialized.
84  """
85  # Register the callback for adding custom menus to the sequencer
86  cmds.callbacks(addCallback=demo_top_menu_callback,
87  hook="sequencerAddMenus",
88  owner=PLUGIN_NAME)
89 
90  # Register the callback for adding custom toolbar buttons to the sequencer
91  cmds.callbacks(addCallback=demo_toolbar_buttons_callback,
92  hook="sequencerAddToolButtons",
93  owner=PLUGIN_NAME)
94 
95  # Register the callback for adding custom right-click menu items to shots in the sequencer
96  cmds.callbacks(addCallback=demo_context_menu_callback,
97  hook="sequencerAddRMBMenuItems",
98  owner=PLUGIN_NAME)
99 
100 def clear_ui_callbacks():
101  """
102  Clear all callbacks registered by this plugin.
103  This function will be called when the plugin is uninitialized but the
104  sequencer must be reopened before the added UI elements are removed.
105  """
106  cmds.callbacks(clearCallbacks=True, owner=PLUGIN_NAME)
107 
108 def initializePlugin(mobject):
109  try:
110  register_ui_callbacks()
111  except Exception as e:
112  om.MGlobal.displayError("Failed to register plugin: " + str(e))
113  raise
114 
115 def uninitializePlugin(mobject):
116  try:
117  clear_ui_callbacks()
118  except Exception as e:
119  om.MGlobal.displayError("Failed to uninitialize plugin: " + str(e))
120 
121 if __name__ == '__main__':
122  register_ui_callbacks()