Pyrevit context menu

Hi everyone,

I’m trying to create a context menu (new in Revit 2025). The contextmenu itself appears, but I can’t seem to add an Item to it. It seems that the BuildContextMenu function just doesn’t run.

#! python 2
# -*- coding: utf-8 -*-

from pyrevit import DB, forms
from pyrevit.revit import HOST_APP
from Autodesk.Revit.UI import IExternalCommand, IContextMenuCreator, ContextMenu, CommandMenuItem

class ContextMenuHandler(IContextMenuCreator):
    def BuildContextMenu(self, context_menu):
        # Create a new menu item
        menu_item = CommandMenuItem("Custom Action", "CustomCommand", __name__)
        menu_item.SetAvailabilityClassName("Autodesk.Revit.DB.View")
        forms.alert("About to add button to context menu")
        context_menu.AddItem(menu_item)

class CustomCommand(IExternalCommand):
    def Execute(self, command_data, message, elements):
        forms.alert("Custom context menu action executed!")
        return DB.Result.Succeeded

# Register context menu for views in the Project Browser
app = __revit__
app.RegisterContextMenu("MyMenu", ContextMenuHandler())

This is the result. I get the menu, but no item:

Anyone got any experience or tips?

0 experience but curious nevertheless.

If I understand correctly you have to

  • first register / set a ContextMenu (I think Revit does this for you)
  • to which you add CommandMenuItems

right?

  • Are all alerts triggered?
  • if you temp. disable menu_item.SetAvailabilityClassName("Autodesk.Revit.DB.View"), does it work?

I disabled the ‘setavailability’ and it doesn’t change anything.

I don’t get any alerts.

  • The one in the customcommand makes sense, because that would only run once I run a command (the one that doesn’t appear).
  • The other one however is more tricky. A contextmenu does appear (which makes me believe the IContextMenuCreator runs) but no item is added to the contextmenu (which makes me believe the ‘buildcontextmenu’ method doesn’t run).

Are you running this as button in Pyrevit?

I did and get the same result. However I think that this needs to be loaded earlier.

from pyrevit import forms
from Autodesk.Revit.UI import IContextMenuCreator, CommandMenuItem

class CustomContextMenu(IContextMenuCreator):
    def BuildContextMenu(self, context_menu):
        forms.alert("BuildContextMenu triggered!")  # Debugging

        if context_menu is None:
            forms.alert("Error: context_menu is None!")
            return

        # Debugging: Show existing menu items
        existing_items = [item.Title for item in context_menu.GetItems()]
        forms.alert("Existing Menu Items: " + ", ".join(existing_items))

        # Create a new menu item and add it to the context menu
        menu_item = CommandMenuItem("Test Menu Item", "TestCommand", __name__)
        context_menu.AddItem(menu_item)

        forms.alert("Menu item 'Test Menu Item' added!")



# Register the context menu in Revit
app = __revit__
try:
    app.RegisterContextMenu("contextmenu", CustomContextMenu())    
    forms.alert("Has RegisterContextMenu: " + str(hasattr(app, "RegisterContextMenu")))    
    forms.alert("Context menu 'contextmenu' registered successfully!")
except Exception as e:
    forms.alert("Failed to register context menu: {}".format(str(e)))

Yeah, that’s what I found somewhere aswell, but running it as a hook on app-init just returns an error that the app has no method registercontextmenu (which is weird, right?)

hmmm interesting.

app = __revit__ does have it.

Using

from pyrevit import forms, HOST_APP
HOST_APP.app

does not

I haven’t tried it hooking it into appinit. Might try that today (in between deadlines)

Okay halfway there.

The app-init doesnt work (I think because at the time it runs, there isn’t any UIapp yet?)
But a startup script does work.
Using my original script in a startup file (in the main folder of a pyrevit extension) works to create the button. So the BuildContextMenu function runs… but the button itself doesn’t yet execute :frowning: