Having trouble with the rps_init.py file

Hi everyone, I am new to coding with the API and learning using the video links in the Github repo. I am currently on the third video called Python for Revit: Basics of collecting data from Revit database, but when I type el to get the element information I get this error (Traceback (most recent call last):
File “”, line 1, in
NameError: name ‘el’ is not defined) do I need a more updated script or is there another Revit dependency that I don’t have? I am using Revit 2021 at the moment.

Thank you!

Hi Callie and welcome,
Would you mind sharing the complete code?

# these commands get executed in the current scope
# of each new shell (but not for canned commands)
#pylint: disable=all
import clr

from Autodesk.Revit import DB
from Autodesk.Revit import UI

import Autodesk.Windows as aw

# creates variables for selected elements in global scope
# e1, e2, ...
max_elements = 5
gdict = globals()
uiapp = __revit__
uidoc = uiapp.ActiveUIDocument
if uidoc:
    doc = uiapp.ActiveUIDocument.Document
    selection = [doc.GetElement(x) for x in uidoc.Selection.GetElementIds()]
    for idx, el in enumerate(selection):
        if idx < max_elements:
            gdict['e{}'.format(idx+1)] = el

# alert function
def alert(msg):
    TaskDialog.Show('RPS', msg)

# quit function
def quit():

This is the exact code that I copied from the link that was in the tutorial.

The error does not look like it is coming from the code you are running
But are you running the code in the second window like in the picture?

I just added a print statement, and it does what it should

after that you have 2 def that are not in use but I guess that will be used in the remainder of the tutorial

It is for the InitScript, I think it is supposed to do all the imports automatically when you open the RPS. I looked at the code in the script again and it looks very different from the code in the video.

Well the video is probably a couple of years back at least.
So if the code is different, it is not that shocking.
Using th elatest RPS I don’t have any issues

and for this one the init.py code looks like this and does not throw any errors

init.py code for latest RPS

# these commands get executed in the current scope
# of each new shell (but not for canned commands)
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Architecture import *
from Autodesk.Revit.DB.Analysis import *

uidoc = __revit__.ActiveUIDocument
doc = __revit__.ActiveUIDocument.Document

from Autodesk.Revit.UI import TaskDialog
from Autodesk.Revit.UI import UIApplication

def alert(msg):
    TaskDialog.Show('RevitPythonShell', msg)

def quit():

exit = quit

def GetSelectedElements(doc):
    """API change in Revit 2016 makes old method throw an error"""
        # Revit 2016
        return [doc.GetElement(id)
                for id in __revit__.ActiveUIDocument.Selection.GetElementIds()]
        # old method
        return list(__revit__.ActiveUIDocument.Selection.Elements)

selection = GetSelectedElements(doc)
# convenience variable for first element in selection
if len(selection):
    s0 = selection[0]

# ------------------------------------------------------------------------------
import clr
from Autodesk.Revit.DB import ElementSet, ElementId

class RevitLookup(object):
    def __init__(self, uiApplication):
        for RevitSnoop to function properly, it needs to be instantiated
        with a reference to the Revit Application object.
        # find the RevitLookup plugin
            rlapp = [app for app in uiApplication.LoadedApplications
                     if app.GetType().Namespace == 'RevitLookup'
                     and app.GetType().Name == 'Application'][0]
        except IndexError:
            self.RevitLookup = None
        # tell IronPython about the assembly of the RevitLookup plugin
        import RevitLookup
        self.RevitLookup = RevitLookup

    def IsInstalled(self):
        if not self.RevitLookup:
            print('RevitLookup not installed. Visit https://github.com/jeremytammik/RevitLookup to install.')
            return False
        return True

    def SnoopCurrentSelection(self):
        if self.IsInstalled():
            form = self.RevitLookup.Views.ObjectsView()

    def SnoopElement(self,element):
        if self.IsInstalled():
            if element is None:
                print("element null object, Please input element to snoop")
            if isinstance(element, int):
                element = doc.GetElement(ElementId(element))
            if isinstance(element, ElementId):
                element = doc.GetElement(element)
            if isinstance(element, list):
                elementSet = ElementSet()
                for e in element:
                form = self.RevitLookup.Views.ObjectsView(elementSet)
            form = self.RevitLookup.Views.ObjectsView(element)

    def SnoopActiveView():
        if self.IsInstalled():

    def SnoopDb(self):
        if self.IsInstalled():
            form = self.RevitLookup.Views.ObjectsView()

_revitlookup = RevitLookup(__revit__)

def SnoopCurrentSelection():

## Example :
## _revitlookup.SnoopElement(doc.ActiveView)
## _revitlookup.SnoopElement(959510)
## _revitlookup.SnoopElement(doc.ActiveView.Id)

def SnoopElement(element):

def SnoopActiveView():

def SnoopDb():

# ------------------------------------------------------------------------------

# a fix for the __window__.Close() bug introduced with the non-modal console
class WindowWrapper(object):
    def __init__(self, win):
        self.win = win

    def Close(self):
        self.win.Dispatcher.Invoke(lambda *_: self.win.Close())

    def __getattr__(self, name):
        return getattr(self.win, name)

    def set_font_sizes(self, size):
        self.rps_repl = self.win.Content.Children[0].Children[0].Content.Children[0]
        self.rps_editor = self.win.Content.Children[2].Children[1].Children[0]
        self.rps_repl.FontSize = size
        self.rps_editor.FontSize = size

__window__ = WindowWrapper(__window__)

Thanks Jean-Marc! That works for me now, I was thinking that code was very old, but this works perfect! Hopefully I will become a contributing member of the community soon!

1 Like