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.
# these commands get executed in the current scope
# of each new shell (but not for canned commands)
#pylint: disable=all
import clr
clr.AddReferenceByPartialName('PresentationCore')
clr.AddReferenceByPartialName('AdWindows')
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName('System')
clr.AddReferenceByPartialName('System.Windows.Forms')
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
else:
break
# alert function
def alert(msg):
TaskDialog.Show('RPS', msg)
# quit function
def quit():
__window__.Close()
This is the exact code that I copied from the link that was in 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():
__window__.Close()
exit = quit
def GetSelectedElements(doc):
"""API change in Revit 2016 makes old method throw an error"""
try:
# Revit 2016
return [doc.GetElement(id)
for id in __revit__.ActiveUIDocument.Selection.GetElementIds()]
except:
# 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
try:
rlapp = [app for app in uiApplication.LoadedApplications
if app.GetType().Namespace == 'RevitLookup'
and app.GetType().Name == 'Application'][0]
except IndexError:
self.RevitLookup = None
return
# tell IronPython about the assembly of the RevitLookup plugin
clr.AddReference(rlapp.GetType().Assembly)
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()
form.SnoopAndShow(self.RevitLookup.Core.Selector.SnoopCurrentSelection)
def SnoopElement(self,element):
if self.IsInstalled():
if element is None:
print("element null object, Please input element to snoop")
return
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:
elementSet.Insert(e)
form = self.RevitLookup.Views.ObjectsView(elementSet)
self.RevitLookup.Core.ModelessWindowFactory.Show(form)
pass
form = self.RevitLookup.Views.ObjectsView(element)
self.RevitLookup.Core.ModelessWindowFactory.Show(form)
def SnoopActiveView():
if self.IsInstalled():
self.SnoopElement(doc.ActiveView)
def SnoopDb(self):
if self.IsInstalled():
form = self.RevitLookup.Views.ObjectsView()
form.SnoopAndShow(self.RevitLookup.Core.Selector.SnoopDb)
_revitlookup = RevitLookup(__revit__)
def SnoopCurrentSelection():
_revitlookup.SnoopCurrentSelection()
'''
## Example :
## _revitlookup.SnoopElement(doc.ActiveView)
## _revitlookup.SnoopElement(959510)
## _revitlookup.SnoopElement(doc.ActiveView.Id)
'''
def SnoopElement(element):
_revitlookup.SnoopElement(element)
def SnoopActiveView():
_revitlookup.SnoopActiveView()
def SnoopDb():
_revitlookup.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!