Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
Support OpenPype icon override in Maya Toolbox in Maya 2022+
Browse files Browse the repository at this point in the history
- Also refactor import maya.cmds as `mc` to `cmds` to match with other code in the OpenPype code base
  • Loading branch information
BigRoy committed Dec 28, 2021
1 parent 74cfde5 commit d090655
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions openpype/hosts/maya/api/customize.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from functools import partial

import maya.cmds as mc
import maya.cmds as cmds
import maya.mel as mel

from avalon.maya import pipeline
Expand All @@ -31,9 +31,9 @@ def override_component_mask_commands():
log.info("Installing override_component_mask_commands..")

# Get all object mask buttons
buttons = mc.formLayout("objectMaskIcons",
query=True,
childArray=True)
buttons = cmds.formLayout("objectMaskIcons",
query=True,
childArray=True)
# Skip the triangle list item
buttons = [btn for btn in buttons if btn != "objPickMenuLayout"]

Expand All @@ -44,14 +44,14 @@ def on_changed_callback(raw_command, state):
# toggle the others based on whether any of the buttons
# was remaining active after the toggle, if not then
# enable all
if mc.getModifiers() == 4: # = CTRL
if cmds.getModifiers() == 4: # = CTRL
state = True
active = [mc.iconTextCheckBox(btn, query=True, value=True) for btn
in buttons]
active = [cmds.iconTextCheckBox(btn, query=True, value=True)
for btn in buttons]
if any(active):
mc.selectType(allObjects=False)
cmds.selectType(allObjects=False)
else:
mc.selectType(allObjects=True)
cmds.selectType(allObjects=True)

# Replace #1 with the current button state
cmd = raw_command.replace(" #1", " {}".format(int(state)))
Expand All @@ -64,32 +64,43 @@ def on_changed_callback(raw_command, state):
# try to implement the fix. (This also allows us to
# "uninstall" the behavior later)
if btn not in COMPONENT_MASK_ORIGINAL:
original = mc.iconTextCheckBox(btn, query=True, cc=True)
original = cmds.iconTextCheckBox(btn, query=True, cc=True)
COMPONENT_MASK_ORIGINAL[btn] = original

# Assign the special callback
original = COMPONENT_MASK_ORIGINAL[btn]
new_fn = partial(on_changed_callback, original)
mc.iconTextCheckBox(btn, edit=True, cc=new_fn)
cmds.iconTextCheckBox(btn, edit=True, cc=new_fn)


def override_toolbox_ui():
"""Add custom buttons in Toolbox as replacement for Maya web help icon."""
icons = resources.get_resource("icons")

# Ensure the maya web icon on toolbox exists
web_button = "ToolBox|MainToolboxLayout|mayaWebButton"
if not mc.iconTextButton(web_button, query=True, exists=True):
maya_version = int(cmds.about(version=True))
if maya_version >= 2022:
# Maya 2022+ has an updated toolbox with a different web
# button name and type
web_button = "ToolBox|MainToolboxLayout|mayaHomeToolboxButton"
button_fn = cmds.iconTextStaticLabel
else:
web_button = "ToolBox|MainToolboxLayout|mayaWebButton"
button_fn = cmds.iconTextButton

if not button_fn(web_button, query=True, exists=True):
# Button does not exist
log.warning("Can't find Maya Home/Web button to override toolbox ui..")
return

mc.iconTextButton(web_button, edit=True, visible=False)
button_fn(web_button, edit=True, visible=False)

# real = 32, but 36 with padding - according to toolbox mel script
icon_size = 36
parent = web_button.rsplit("|", 1)[0]

# Ensure the parent is a formLayout
if not mc.objectTypeUI(parent) == "formLayout":
if not cmds.objectTypeUI(parent) == "formLayout":
return

# Create our controls
Expand All @@ -106,7 +117,7 @@ def override_toolbox_ui():

if look_assigner is not None:
controls.append(
mc.iconTextButton(
cmds.iconTextButton(
"pype_toolbox_lookmanager",
annotation="Look Manager",
label="Look Manager",
Expand All @@ -120,7 +131,7 @@ def override_toolbox_ui():
)

controls.append(
mc.iconTextButton(
cmds.iconTextButton(
"pype_toolbox_workfiles",
annotation="Work Files",
label="Work Files",
Expand All @@ -136,7 +147,7 @@ def override_toolbox_ui():
)

controls.append(
mc.iconTextButton(
cmds.iconTextButton(
"pype_toolbox_loader",
annotation="Loader",
label="Loader",
Expand All @@ -152,7 +163,7 @@ def override_toolbox_ui():
)

controls.append(
mc.iconTextButton(
cmds.iconTextButton(
"pype_toolbox_manager",
annotation="Inventory",
label="Inventory",
Expand All @@ -173,7 +184,7 @@ def override_toolbox_ui():
for i, control in enumerate(controls):
previous = controls[i - 1] if i > 0 else web_button

mc.formLayout(parent, edit=True,
attachControl=[control, "bottom", 0, previous],
attachForm=([control, "left", 1],
[control, "right", 1]))
cmds.formLayout(parent, edit=True,
attachControl=[control, "bottom", 0, previous],
attachForm=([control, "left", 1],
[control, "right", 1]))

0 comments on commit d090655

Please sign in to comment.