Allowing to configure the VR menu

This plugin creates a new menu entry under “Scripts/VRMenu”. With this menu you can configure the visible tools in the VR menu.

VRMenuSetupModule.py
  1from PySide6 import QtCore, QtWidgets, QtGui
  2from shiboken6 import wrapInstance
  3from vrKernelServices import vrdImmersiveMenu
  4
  5def getIcon(name):
  6    icon = QtGui.QIcon()
  7    iconPath = "resources:" + name
  8    icon.addPixmap(QtGui.QPixmap("{}".format(iconPath)), QtGui.QIcon.Normal)
  9    return icon
 10
 11class VRSetupMenu(QtCore.QObject):
 12    def __init__(self, parent=None):
 13        super(VRSetupMenu, self).__init__(parent)
 14        self.settings = QtCore.QSettings("Autodesk","VRED")
 15        QtCore.QTimer.singleShot(1, self.init)
 16        self.menuName = QtCore.QCoreApplication.translate("QVRDockWindowManager","Scripts")
 17    def init(self):
 18        self.createMenu()
 19        vrFileIOService.newScene.connect(self.updateMenu)
 20        vrFileIOService.projectLoaded.connect(self.onProjectLoaded)
 21    def __del__(self):
 22        self.destroyMenu()
 23    def createToolsActions(self,tools,internal):
 24        for tool in tools:
 25            if tool.getIsInternal() != internal:
 26                continue
 27            action = QtGui.QAction(tool.getText(), self.mw)
 28            action.setCheckable(True)
 29            key = "VRMenu_"+tool.getName();
 30            if self.settings.contains(key):
 31                hideAway = bool(self.settings.value(key,"false") == "true")
 32            else:
 33                hideAway = tool.getHideAway()
 34            tool.hideAway(hideAway)
 35            action.setChecked(not hideAway)
 36            action.setProperty("tool",tool.getName())
 37            action.toggled.connect(self.actionTriggered)
 38            self.menu.addAction(action)
 39    def createMenu(self):
 40        self.mw = wrapInstance(VREDMainWindowId, QtWidgets.QMainWindow)
 41        self.menu = QtWidgets.QMenu("VR Menu", self.mw)
 42        self.menu.setIcon(getIcon("Various/TransparentIcon.svg"))
 43        self.menu.setTearOffEnabled(True)
 44        showAllAction = QtGui.QAction("Show All Tools", self.mw)
 45        hideAllAction = QtGui.QAction("Hide All Tools", self.mw)
 46        self.menu.addAction(showAllAction)
 47        self.menu.addAction(hideAllAction)
 48        self.menu.addSeparator()
 49        showAllAction.triggered.connect(self.showAll)
 50        hideAllAction.triggered.connect(self.hideAll)
 51        tools = sorted(vrImmersiveUiService.getTools(), key=lambda tool: tool.getName())
 52        self.createToolsActions(tools,True)
 53        self.menu.addSeparator()
 54        self.createToolsActions(tools,False)
 55        self.menu.addSeparator()
 56        showStatusAction = QtGui.QAction("Show Status Panel", self.mw)
 57        showStatusAction.setCheckable(True)
 58        showStatusAction.setChecked(not vrImmersiveUiService.getHideStatusVRPanel())
 59        showStatusAction.toggled.connect(self.showStatus)
 60        self.menu.addAction(showStatusAction)
 61        showParticipantsAction = QtGui.QAction("Show Participants Panel", self.mw)
 62        showParticipantsAction.setCheckable(True)
 63        showParticipantsAction.setChecked(not vrImmersiveUiService.getHideParticipantsVRPanel())
 64        showParticipantsAction.toggled.connect(self.showParticipants)
 65        self.menu.addAction(showParticipantsAction)
 66        self.menu.addSeparator()
 67        showVRMenuAction = QtGui.QAction("Show VR Menu", self.mw)
 68        showVRMenuAction.setCheckable(True)
 69        showVRMenuAction.setChecked(False)
 70        showVRMenuAction.toggled.connect(self.showVRMenu)
 71        self.menu.addAction(showVRMenuAction)
 72        for action in self.mw.menuBar().actions():
 73            if action.text() == self.menuName:
 74                scriptMenu = action.menu()
 75                first = scriptMenu.actions()[0];
 76                scriptMenu.insertAction(first, self.menu.menuAction())
 77                self.separator = scriptMenu.insertSeparator(first)
 78                break
 79    def destroyMenu(self):
 80        for action in self.mw.menuBar().actions():
 81            if action.text() == self.menuName:
 82                action.menu().removeAction(self.menu.menuAction())
 83    def actionTriggered(self,checked):
 84        action =  self.sender()
 85        toolName = action.property("tool")
 86        tool = vrImmersiveUiService.findTool(toolName)
 87        tool.hideAway(not checked)
 88        self.settings.setValue("VRMenu_"+tool.getName(),tool.getHideAway())
 89        self.settings.sync()
 90    def showAll(self):
 91        for action in self.menu.actions():
 92            if not action.property("tool") == None:
 93                action.setChecked(True)
 94    def hideAll(self):
 95        for action in self.menu.actions():
 96            if not action.property("tool") == None:
 97                action.setChecked(False)
 98    def showStatus(self,checked):
 99        vrImmersiveUiService.setHideStatusVRPanel(not checked)
100    def showParticipants(self,checked):
101        vrImmersiveUiService.setHideParticipantsVRPanel(not checked)
102    def updateMenu(self):
103        self.destroyMenu()
104        self.createMenu()
105    def onProjectLoaded(self):
106        QtCore.QTimer.singleShot(0, self.updateMenu)
107    def showVRMenu(self,checked):
108        # Just for testing. Show VR menu in desktop mode
109        if not checked:
110            vrImmersiveUiService.showToolsMenu(False,False)
111            return
112        vrImmersiveUiService.showToolsMenu(True,False)
113        tools = vrImmersiveUiService.findMenu("ToolsMenu")
114        tools.setOrigin(vrdImmersiveMenu.MenuOrigin.ORIGIN_LOCAL)
115        tools.setDepth(3)
116        tools.setWidth(200)
117        tools.setTranslation(0,300,0)
118        tools.setRotation(0,0,0)
119        tools.setOrigin(vrdImmersiveMenu.MenuOrigin.ORIGIN_CAMERA)
120        
121menuSetup = VRSetupMenu()
122
123label = QtWidgets.QLabel(VREDPluginWidget)
124label.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter);
125label.setScaledContents(True)
126label.setText("Python VR menu configure tool\n" + __file__)
127VREDPluginWidget.layout().addWidget(label)