Generating Python Stubs

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
5 Likes

@Mike.Buttery ,

you mean auto-complete based on Revit version in Dynamo Python window ! yes

grafik

in PyCharm it is a dream

how does it work? in my case i got errors

is this refered to my Revit folder ?

in PyRevit Toolbar it works smooth

KR

Andreas

The warning is to do with the windows .net System
PythonStubsBuilder is from pyRevit - that is why it is a requirement
Which version of Revit and Dynamo are you using?

@Mike.Buttery ,

grafik

Revit 2024, pyRevit is up to date

PythonStubsBuilder is part of pyRevit or have to import it extra ?

Ok, i think i understand, my Stubs are working in PyCharm, so dynamo can just use PyRevit content.

KR

Andreas

pyRevit has it’s own version in Dev tools which is also usable, however I have provided additional references to using the Dynamo API and RevitSevices that pyRevit does not cover

1 Like

@Draxl_Andreas
I’m not at my workstation - the warning you get confuses me a bit as CurrentDomain has been part of .net for a very long time.

Perhaps try this
for assembly in System.AppDomain.GetAssemblies()

What I am doing here is using .net to get the dll file path for the reference so users don’t have to find it themselves. The stubs will also be current Revit & Dynamo version you are using.

I’ll have a deeper look in the new year when I am back from a break as I was going to attempt to extend it to C3D and AutoCAD.

Cheers

1 Like