Q100108: Add custom menu items to the context menu of nodes

Follow

SUMMARY

This article describes how to add custom menu items to the context menu (right click menu) of nodes within Katana.

 

MORE INFORMATION

You can add custom menu items to the right click / context menu of a node, by using the NodeActionDelegate module Katana provides. 
 
Here is example showing how you can register menu actions for the context menu of a given node, to select and deselect nodes:
 
from Katana import UI4, NodegraphAPI
from UI4.FormMaster.NodeActionDelegate import (BaseNodeActionDelegate, RegisterActionDelegate)
from UI4.Manifest import QtCore, QtGui

class MyNodeActionDelegate(BaseNodeActionDelegate.BaseNodeActionDelegate):
    class _SelectNodeAction(QtGui.QAction):

        def __init__(self, parent, node):
            QtGui.QAction.__init__(self, 'Select "%s"' % node.getName(), parent)

            self.__node = node
            if node:
                QtCore.QObject.connect(self, QtCore.SIGNAL('triggered(bool)'),self.__triggered)
            self.setEnabled(self.__node is not None
                            and not self.__node.isLocked(True))

        def __triggered(self, checked):
            NodegraphAPI.SetNodeSelected(self.__node, True)

    class _DeselectNodeAction(QtGui.QAction):

        def __init__(self, parent, node):
            QtGui.QAction.__init__(self, 'Deselect "%s"' % node.getName(), parent)

            self.__node = node
            if node:
                QtCore.QObject.connect(self, QtCore.SIGNAL('triggered(bool)'), self.__triggered)
            self.setEnabled(self.__node is not None
                            and not self.__node.isLocked(True))

        def __triggered(self, checked):
            NodegraphAPI.SetNodeSelected(self.__node, False)

    def addToContextMenu(self, menu, node):
        menu.addAction(self._SelectNodeAction(menu, node))
        menu.addAction(self._DeselectNodeAction(menu, node))
    
    def addToWrenchMenu(self, menu, node, hints=None):
        pass


RegisterActionDelegate("CameraCreate", MyNodeActionDelegate())
 

This will add two custom menu commands to the context menu of CameraCreate nodes under the 'CameraCreate' line header.

 

If you'd like to add the same menu commands to the context menus of all the types of nodes, you can use something like this:

 

myNodeActionDelegate = MyNodeActionDelegate()
for nodeTypeName in NodegraphAPI.GetNodeTypes():
    RegisterActionDelegate(nodeTypeName, myNodeActionDelegate)

 

You can also register the same node action delegate against specific node types, as shown in the following example:

myNodeActionDelegate = MyNodeActionDelegate()
for nodeTypeName in ['AttributeSet', 'CameraCreate', 'OpScript']:
    RegisterActionDelegate(nodeTypeName, myNodeActionDelegate)

 

 


Keywords: Katana, custom menu, right click menu, context menu, NodeActionDelegate

Was this article helpful?
1 out of 1 found this helpful

Comments