创建支持服务单
关注

Q100108:将自定义菜单项添加到节点的上下文菜单中

摘要

本文介绍如何将自定义菜单项添加到Katana中节点的上下文菜单(右键菜单)。

更多信息

您可以使用Katana提供的NodeActionDelegate模块将自定义菜单项添加到节点的右键单击/上下文菜单中。
下面的示例显示如何为给定节点的上下文菜单注册菜单操作,以选择和取消选择节点:
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())

这将在“CameraCreate”行标题下的CameraCreate节点的上下文菜单中添加两个自定义菜单命令。

如果您想将相同的菜单命令添加到所有类型节点的上下文菜单中,您可以使用以下内容:

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

您还可以针对特定节点类型注册相同的节点操作委托,如以下示例所示:

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

 


关键字: Katana ,自定义菜单,右键菜单,上下文菜单,NodeActionDelegate

这篇文章有帮助吗?
/

We're sorry to hear that!

Please tell us why.
4 人中有 4 人觉得有帮助

评论