Smartbutton error

I have a smart button that toggles a custom view mode (this is my first attempt at a smartbutton). It is working fine on my machine, but when I deploy it to others, they get this error when opening revit:

ERROR [pyrevit.loader.uimaker] Smart button script import error: <type_id ‘.smartbutton’ name ‘My Tool Name’ @ path\to\my\tool.smartbutton> | ‘NoneType’ object has no attribute 'ActiveView

i copied the self.init function from the override 2D elements function built into pyrevit:

doc = revit.doc
activeView = doc.ActiveView

def __selfinit__(script_cmp, ui_button_cmp, __rvt__):
    off_icon = script_cmp.get_bundle_file('off.png')
    ui_button_cmp.set_icon(off_icon)

It seems to me that it’s trying to get the active view before a document is actually open. The odd thing is that it’s inconsistent. It works on some machines, but not others.

any ideas why this would be happening?

Can you please show the entire code? From what you posted I’d say your missing an import: where does revit come from?
One thing you can try is to put your script inside a main function, place it after the __selfinit__ one and call main() at the end of the file.
Not sure if this solves the problem, bit in plain python this is a good practice to avoid script execution when a module is imported.

the “revit” is pyrevit.revit in this case. Your suggestion of rapping it in main seems to have done the trick though. Thanks for the help!

1 Like