Very often there are requests for how to get python stubs for IDEs - this is not always straight forward and a bit fiddly. I have written a python script to assist making this task easier.
- Requires an install of pyRevit
IN[0]
is the output directory- for VSCode add these lines to settings.json
"python.analysis.extraPaths": [
"C:\\<path to stubs directory>",
],
"python.autoComplete.extraPaths": [
"C:\\<path to stubs directory>",
],
Stubs code
import sys
import traceback
from os import path
import clr
import System
# Add references for stubs
clr.AddReference("ProtoGeometry")
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference("RevitServices")
clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")
clr.AddReference('PythonStubsBuilder')
from PythonStubs import PythonStubsBuilder
def get_assemblies():
return {
assembly.GetName().Name: assembly
for assembly in System.AppDomain.CurrentDomain.GetAssemblies()
}
"""
Requires install of pyRevit
https://pyrevitlabs.notion.site/Install-pyRevit-98ca4359920a42c3af5c12a7c99a196d
Refer to pyRevit GitHub repository which is the primary source for this code
https://github.com/eirannejad/pyRevit/blob/master/extensions/pyRevitDevTools.extension/pyRevitDev.tab/Developer%20Tools.panel/Generate%20APIStubs.pushbutton/script.py
"""
# References for stubs
refs = [
"ProtoGeometry",
"RevitNodes",
"RevitServices",
"RevitAPI",
"RevitAPIUI",
]
assemblies = get_assemblies()
ref_dict = {ref: assemblies.get(ref).Location for ref in refs}
dest_path = IN[0]
if not isinstance(dest_path, str):
raise TypeError("IN[0] is not a path string")
output = []
for k, v in ref_dict.items():
try:
stubs_path = PythonStubsBuilder.BuildAssemblyStubs(
v,
destPath=dest_path
)
output.append('Generated stubs for {} -> {}'.format(k, stubs_path))
except:
output.append('Failed generating stubs for {}\n{}\n{}'.format(k, v, traceback.format_exc()))
OUT = output