Batch process - No linked files

Hello, I am using batch process to do some automatic data exports using Windows task planner.

pyrevit run “C:\Users\xxx\AppData\Roaming\pyRevit\Extensions\WYC-tools.extension\lib\BatchProcessor\batch_process.py” “C:\Users\xxx\documents\dummy2024 - 1.rvt” --revit=24.3.30.11 --purge

After the dummy project is opened I can open predefined cloud models (newdoc) and do everything I can in a regular project. But when trying to get the revit links I get nothing. Below is a simplified snippet.

cloud_models is custom class containig all nescessary information based on a Excel file.

def batch_process(cloud_models, logger):
	for model in cloud_models:
		modelPath = ModelPathUtils.ConvertCloudGUIDsToCloudPath(model.cloud_region, model.project_guid, model.model_guid)
		openOptions = OpenOptions()
		openOptions.SetOpenWorksetsConfiguration(WorksetConfiguration(WorksetConfigurationOption.OpenAllWorksets))
		uidoc = HOST_APP.uiapp.OpenAndActivateDocument(modelPath, openOptions, False)
		newdoc = uidoc.Document #type:Document
		
		link_types = FilteredElementCollector(newdoc).OfCategory(BuiltInCategory.OST_RvtLinks).OfClass(RevitLinkType)
		link_instances = FilteredElementCollector(newdoc).OfCategory(BuiltInCategory.OST_RvtLinks).OfClass(RevitLinkInstance)
		for link_type in link_types:
			for link_instance in link_instances:
				if RevitLinkType.IsLoaded(newdoc, link_type.Id):
					if link_instance.GetTypeId() == link_type.Id and ".rvt" in link_instance.Name:
						logger.log(link_instance.GetLinkDocument())

When logging the last line I get nothing, but when running the same code without the batch process it works.

Kind regards.

1 Like

A few things you might try to resolve this.

  1. Are you certain you are logging to an external file, so that it still exists?
  2. Have you tried logging at newdoc, to make sure it’s successfully getting there in pyrevit ru
  3. Before entering the for look, same question, have you logged “link_instances”?

Hello, thank you for your response.

  1. Yes I am logging to an external .log file
  2. Yes, and I am able to make transactions on the newdoc
  3. Yes I have, it is always empty

My initial thought to debug that would be to save to a file (outside the logger utils) every step of the way just to make sure you fetch everything th right way.

Second, the document. The initial document. How are you getting it? Through execparams?

I am using a custom logger class so I don’t think the logger is the issue.

...
    def _log_to_file(self, message):
        with open(self.log_file, 'a') as f:
            f.write(message + '\n')
...

I never use the initial document because I don’t need it. I open each model using the code below.

from pyrevit import HOST_APP
uidoc = HOST_APP.uiapp.OpenAndActivateDocument(modelPath, openOptions, False)
newdoc = uidoc.Document

After some further investigation it seems like the links are not loaded. link_types and link_instances do contain all links.

link_types = FilteredElementCollector(newdoc).OfCategory(BuiltInCategory.OST_RvtLinks).OfClass(RevitLinkType)
for link_t in link_types:
    log_debug.info(RevitLinkType.IsLoaded(newdoc, link_t.Id))   # Returns FALSE for all

link_instances = FilteredElementCollector(newdoc).OfCategory(BuiltInCategory.OST_RvtLinks).OfClass(RevitLinkInstance)
for link_i in link_instances:
    log_debug.info(link_i.GetLinkDocument())    # Returns None for all

I tested the same code in an open project without batch process and all links return a value. So this might be something specific to the batch process?

1 Like

It seems like Revit doesn’t load the links by default when using batch process, so I tried manually loading the links using below code.

link_types = FilteredElementCollector(newdoc).OfCategory(BuiltInCategory.OST_RvtLinks).OfClass(RevitLinkType)
for link_type in link_types:
        if not RevitLinkType.IsLoaded(newdoc, link_type.Id):
                load_result = link_type.Load()  # or link_type.Reload() in newer APIs
                logger.info(str(load_result.LoadResult))

This returns "ExternalServerMissing" for all links.

Explanation from the ApiDocs: The external add-in required to load this link model could not be found.

As I understand the infrastructure under the hood. pyrevit run leverages journal files to launch. This comes with many idiosyncrasies. Something that could be helpful for users to know. Journal files were not intended to be used by users, but do have many secret super powers. An odd one that most people don’t know is that only the addins that are located in the journal file directory at the time the journal file is launched are loaded. So it’s likely you aren’t getting some default Autodesk Addins that are required for cloud loading of the linked files based on where the journal file is being launched from. I haven’t looked at the journal file launching mechanism in pyrevit, but it’s likely being launched from a folder. A few years back, I forced the addins I wanted to run, but putting the .addin files into the launch folder. Reach out if you want some hand holding on this one. You can definitely overcome and the last warning is super helpful to figuring this out.

Thanks a lot for the explanation, I indeed read somewhere the addin responsible for loading Revitlinks might not be loaded. Could you give me some further guidance in how to force or check this?

Kind regards