On this page |
This object will build QMenus based on an XML menu definition and will also simplify keyboard shortcut handling for activating items in the menu that have shortcuts assigned to them.
Script items like expressions and script code are passed kwargs that will contain (if defined) the id and label of the corresponding menu item.
Methods ¶
__init__(context, kwargs, xmlfile, xmlstring)
When creating the object, one of xmlstring
or xmlfile
must be provided.
If both are provided then only xmlstring will be used.
context
Hotkey context for this menu. This value is prepended to
id
values that do not contain '.'
kwargs
Additional kwargs items to be sent to all script code.
xmlstring
Optional XML Menu string to parse.
xmlfilename
Optional XML menu filename to find and parse.
parseString(xmlstring)
Parse an XML menu string and aggregate its contents into this menu.
xmlstring
A string containing valid XML.
setHotkeyContext(hotkey_context)
Sets or modifies the hotkey context for this XML menu.
hotkey_context
A string containing a valid hotkey context.
hotkeyContext()
→ str
Returns the current hotkey context used by this XML menu.
parseFile(xmlfile)
Parse an XML menu file and aggregate its contents into this menu. This should be a full path to the file as no search is performed to find the file.
xmlfile
A file name and path for a valid XML file.
parseFiles(xmlfilename)
Search HOUDINI_PATH for and parse XML menu files that match given filename and aggregate their contents into this menu.
xmlfilename
A filename for a valid XML file.
generateMenu(kwargs, menu, actionitem_callback)
→ hou.qt.Menu
Generate a QMenu hierarchy based on the already parsed XML menu definition. Functions to compute visibility, enabled, labels and the current value of toggle items are evaluated at this time.
kwargs
is appended to the kwargs passed to all functions and scripts in the XML file. This is the way to pass context related data to those functions, like the currently selected items.
actionitem_callback
A function to be invoked when 'ActionItem' extries are activaed. If this is None then any 'ActionItem' will be omitted from the menu as they cannot be activated.
handleKeyPress(keystring, kwargs, actionitem_callback)
→ bool
Look up and trigger menu actions based on their shortcut key assigned in the hotkey manager. Returns True if the keystring was handled, otherwise False.
keystring
A string representation of the key press equivalent to what is provided
by hou.qt.qtKeyToString()
kwargs
Is appended to the kwargs passed to all functions and scripts in the XML file. This is the way to pass context related data to those functions, like the currently selected items.
actionitem_callback
A function to be invoked when 'ActionItem' entries are activated. If this is None then any 'ActionItem' will be ignored as they cannot be activated.
Examples ¶
This example shows how you would use this object to preparse the XML file. Then on-demand generate a /hom/hou/qt/hou.qt.Menu.html which can be invoked within an event handler.
self._mymenuparser = XmlMenuParser(context = "h.pane.parms", xmlfile = "c:/temp/menu.xml") ... def contextMenuEvent(self, event): # custom kwargs passed to any expression or code in menu.xml when evaluating menukwargs["parm"] = menucontextparm menukwargs["parmname"] = menucontextparm.name() menu = self._mymenuparser.generateMenu(kwargs = menukwargs, actionitem_callback = self._processHotKeyAction) if not menu.isEmpty(): menu.popup(event.globalPos())
XML file:
<?xml version="1.0" encoding="UTF-8"?> <menuDocument> <menu> <actionItem id="set_keyframe"> <label>Set a Keyframe</label> </actionItem> <actionItem id="rem_keyframe"> <labelExpression>return kwargs["label"] + " - " + kwargs["parmname"]</labelExpression> </actionItem> <actionItem id="revert_defs"> </actionItem> <toggleItem> <label>Parm Locked</label> <enableExpression><![CDATA[ return kwargs['parm'] is not None ]]></enableExpression> <valueExpression><![CDATA[ return kwargs['parm'].isLocked() ]]></valueExpression> <scriptCode><![CDATA[ kwargs['parm'].lock(not kwargs['parm'].isLocked()) ]]></scriptCode> </toggleItem> <actionItem id="lock_parm"> <context><expression><![CDATA[ if kwargs['parm'] is not None and kwargs['parm'].isLocked(): return False return True ]]></expression></context> </actionItem> <actionItem id="unlock_parm"> <context><expression><![CDATA[ if kwargs['parm'] is not None and kwargs['parm'].isLocked(): return True return False ]]></expression></context> </actionItem> <separatorItem/> <subMenu> <label>Expressions</label> <actionItem id="toggle_expr"/> <actionItem id="edit_expression"/> <actionItem id="expand_values"/> <actionItem id="use_python"/> <actionItem id="use_old_language"/> </subMenu> </menu> </menuDocument>