Revit + Python

Revit API – ModelPathUtils

R

Chances are, at some point in your Pythonic exploration of Revit, you’ll need to interact with file names and/or paths. Whether this is to open or save a file, or to strip information from a file to enter into a database, it’s important to understand how Revit expects Model Paths to be handled. This brings us to ModelPathUtils.

ModelPathUtils Constructors:  There are no Constructors for ModelPathUtils!

ModelPathUtils Methods:  In Revit 2020, there are a 4 methods available to use with ModelPathUtils:

  • .ConvertCloudGUIDsToCloudPath(projectId, modelId):  This method is used to convert a cloud model (think BIM360…) to an actual usable model path. I’ve found this one to be a bit tricky to use, because it requires using other API commands to find both the projectId GUID and the model GUID in order to use it. It’s definitely useful at times but, because it requires a known GUID for each argument, it’s only particularly helpful to use with existing cloud files that are already open within Revit.

  • .ConvertModelPathToUserVisiblePath(path):  This method is incredibly helpful in order to read or modify a file path using string manipulation. By default, a model path is a Python object – it’s not a string of text that’s readable. This method converts the path to a string that then allows a user to search, modify, read, or otherwise manipulate that string. When used in combination with the next method, it makes it very easy to get a path, convert it to be visible, modify it, and then convert back to a path.

  • .ConvertUserVisiblePathToModelPath(string):  This method is the opposite of the previous one. It takes a string as an argument and converts it to a model path object. Many of the file commands within the Revit API require an actual path object (not a string) in order to function. This method makes that happen.

  • .IsValidUserVisibleFullServerPath(string): Finally, this method will validate whether a string is a valid server path and either return ‘True’ if it is, or ‘False’ if it’s not. To be honest, I’ve never had the need to use this method, but it’s helpful to know that it exists.

ModelPathUtils Properties:  Just like Constructors, there are no Properties for the ModelPathUtils class!

So, what’s it look like in your Python code?

path = ModelPathUtils.ConvertModelPathToUserVisiblePath(model_path)
print path
C:\Temp\Revit\revitfile.rvt

or

model = ModelPathUtils.ConvertUserVisiblePathToModelPath('C:\Temp\revit.rvt')
print model
model_path_object


By using the ModelPathUtils class, you’ll ensure that your manipulating your file paths exactly how Revit expects them. Good Luck!

About the author

Andrew Urban

With almost 15 years as a registered architect, and nearly as much experience using Revit, I have recently taught myself Python and have begun to create productivity increasing addons for Revit. I'm am always interested in learning new and innovative ways to use Revit and all other tools of our profession.

By Andrew Urban
Revit + Python

Andrew Urban

With almost 15 years as a registered architect, and nearly as much experience using Revit, I have recently taught myself Python and have begun to create productivity increasing addons for Revit. I'm am always interested in learning new and innovative ways to use Revit and all other tools of our profession.