Thanks for looking into it.
The CLR imports are for importing Windows forms & WPF for the UI that I am using.
@pyrevti Thanks for the suggestion. I tried adding that to the bundle.yaml file. No change.
The errors are only thrown when Rocket mode setting is enabled and extension.json contains "rocket_mode_compatible": "True". When attempting to reproduce the bug, please make sure both of those things are true. Disable either of those and the errors cease.
FilteredElementCollector (and all other imports) throw a NameError every time they are called from the UI-triggered external event, even though they are imported at the top of the script. I even tried lazy importing during the Execute() method, but NameError still gets thrown.
Here is the debug log when first activating the pushbutton:
DEBUG [pyrevit.perf] [PERF:py] pyrevit.__init__:before labs: 13ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.labs:entry: 311ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.labs:after clr.AddReference block (14 pyRevitLabs DLLs): 2ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.labs:after `from pyRevitLabs import` block: 17ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.labs:exit: 1ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.__init__:after labs (exit): 0ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.revit:entry: 61ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.revit:after `from pyrevit.revit.db import *`: 8ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.revit:after db.query/select/create/update/ensure/delete: 46ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.revit:after db.transaction *: 0ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.revit:after db.failure: 0ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.revit:after db.pickling *: 2ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.revit:after journals *: 19ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.revit:after selection *: 3ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.runtime:entry: 7ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.userconfig:entry: 14ms
DEBUG [pyrevit.coreutils.git] Loading dll: C:/Users/***/AppData/Roaming/pyRevit-Master/bin/netfx/LibGit2Sharp.dll
DEBUG [pyrevit.perf] [PERF:py] pyrevit.userconfig:after imports: 11ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.userconfig:find_config_file x3 (HOME / ALLUSER / USER): 1ms
DEBUG [pyrevit.userconfig] Using User config file: C:/Users/***/AppData/Roaming/pyRevit/pyRevit_config.ini
DEBUG [pyrevit.userconfig] Debug mode is enabled in user settings.
DEBUG [pyrevit.perf] [PERF:py] pyrevit.userconfig:verify_configs(CONFIG_FILE): 111ms
DEBUG [pyrevit.userconfig] Debug mode is enabled in user settings.
DEBUG [pyrevit.perf] [PERF:py] pyrevit.userconfig:PyRevitConfig(__init__): 27ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.userconfig:upgrade.upgrade_user_config: 214ms
DEBUG [pyrevit.userconfig] Debug mode is enabled in user settings.
DEBUG [pyrevit.perf] [PERF:py] pyrevit.userconfig:user_config.save_changes: 19ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.userconfig:exit (user_config ready): 385ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.runtime:after imports: 2ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.runtime:after dotnet dir listings: 1ms
DEBUG [pyrevit.perf] [PERF:py] userconfig.get_active_cpython_engine:GetAttached: 0ms
DEBUG [pyrevit.perf] [PERF:py] userconfig.get_active_cpython_engine:clone.GetCPythonEngines: 1ms
DEBUG [pyrevit.userconfig] cpython engines dict: {<pyRevitLabs.PyRevit.PyRevitEngineVersion object at 0x00000000000005A1 [3123]>: <pyRevitLabs.PyRevit.PyRevitEngine object at 0x00000000000005A2 [CPY3123 (netfx) | Kernel: CPython | Version: 3123 | Runtime: False | Path: "C:/Users/***/AppData/Roaming/pyRevit-Master/bin/cengines/CPY3123/python312.dll" | Desc: "CPython Engine"]>}
DEBUG [pyrevit.perf] [PERF:py] pyrevit.runtime:after user_config.get_active_cpython_engine(): 12ms
DEBUG [pyrevit.runtime] Building on IronPython engine: 2712
DEBUG [pyrevit.perf] [PERF:py] pyrevit.runtime:after calculate_dir_hash + BASE_TYPES_DIR_HASH: 19ms
DEBUG [pyrevit.runtime] Interface types assembly file is: pyRevitLabs.PyRevit.Runtime.2023
DEBUG [pyrevit.perf] [PERF:py] pyrevit.runtime:after assmutils.find_loaded_asm: 25ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.runtime:exit (after 4x find_type_by_name): 1ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.revit:after ui: 5ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.revit:after tabs: 1ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.revit:after events: 8ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.revit:after report: 12ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.revit:after files: 1ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.revit:after serverutils: 2ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.revit:after geom: 0ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.revit:after units: 1ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.revit:after features: 0ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.revit:after bim360/dc3dserver/tmpgfx (exit): 15ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.revit:after bim360/dc3dserver/avf (exit): 66ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.forms._ipy:entry: 172ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.forms._ipy:after coreutils+logger+colors: 33ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.forms._ipy:after framework re-imports: 0ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.forms._ipy:after pyrevit.api.AdWindows: 0ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.forms._ipy:after `from pyrevit import revit, UI, DB`: 0ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.forms._ipy:after forms.utils + forms.toaster: 3ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.forms._ipy:after pyrevit.versionmgr: 0ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.forms._ipy:after `from pyrevit.userconfig import user_config`: 0ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.forms._ipy:after pyevent: 2ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.forms._ipy:after Autodesk.Windows/Internal: 0ms
DEBUG [pyrevit.perf] [PERF:py] pyrevit.forms._ipy:exit (all class defs done): 12ms
Here is the error traceback, which does not occur on activating the pushbutton, but only when clicking the button in the dialog that pops up:
Traceback (most recent call last):
File "C:\***.tab\Advanced.panel\Advanced Functions.pulldown\NameErrorMRE.pushbutton\script.py", line 47, in Execute
self.func()
File "C:\***.tab\Advanced.panel\Advanced Functions.pulldown\NameErrorMRE.pushbutton\script.py", line 99, in recenter_all_tags_in_view
misplaced_in_view = self.misplaced_in_view
File "C:\***.tab\Advanced.panel\Advanced Functions.pulldown\NameErrorMRE.pushbutton\script.py", line 90, in misplaced_in_view
result = [room_tag for room_tag in self.room_tags_in_view if not room_tag.IsInRoom]
File "C:\***.tab\Advanced.panel\Advanced Functions.pulldown\NameErrorMRE.pushbutton\script.py", line 80, in room_tags_in_view
result = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_RoomTags).WhereElementIsNotElementType().OwnedByView(self.active_view.Id)
NameError: name 'FilteredElementCollector' is not defined
AI suggested the following:
If this is really an ExternalEvent or modeless UI callback
If your “External Command” is actually part of a modeless WPF window, dockable pane, ExternalEvent, IExternalEventHandler, or a Revit callback registered from pyRevit, then the issue is even more likely: Revit calls Execute() later, outside the original pyRevit script execution context.
For those cases:
-
Do not rely on globals from script.py.
-
Put handler logic in a lib module.
-
Import inside Execute().
-
Do not store doc, uidoc, view, Selection, Element, or Transaction objects globally.
-
Use element ids, stable config data, or simple DTO/state objects instead.
I’m not sure the above is really the issue, though, since it used to work in previous releases of pyRevit. That is the main reason I think it is a pyRevit bug.
Thank you all for your assistance.