<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" /> Start with the short introduction to hook scripts at Create Your First Hook

</aside>

<aside> <img src="https://s3-us-west-2.amazonaws.com/secure.notion-static.com/b9bd683a-6d4b-451b-b985-a3b83a9620ec/alert.png" alt="https://s3-us-west-2.amazonaws.com/secure.notion-static.com/b9bd683a-6d4b-451b-b985-a3b83a9620ec/alert.png" width="40px" /> Current pyRevit implementation supports hook scripts in IronPython, CPython, C#, and VB.Net

See Anatomy of IronPython Scripts for more information about IronPython scripts and the support modules See Anatomy of CPython Scripts for more information about CPython scripts and the support modules See Anatomy of .NET (C#, VB) Scripts for more information about .NET scripts and the support modules

</aside>

<aside> <img src="https://s3-us-west-2.amazonaws.com/secure.notion-static.com/d1a7ead2-be9c-41d7-a8b5-7dc7bee259dc/ic_01_idea.png" alt="https://s3-us-west-2.amazonaws.com/secure.notion-static.com/d1a7ead2-be9c-41d7-a8b5-7dc7bee259dc/ic_01_idea.png" width="40px" /> The examples here are all part of the pyRevitDevHooks.extension (Part of the BASEEX deployment. See Deployments )

</aside>

Python Hook Scripts


Both IronPython and CPython engines provide the two __eventsender__ and __eventargs__ global variables to give you access to the event sender and arguments provided by Revit. Avoid using these variables directly though. The pyrevit module gives you safe access to them using EXEC_PARAMS as shown below. This way you will mask your scripts against interal pyRevit runtime changes should the name of these global variables ever change.

↓ Runtime Global Variables

# ONLY FOR EVENT HOOKS

# event sender
__eventsender__
# event arguments
__eventargs__

↓ Safe Access Method

from pyrevit import EXEC_PARAMS

# event sender
EXEC_PARAMS.event_sender
# event arguments
EXEC_PARAMS.event_args

See the Revit API documentation for the event argument types provided by EXEC_PARAMS.event_args (differ based on the event type). There are plenty of examples for hook scripts under the pyRevitDevHooks.extension. Below are examples of a hook script named view-activated.py

<aside> <img src="https://s3-us-west-2.amazonaws.com/secure.notion-static.com/8ab25e9f-c19a-4925-b270-e0a6dc60f782/ic_01_idea.png" alt="https://s3-us-west-2.amazonaws.com/secure.notion-static.com/8ab25e9f-c19a-4925-b270-e0a6dc60f782/ic_01_idea.png" width="40px" /> In the test examples below, the hook scripts write some information about the event to a log file named hooks.log on user desktop.

</aside>

IronPython

<aside> <img src="https://s3-us-west-2.amazonaws.com/secure.notion-static.com/a9fe63ec-39bd-4fb2-8c4c-6b6e7bc70384/alert.png" alt="https://s3-us-west-2.amazonaws.com/secure.notion-static.com/a9fe63ec-39bd-4fb2-8c4c-6b6e7bc70384/alert.png" width="40px" /> pyRevit caches the IronPython engine that is used for event hooks. Reload pyRevit if you are making changes to the modules being imported by the hook scripts to refresh the IronPython engine

</aside>

The IronPython hook scripts are almost identical to

from pyrevit import EXEC_PARAMS

# hooks_logger is a helper module to provide write methods to hooks.log
# that all python hook scripts can use
import hooks_logger

# __file__ is set to the path of the hook script, per python conventions
hooks_logger.log_hook(__file__,
    {
        "cancellable?": str(EXEC_PARAMS.event_args.Cancellable),
        "doc": str(EXEC_PARAMS.event_args.Document),
        "current_view": str(EXEC_PARAMS.event_args.CurrentActiveView),
        "prev_view": str(EXEC_PARAMS.event_args.PreviousActiveView),
        "status": str(EXEC_PARAMS.event_args.Status),
    }
)

CPython

The CPython hook script is identical to IronPython. Just start the first line with the CPython identifier.

#! python3

.NET Hook Scripts