<aside> <img src="https://s3-us-west-2.amazonaws.com/secure.notion-static.com/01f9b8a6-3c36-442b-bed3-ec193007d66f/ic_01_idea.png" alt="https://s3-us-west-2.amazonaws.com/secure.notion-static.com/01f9b8a6-3c36-442b-bed3-ec193007d66f/ic_01_idea.png" width="40px" /> Understanding how extensions work, is a prerequisite for this guide.
See an introduction to extension bundles at Create Your First Command See Extension Bundles for more detailed information about extensions See Extension Hooks/ for detailed information about hooks
</aside>
Hook scripts are specially named scripts that would hook up to the Revit event system and will get executed whenever that event occurs. For example, a doc-changed.py
hook script gets executed automatically every time DocumentChanged event occurs in Revit.
The hook scripts live inside the hooks/
subdirectory in an extension bundle.
Let's create a hook script, to listen to a ViewActivated event (fires when a new view is activated in the Revit user interface) and check whether the view name is formatted correctly. We are going to write this script in IronPython, but you can use more languages in extension hooks. See Extension Hooks/ for more information.
Create a python file under our test extension from Create Your First Command
MyExtensions/
└── MyFirstExtension.extension/
└── hooks/
└── view-activated.py
Make sure the file is named correctly as view-activated.py
otherwise the hook system will not see or use this script.
Let's open the file and add the code below.
from pyrevit import forms
from pyrevit import EXEC_PARAMS
forms.alert(EXEC_PARAMS.event_args.CurrentActiveView.Name)
Now reload pyRevit and (assuming your extension is active), pyRevit will hook up this script into the ViewActivated event of the running Revit instance. Once pyRevit is reloaded, the hook should be active and running.
If you open a new view in your model, you should get a popup with the name of the view
Ok, let's talk about the script now. On the first line we are importing the pyRevit forms module to use the ready-made alert()
function to show the popup window.
from pyrevit import forms
On the second line we used the EXEC_PARAMS.event_args
to access the event arguments that are passed to the ViewActivated event by Revit. The type for this EXEC_PARAMS.event_args
object instance is ViewActivatedEventArgs.
Look that up in the API documents and you can see all its properties. As you can see the event args object, provides access to the current view (CurrentActiveView) and the previously active view (PreviousActiveView)
On the second line we are using the CurrentActiveView property of the ViewActivatedEventArgs instance to get access to the active view (of type View) and get its view name.
forms.alert(
EXEC_PARAMS.event_args.CurrentActiveView.ViewName
)