Hooks not finding button

I made some code that needs to run on a hook (start-up, open doc or app-init all seem viable).
The script works when i test it from a PushButton, But it fails when run from a hookscript.
When run form a hook ( tried all 3 types) it doesn’t seems to find the button itself.

reflections on why this is?

# -*- coding: utf-8 -*-

# imports
# ===================================================
import os

from pyrevit import forms
from pyrevit.coreutils import ribbon

from logUtils import version_check
from dirUtils import ext_dir

# definitions
# ===================================================
def Updatebutton():
    uptodate = version_check()
    if uptodate == True:
        icon_path = os.path.join(ext_dir(),'Tools.tab','Settings.panel','Update.PushButton','on.png')
    if uptodate == False:
        icon_path = os.path.join(ext_dir(),'Tools.tab','Settings.panel','Update.PushButton','off.png')

# Main
# ===================================================
if __name__ == '__main__':
    # Set update button

What is the error?
What does this returns when in hook folder?
To debug you need to know this


The ext_dir() methode returns the os.path of the extension folder, no matter from which depth in the extension it is triggered.

def ext_dir():
    """Methode to locate the extention directory

        _type_: directory (os.path)
    # split directorie
    directories = __file__.split(os.sep)
    # find extension level
    ext_index = directories.index('BB-Tools.extension')
    levels = len(directories) - ext_index  -1
    # return directorie
    result_dir = __file__
    for l in range(levels):
        result_dir = os.path.dirname(result_dir)
    return result_dir

Error is that Nonetype does not have .set_icon() attribute

1 Like

Hi @AlexanderVDB ,

It looks like the get_uibutton can’t retrieve the pyrevit ribbon when run via hooks

what does the following return?


hi @sanzoghenzo,

I tried multiple configs and it seems that pyrevit is able to find the tabs and even the button.
But not the Methode for setting the icon in any hook configureation i tried.

Wierd thing is that in a statup.py script the button is not found on a reload it is (wich also trigger the startup script).


When run from reload (in startup.py):
Name: Update RevitAPIObject: <Autodesk.Revit.UI.PushButton object at 0x0000000000002093 [Autodesk.Revit.UI.PushButton]>

When run from revit startup (in startup.py):

Hi @AlexanderVDB, the error you stated before is due to the get_ubutton not finding the button, and that’s exactly what you found out in your last post.

What I was asking is to run the the first instruction that is inside the get_uibutton function (pay attention at the code of my previous post) to understand if there are some toolbars to look into for your buttons.
If it returns None (and I suspect it will, but I’m still not that knowledgeable of pyrevit internals) that’s because pyrevit tabs are not yet ready during the startup.

Of course, when you reload the tabs/buttons are already there, so you can access them.

If my suppositions are right, this could be a good candidate for a github issue


just to clearify but i also tried your previous code and those also returned an empty list indeed.
Sorry from my end for not clearly stating this!

i’m talking about this code: print(ribbon.get_current_ui().get_pyrevit_tabs())

so indeed it can’t find nothing, but this even happens on doc.opening/ opend hooks as well.
Which is kind of strange since by then the tabs are defenitly already load in ( same for buttons).

Thanks for clarifying this!
It is indeed strange!

I guess it’s time I’ll try some hooks myself (I never used them!)…

One last thing: which ironpython engine do you use? Have you tried other versions to see if the behavior changes?

that would be 2.7 ( see below :slight_smile: )