Skip to content

Instantly share code, notes, and snippets.

@LiamHz
Last active January 26, 2025 17:05
Show Gist options
  • Save LiamHz/b5ce2d43ddcb5de998036c38e556c063 to your computer and use it in GitHub Desktop.
Save LiamHz/b5ce2d43ddcb5de998036c38e556c063 to your computer and use it in GitHub Desktop.
"""
Creates a marking menu to switch the active camera between a list of focal lengths
Opens on Cmd+Shift+MMB
"""
import maya.cmds as mc
MENU_NAME = "focalLengthMarkingMenu"
class FocalLengthMarkingMenu:
def __init__(self):
self._remove_old_menu()
self._create_menu()
def _remove_old_menu(self):
if mc.popupMenu(MENU_NAME, exists=True):
mc.deleteUI(MENU_NAME)
def _create_menu(self):
mc.popupMenu(
MENU_NAME,
markingMenu=True,
button=2, # MMB
clt=True,
parent="viewPanes",
postMenuCommand=self._build_menu_content
)
def _build_menu_content(self, menu, parent):
focal_lengths = [
(25, "N"),
(35, "W"),
(50, "E"),
(70, "S")
]
for fl, position in focal_lengths:
mc.menuItem(
parent=menu,
label=f"{fl}mm",
radialPosition=position,
command=lambda _, fl=fl: self._set_focal_length(fl)
)
def _set_focal_length(self, focal_length):
current_panel = mc.getPanel(withFocus=True)
if mc.getPanel(typeOf=current_panel) == "modelPanel":
camera = mc.modelPanel(current_panel, query=True, camera=True)
mc.setAttr(f"{camera}.focalLength", focal_length)
# Create the marking menu
FocalLengthMarkingMenu()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment