Hi everyone,
I’m running into an issue with pyRevit hooks and wanted to ask if there’s a recommended way to disable or suspend them.
I have several hooks implemented (e.g. doc_changed, app_init, and a few others). Some of my scripts are getting triggered recursively, and because the hooks fire again during execution, the scripts end up failing or behaving unpredictably.
My questions are:
-
Is there a way to globally disable pyRevit hooks (temporarily or conditionally)?
-
Or is there a best practice for preventing hooks from re-triggering scripts that are already running?
-
For example, is there a built-in mechanism, context flag, or pattern to safely ignore hook execution during certain operations?
Any guidance, examples, or recommended patterns would be greatly appreciated.
Thanks in advance!
Not really a true ‘disable’ but I read the state of a custom global variable at the start of all my hooks and exit/continue based on its state True/False. This way very little of the hook actually runs if triggered. A simple toggle button sets the state.
script.get_envvar(‘PAUSEHOOKS_ENV_VAR’)
script.set_envvar(‘PAUSEHOOKS_ENV_VAR’, new_state)
1 Like
Hello,
I have finally found the solution to this….
def get_uiapp():
return __revit__
def get_hooks_handler():
return envvars.get_pyrevit_env_var(envvars.HOOKSHANDLER_ENVVAR)
def deactivate():
"""Deactivate all event hooks."""
hooks_handler = get_hooks_handler()
hooks_handler.DeactivateEventHooks(uiApp=__revit__)
def activate():
"""Reactivate all event hooks."""
hooks_handler = get_hooks_handler()
hooks_handler.ActivateEventHooks(uiApp=__revit__)
And then simply wrap your def in try and finally block, see example below :
deactivate() # Turn OFF event hooks before running the tool
try:
# -----------------------------------
# Your main tool logic goes here
# -----------------------------------
run_my_tool()
finally:
# -----------------------------------
# This ALWAYS runs, even if something fails
# Re-enable event hooks here
# -----------------------------------
activate()
1 Like