Opening document issue with pyrevitCLI run batch

I have been trying to setup an automated batch script, but my script fails at the very beginning when trying to open the model file.

I have tried using the bare example from the docs below:

from pyrevit import HOST_APP

# __models__ is set to a list of model file paths
# __models__ = ['C:\model1.rvt']
for model in __models__:
    uidoc = HOST_APP.uiapp.OpenAndActivateDocument(model)
    doc = uidoc.Document
    # do something here with the document

but the last line fails as uidoc is always None

I’ve also tried using the DB Application.OpenDocumentFile(…) method to get a doc without a uidoc, but then I get an Exception that the “Document can not be opened”.

I can see Revit opening the file (if I run an higher version I even see the update dialog), and the UI flashes when the model is loaded, but I can never get a valid UIDocument or Document.

I have a feeling I am doing something wrong, but can’t figure out what…

you need to re-assign the uidoc variable after opening the file.

uidoc = HOST_APP.uiapp.OpenAndActivateDocument(model)
uidoc = HOST_APP.uiapp.ActiveUIDocument
doc = uidoc.Document

idk why you have to do this, but it works.

ideally OpenAndActivateDocument() would return the opened uidoc, but it appears that return value is getting swallowed somewhere in the HOST_APP wrapper.